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

For example, a two-digit number has 4 combinations: 11, 12, 21, 22. A three-digit number has 8 combinations: 111, 112, ... 222.

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

thanks

PS This refers to Delphi :)

+4
source share
1 answer

Answer 2 N where N is the number of digits.

This is a purely mathematical problem and relates to the most basic combinatorics. It is easy to see why 2 N is the correct answer. Indeed, there are two ways to select the first digit. For each such choice, there are two ways to select the second digit. Therefore, there are 2 × 2 ways to choose a two-digit number. For each such number, there are two ways to add a third digit, making 2 × 2 × 2 ways of constructing a three-digit number. Consequently, there are

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

ways to build an N-digit number.

In Delphi, you calculate 2 N on Power(2, N) ( uses Math ). [A less naive way that works for N <31, 1 shl N ]

+15
source

All Articles