Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the number of combinations from some number that contains only two different digits?

For example, two-digit number have 4 combinations: 11, 12, 21, 22. Three-digit number have 8 combinations: 111, 112,...222.

  • How to get number of combinations for number that have 4, 5, ... 10 or more digits?

Thanks

P.S. This refers to the Delphi :)

like image 337
Srdjan Vukmirica Avatar asked Dec 06 '22 13:12

Srdjan Vukmirica


1 Answers

The answer is 2N, where N is the number of digits.

This is a purely mathematical problem, and concerns very basic combinatorics. It is easy to see why 2N is the right answer. Indeed, there are two ways to choose the first digit. For each such choice, there are two ways to chose the second digit. Hence, there are 2×2 ways to chose a two-digit number. For each such number, there are two ways to add a third digit, making 2×2×2 ways to construct a three-digit number. Hence, there are

2 × 2 × ... × 2 = 2^N

ways to construct a N-digit number.

In Delphi, you compute 2N by Power(2, N) (uses Math). [A less naïve way, which works for N < 31, is 1 shl N.]

like image 200
Andreas Rejbrand Avatar answered Dec 29 '22 01:12

Andreas Rejbrand