Kaprekar’s constant

The Indian number theorist, DJ Kaprekar, discovered this interesting property of 4-digit integers.

  • Take a four-digit number.  
  • Arrange the digits in ascending order and descending order.  
  • Subtract the smaller from the larger.  
  • Repeat the same process with the answer.  
  • Astonishingly, four-digit numbers eventually reach the four-digit number 6174.  
  • This number is called Kaprekar’s Constant, after its discoverer.

For Example, take the number 4527:

7542 – 2457 = 5085

8550 – 0558 = 7992

9972 – 2799 = 7173

6543 – 3456 = 3087

8703 – 0378 = 8352

8532 – 2358 = 6174

7641 – 1467 = 6174, and the process repeats.

Some numbers do not comply, however.  

For example 1000 gives 1000 – 0001 = 999 and 999 – 999 = 0.  

Your program mustn’t crash for these numbers.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.