0

I developing a simple algorithm and would like to know how I can use formulas to find a number of combinations of two in a row of digits.

For example:

The row of digits: 0, 1, 2; 
Possible combinations are: (0, 1), (0, 2), (1, 2)
The number of combinations is: 3
The row of digits: 0, 1, 2, 3
Possible combinations are: (0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2,3) 
The number of combinations is: 6

How do I find a number of unique combinations knowing the number of digits in the row?

2 Answers2

2

This is a simple combinatorics problem. You have $N$ distinct elements, and you want to arrange them in group of $k$, where the order doesn't count (I imagine that, to say, $1, 0$ as an element is the same as $0, 1$). So you invoke combinations (order doesn't count) without repetitions (cannot have $1, 1$ to say):

$$C = \frac{N!}{k!(N-k)!}$$

In your second case for example $N = 4$, $k =2$ hence

$$C = \frac{4!}{2!2!} = \frac{24}{4} = 6$$

If you do not know that the symbol "!" means, that is the factorial.

Enrico M.
  • 26,114
1

It looks like you want to pick distinct pairs of numbers with the lower number of the pair coming first.

Say you have four numbers. The first one can be any of four, but the second one must be different from the first one, so there are only three choices. Now of those choices, half of them are with the first number bigger than the second one. You don't want those, so you divide by two so that you only get the ordered pairs. The total is $4\times3/2=6$.

The general solution for this problem is to calculate $n(n-1)/2$, and mathematicians represent this with the symbol $n\choose2$ which is read "n choose two". If you have three numbers, then ${3\choose2}=3$ for example, as in your upper case. If you have a million numbers then you can calculate $1,000,000\choose 2$.

If you want to pick say three numbers in a row then you change the bottom number to 3.

Suzu Hirose
  • 11,660