Kaprekar’s Constant

Description

  1. Pick any integer in the interval (0, 9999), not including integers that consist entirely of repeating digits (e.g. 1111, 2222, etc.).
  2. Arrange the digits in descending and then in ascending order to get two four-digit numbers, adding leading zeros if necessary.
  3. Subtract the smaller number from the bigger number.
  4. Repeat step 2.

Going through this process, known as  Kaprekar's routine, yields an interesting result: The number this process ends in will always be 1674 (feel free to test this yourself). This number is aptly known as Kaprekar's constant. By generalizing this process to numbers that are not four-digits, other interesting constants and patterns become apparent.

The program takes in the starting number: for example, 7695. Then, the program takes in the desired number of digits and will fill in zeroes where necessary to reach the desired number of digits: continuing the previous example if the desired number of digits is 4, we would get 9765 as the largest number, whereas 5 would give 97650 as the largest. The program repeats the process until it comes across a previous number again, implying there is a cycle and the same set of numbers will continue to be produced since the process does not change.

>
TagsPythonProjectDate06/10/2021

More Projects