BrainDen.com - Brain Teasers
• 0

# A self-referential number

Go to solution Solved by kukupai,

## Question

Can you find a seven digit number which describes itself. The first digit is the number of zeros in the number. The second digit is the number of ones in the number, etc. For example, in the number 21200, there are 2 zeros, 1 one, 2 twos, 0 threes and 0 fours.

## Recommended Posts

• 0
• Solution

3211000

##### Share on other sites
• 0

There is only one such number and kukupai found it.

```public class Class1 {

public static void main(String[] args) {
int[] digits;
for(int i=0;i<10000000;i++){
digits = getDigits(i);
if(isSelfReferential(digits)){
System.out.printf("%07d\n",i);
}
}
}
private static int[] getDigits(int num) {
int[] d = new int;
for(int i=0;i<d.length;i++){
d[i] = num % 10;
num = num / 10;
}
return d;
}
private static boolean isSelfReferential(int[] d) {
for(int i=0;i<d.length;i++){
int count = 0;
for(int d1:d){
if(d1==i){
count++;
}
}
if(d[d.length-1-i] != count){
return false;
}
}
return true;
}
}
```

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account. ×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.