This is a challenging puzzle to find a nine digit number, using the numbers 1 to 9, and using each number once without repeats, such that;
- the first two digits form a number divisible by 2;
- the first three digits form a number divisible by 3,
- the first four digits form a number divisible by 4,
- the first five digits form a number divisible by 5;
- And so on until we get a nine digit number divisible by 9.
You might try, for example, the number being 923,156,784. But this number doesn't work — the first three digit number, 923, is not divisible by 3. Again, 987,654,321 doesn't work as well — the first seven digit number, 9,876,543, is not divisible by 7.
Can you find the nine digit number that works?
Hints:
Let the number be ‘abcdefghi’
The fifth digit e=5, because multiples of 5 end in 5 or 0
Alternate digits must be even, so the rest have to be odd.
Therefore, a, c, g and i are elements of {1,3,7,9}
And the remaining b, d, f and h are elements of {2,4,6,8}
Solution:
*
*
*
Consider following conditions:
a could be anything as it is divisible by 1.
b is even.
(a+b+c) is divisible by 3.
(2c+d) is divisible by 4.
e is 5
(4a+4b+4c+4d+4e+f) is divisible by 6.
(a+5b+4c+6d+2e+3f+g) is divisible by 7. (It is quite complicated)
(4f+2g+h) is divisible by 8.
Considering all these conditions, the only possible answer is 381654729.
Number which you got is correct and i wrote a programme to be prove this is the only number .
ReplyDeletepublic class NineDigitMagic
{
public static void main(String args[]){
long globalcount=0;
for (int i=987654321;i>=123456789;i--){
globalcount++;
String s=i+"";
boolean unique=(s.matches(("^(?!.*(.).*\\1)\\d{9}")));
boolean zero= !(s.contains( "0" ));
if(zero&&unique){
int count =0;
for(int inner=9;inner>=1;inner--){
if(Integer.valueOf(s.substring(0,inner))%inner==0)
{
count++;
}
}
if(count==9)
{
System.out.println(i+" is magic number");
}
}
}
System.out.println("globalcount"+globalcount +"times ");
}
}