# Thread: Combinations of digits - formula needed.

1. ## Combinations of digits - formula needed.

Hi there,

Lets look at this little problem...

- Given the integer number 123, combining its digits in all possible positions, the results are:
123, 132, 213, 231, 312, 321
... a total of 6 combinations.

- Doing the same with number 1234, the results are:
1234, 1243, 1324, 1342, 1423, 1432, 2134, 2143, 2314, 2341, 2413, 2431,
3124, 3142, 3214, 3241, 3412, 3421, 4123, 4132, 4213, 4231, 4312, 4321
... a total of 24 combinations.

So, my question is: what formula can I use to get the total number of combinations, given L as the digits length?

Thanks

2. Originally Posted by Betty
Hi there,

Lets look at this little problem...

- Given the integer number 123, combining its digits in all possible positions, the results are:
123, 132, 213, 231, 312, 321
... a total of 6 combinations.

- Doing the same with number 1234, the results are:
1234, 1243, 1324, 1342, 1423, 1432, 2134, 2143, 2314, 2341, 2413, 2431,
3124, 3142, 3214, 3241, 3412, 3421, 4123, 4132, 4213, 4231, 4312, 4321
... a total of 24 combinations.

So, my question is: what formula can I use to get the total number of combinations, given L as the digits length?

Thanks

it's n factorial

$n!= n \cdot (n-1)\cdot (n-2) \cdot ...$

so n=6

$6! = 6\cdot 5\cdot 4 \cdot 3 \cdot 2\cdot 1 = 720$

3. Originally Posted by Betty
Hi there,

Lets look at this little problem...

- Given the integer number 123, combining its digits in all possible positions, the results are:
123, 132, 213, 231, 312, 321
... a total of 6 combinations.

- Doing the same with number 1234, the results are:
1234, 1243, 1324, 1342, 1423, 1432, 2134, 2143, 2314, 2341, 2413, 2431,
3124, 3142, 3214, 3241, 3412, 3421, 4123, 4132, 4213, 4231, 4312, 4321
... a total of 24 combinations.

So, my question is: what formula can I use to get the total number of combinations, given L as the digits length?

Thanks
Are all the digits different?

CB

4. Thanks!

It was easy, and I forgot
The little "!" is a killer, indeed.

I'm using it on a programming function. Amazing, it seams no native support for it in several languages, so we need to go with a recursive function or a loop, as I did (VB.NET):

Private Function Factorial(ByVal length As Integer) As Integer
Dim res As Integer = 1
For i = 1 To length
res *= i
Next
Return res
End Function

Are all the digits different?
No. The same digit can repeat.

Actually, the digits are not digits, are letters, but for the purposes of my application, all computations are done numerically, and not in string or char data types, for obvious efficiency.

Kind regards,

5. Originally Posted by Betty

No. The same digit can repeat.

Kind regards,

if they can repeat than just n! can't do you any help

that's in the case that they can't repeat ... as you wrote there for 1,2,3

Originally Posted by Betty
123, 132, 213, 231, 312, 321
than is not included 111 and such ....

6. Originally Posted by Betty
...

No. The same digit can repeat.

Actually, the digits are not digits, are letters, but for the purposes of my application, all computations are done numerically, and not in string or char data types, for obvious efficiency.

Kind regards,
Then factorials just don't cut it.

If you can use any letter any number of times then using your example, if you have 4 different letters that you can repeatedly use to use to compose a 4 letter "word" (sequence of letters), then there are $4^4$ different words you can put together.

n different symbols that you can repeat can form $n^k$ diferent sequences of length k.
on every 'place' in the sequence of length k, you can always choose any of n available 'symbols'.

7. if they can repeat than just n! can't do you any help
Oh, I guess I wasn't clear.

The digits can repeat, but can not change.

By repeat I mean "1434"... there are two 4 digits.

But given "1434", the digits can not change to anything they aren't, such as "1444"... because there weren't three 4's in the first place.

This is a permutation of digits. Restricted to the original digits (that are not all necessarly different, such as "1234", can be also "1434"

8. Originally Posted by Betty
Oh, I guess I wasn't clear.

The digits can repeat, but can not change.

By repeat I mean "1434"... there are two 4 digits.

But given "1434", the digits can not change to anything they aren't, such as "1444"... because there weren't three 4's in the first place.

This is a permutation of digits. Restricted to the original digits (that are not all necessarly different, such as "1234", can be also "1434"
than is okay

9. than is okay
Yes, the factorial is the answer for permutations.

Thank's again, guys!

By the way, someone mentioned the word "word"... so here it goes the permutations:

word, wodr, wrod, wrdo, wdor, wdro, owrd, owdr, orwd, ordw, odwr, odrw,
rwod, rwdo, rowd, rodw, rdwo, rdow, dwor, dwro, dowr, dorw, drwo, drow

4!

10. Originally Posted by Betty
Yes, the factorial is the answer for permutations.

Thank's again, guys!

By the way, someone mentioned the word "word"... so here it goes the permutations:

word, wodr, wrod, wrdo, wdor, wdro, owrd, owdr, orwd, ordw, odwr, odrw,
rwod, rwdo, rowd, rodw, rdwo, rdow, dwor, dwro, dowr, dorw, drwo, drow

4!
You know that with repeated charaters that the answer given is wrong, it assumes they are all distinguishable.

That is you treat 1434 as different from 1434.

CB

11. Where there are repeated characters you can use the multinomial coefficient

Multinomial Coefficient -- from Wolfram MathWorld

For example, "excellent" there are 3 E's, 1 X, 1 C, 2 L's, 1 N, and 1 T, giving

$(3,1,1,2,1,1)! = \dfrac{(3+1+1+2+1+1)!}{(3!)^1(2!)^1(1!)^4}$

12. You know that with repeated charaters that the answer given is wrong, it assumes they are all distinguishable.
Good point, thanks. For the purpose of my word anagram application, I sure can't tell "alma" is distinguishable from "alma". They are the same, so I shouldn't count them twice as if different, like the factorial n! does.

you can use the multinomial coefficient
Thanks. I better implement that function in code, instead of factorial.

13. Originally Posted by Betty
Amazing, it seams no native support for it in several languages
I think there is a good reason for this; at 13! you overflow 32-bit ints, and at 21! you overflow 64-bit ints.