Kaprekar's Constant

What is this?

In short, Kaprekar's Constant is a fixed point in an iterative algorithm invented by Indian mathematician D.R. Kaprekar. This algorithm is called, you guessed it, Kaprekar's Routine. The basic idea is that if you use any 4-digit number with at least two unique digits and run it through this algorithm, you will reach the number 6174 in no more than seven steps. 6174 is the endpoint of the process.

There are different values besides 6174 in non base 10 systems, but that is well beyond the scope of this project. I am not a mathematician. I am just a dude who stumbled upon this funny little math thing.

The Process

There are several steps within each iteration of the algorithm. It's actually quite simple.

  1. Receive some input. The parameters were touched on previously, but to reiterate: The input must be 4-digits in length. The input must contain at least two unique numbers.
  2. Rearrange the input, sorting the numbers in both ascending and descending order. These are your "maximum" and "minimum" values available from the provided input.
  3. Get the difference of the max and min values we established in the previous step.
  4. Run that new value (the calculated difference) through the process.
  5. Repeat until the result is 6174. At that point the result will always be 6174.
example 1

Input: 1234

Unique digits: 1,2,3,4

4321
- 1234
3087

Input: 3087

Unique digits: 3,0,8,7

8730
- 0378
8352

Input: 8352

Unique digits: 8,3,5,2

8532
- 2358
6174

Input: 6174

Unique digits: 6,1,7,4

7641
- 1467
6174

Observations

I did notice a quirk about this algorithm that might be worth pointing out. If the input has only two unique numbers, those two numbers are sequential, and there are three instances of one and one of the other, it sort of breaks the process.

For example, if the input is 4544 the resulting calculation is 5444 - 4445 = 999. The result in cases such as these will always be 999. This is also true if the large and small numbers are swapped (using 5455 as the input will yield the same result). You can see why we are in a bit of a pickle at this point. The algorithm depends on an input with 4 digits.

To get around this we have to cheat a little bit and add a leading zero, passing along 0999 as our input. By doing this the resulting calculation will be 9990 - 0999 = 8991, which we can then use to continue the routine. This also means that the steps to arrive at 6174 for such scenarios will always be the same.

example 2

Input: 4544

Unique digits: 4,5

5444
- 4445
999

Input: 0999

Unique digits: 0,9

9990
- 0999
8991

Input: 8991

Unique digits: 8,9,1

9981
- 1899
8082

Input: 8082

Unique digits: 8,0,2

8820
- 0288
8532

Input: 8532

Unique digits: 8,5,3,2

8532
- 2358
6174

There are plenty of funny quirks like that.

Try inputting different types of patterns and sequences, then using variations of that pattern/sequece. For example: 1313, 2424, and 3535. Or 1135, 3357, and 5579. As I've been building and iterating on this, I have found myself strangely fascinated by the whole thing and will just spend time entering random numbers to see the steps play out. But I am totally willing to accept that I am the weirdo here.

Try it out.

Not enough unique digits Enter a number other than 6174

Results

Input:

Unique digits: