Jump to content
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.

Link to post
Share on other sites

2 answers to this question

Recommended Posts

  • 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[7];
		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;
	}
}

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

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

Loading...
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...