Friday, March 4, 2011

A puzzle (mathematics) - properties of decimal number system

5, March 2011.
I come across one geek blog, and there is one interesting question... "Given a number containing all numbers from 1 through 9, find next higher number with same digits!".

The question should trigger a hint. "Same digits" mean the next higher number will have all same digits (In other words, the difference between N and Nnext should be multiple of 9). Since all the digits are same in both the numbers, difference between the two numbers will cancel out, and makes the next higher number increase by a factor which is multiple of 9. Also, since the number is higher one, we should look for increasing patterns from right to left.
For example consider

N = 125786493

We can see 125786439 can't be next higher number. We must look for first three digits. 125786493 should be transformed to 125786934 with a difference of 441 which is multiple of 9. Or simply 49 x 9 (observe the original number).

Another number - 157869324 transformed to 157869342 with difference of 18 = 2 x 9. Obviously, there is a pattern.

Logic is clear, can we write the program easily, ....? Not really! Try out.

Another variant "Find the next higher number with same number of set bits", I had published an article here.

No comments: