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.
There are several steps within each iteration of the algorithm. It's actually quite simple.
Input: 1234
Unique digits: 1,2,3,4
Input: 3087
Unique digits: 3,0,8,7
Input: 8352
Unique digits: 8,3,5,2
Input: 6174
Unique digits: 6,1,7,4
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.
Input: 4544
Unique digits: 4,5
Input: 0999
Unique digits: 0,9
Input: 8991
Unique digits: 8,9,1
Input: 8082
Unique digits: 8,0,2
Input: 8532
Unique digits: 8,5,3,2
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.
Input:
Unique digits: