# A number with self-referential digits

Which five-digit number has the $n$th digit representing how many occurrences of the digit $(n-1)$ there are in the number, for all $n\le5$?

For example, the fourth digit represents the number of 3s, and the first digit represents the number of 0s.

The solution is

21200

This can be found by simply iterating from a starting point. Let's choose 40000 as a start (it's as good as any).

40001 — count four zeroes, one four
31001 — count three zeroes, one one, one four
22010 — count two zeroes, two ones, one three
21200 — count two zeroes, one one, two twos
21200 — count two zeroes, one one, two twos

The next row counts the numbers it sees in the previous row. Once we hit two identical numbers back-to-back, we have settled on a solution.

You'll see that 43210, 11111, 22222, 33333, and 44444 are not issues if you make one simple admission.

43210
11111
05000
400001 — allow counting the 5, but don't permanently extend the length
41000
31001
22010
21200
21200

• Just don't start with a permutation of 43210 ;-) All starting points converge to the solution, aside from these permutations and the even more obvious failures 11111, 22222, 33333, 44444. – Steve Jessop Aug 22 '15 at 12:31

cjm shows in his answer that there is only one correct solution by using a Perl program to brute force the puzzle.

This can also be demonstrated logically:

First digit:

• Cannot be 0, because then there would be at least 1 zero
• Cannot be 1, because it would mean that there would be at least a 3, in the best case (1xx10). No matter where you put it, there's no final possible solution.
• Cannot be 3, because the 4th digit would have to be at least a 1, and all other digits, including the one representing the number of 1s would have to be 0
• Cannot be a 4, because all other digits, including the one describing the number of 4s would have to be 0s
• Cannot be a 5, because itself would then have to be a 0

Second digit:

• Cannot be a 0, because then only one of the last digits can be a 0, so there would have to be, in the best case, at least a 3 in the number, giving us, in the best case, 20310, which is false.
• Cannot be a 2, 3, 4 or 5, because with more than one 1 we would have less than two 0s.

Third digit:

• Cannot be a 0, because we already have a 2
• Cannot be a 1, because we already have one 1 and need only one
• Cannot be a 3, 4 or 5 because the remaining two digits need to be 0s, so we wouldn't have place to fit them

Fourth and fifth digits:

• Must be 0s, because we need at least 2.

Therefore the number is 21200.

21200

There is :

2 zeroes, 1 one, 2 twos, 0 three, 0 four

The other answers give a correct solution, but they don't indicate whether it is the only solution.

In fact, 21200 is the only solution.

This is shown by this brute-force Perl solution:

for (10000 .. 44444) {
say $_ if substr($_, 0, 1) == tr/0//
and substr($_, 1, 1) == tr/1// and substr($_, 2, 1) == tr/2//
and substr($_, 3, 1) == tr/3// and substr($_, 4, 1) == tr/4//;
}


There are no solutions for 1,2 and 3 digits.

There are two solutions for 4 digits:

1210 and 2020

There is one for 5 digits:

21200

One for 7 digits:

3211000

Is there any other possible solution?

And the general math solution (or attempt to build one):

Since the first digit counts the number of 0s and the 2nd digit the number of 1s etc and since the total number digits is 5 the sum of all the digits must be 5. As a counter example if you take a number like 11112 this would imply there are a total of 6 digits which isn't possible. So we need five digits adding up to 5. Possible combinations are:- 11111, 01112, 00122, 00113, 00023 and 00014. If we encode these 6 possibilities according to the rule we get:- 11111 -> 05000 (no zeros, 5 ones, no 2s, 3s or 4s); 01112 -> 13100; 00122 -> 21200; 00113 -> 22100; 00023 -> 30110; 00014 -> 31001; Only one of these is a rearrangement namely 00122 which goes to 21200 which would map onto itself under the rule and so is the solution.