Skip to main content
Submitted by manoj on 21 October 2023
coding-cability

Indian mathematician D.R. Kaprekar observed a very interesting property of number 6174.

If you take any four digit number (let's say 3281) and arrange its digits in descending order (8321) and ascending order (1238) and then subtract the smaller number from the larger number -- and if you repeat that process, you will reach number 6174.

For example:

3281: 8321 - 1238 = 7083 8730 - 0378 = 8352 8532 - 2358 = 6174

Number 6174 - when you do this process, will result into 6174 itself.

Exception to this is a four digit number with all same digits (such as 1111, 2222, 3333 etc.), as that will result into 0 in the very first step.

Your task is to write a function that finds out that for all four digit numbers (from 1000 to 9999), how many steps does it take to reach the magic number (6174). The function should also find out if it can't reach the number 6174 for whatever reason.

Hint: Provide this hint to candidate if needed: Define a threshold - a large number of steps - after which, your function should give up and declare that a given four digit number can't reach the magic number.