Kaprekar's Routine
9/11/2022
Attention Conservation Notice
A quick look at an iterative algorithm that leads to a fixed cycle. I have no idea if there exist any applications outside of recreation. This is probably a little light/hand-wavey for a mathematician and not useful enough for a pragmatist.
Kaprekar's Routine
Kaprekar's routine is a simple iterative algorithm that leads to a fixed point. It is so simple that anyone can play with it and have fun. In that respect, it reminds me a bit of the Collatz conjecture.
A simple version of Kaprekar's Routine:
- Choose a 4 digit number (in base 10) in which there are at least 2 unique digits
- Rearrange the digits in descending order to obtain a number
- Rearrange the digits in ascending order to obtain a number
- Find the difference
- Repeat the process starting from , adding leading 0 if the number goes below 1000.
By running this routine, every starting 4 digit number converges to the number 6174 in at most 7 iterations. 6174 is known as Kaprekar's Constant.
Here is an example with the starting number 8989:
↪ 6174 found! Took 4 iterations
This relationship exists for 3 digit numbers as well, except the fixed point is the number 495. Interestingly, for 3 digits the starting number converges in <= 6 iterations. Here is an example:
↪ 495 found! Took 6 iterations
This is a recursive map: where is the Kaprekar Operation that maps the input to steps 2, 3, 4 above, i.e. where is the th step at the beginning of this section.
Once the fixed point is found, it cycles:
4 Digit Version:
3 Digit Version:
Try it yourself:
Iteration Counting
A reasonable next question: What is the likelihood that a given starting number will take iterations to reach the fixed point?
I generated the iteration lengths for each starting number in the range 1000 to 9999. Here is a histogram of the results:
Interestingly 3 steps is the most likely number of steps for any given number. This also shows that aside from 3, it is more likely that the iterations will be higher (closer to 7) than smaller. This gives us a sense of the tree that leads to our fixed point. It is bushy around the 3rd level and has many longer branches beyond iteration 3.
The Wiki article has a nice image of the decision tree:
We can also look at the indexed distribution:
This plot was made by reshaping the 9000 starting numbers into a matrix. With this "coiling" a nice picture shows up and symmetry becomes apparent.
Uniqueness of the 4-Digit Case
Consider the operation that is performed by . We can write steps 2 and 3 (putting numbers in descending/ascending order) like so:
For four integers where and they are not all the same digit, the max ordering is and the min ordering is . Subtracting the two (step 4), gives us:
which leads to the following relations:
We can say we have found a kernel of Kaprekar's operation if
is equal to some combination of and the inequality
constraint holds [2]. If this happens, then the sorting
steps will make it such that applying Kaprekar's operation again will
lead to our fixed point.
Since there are only possible permutations of we can generate all the permutations and swap with them. This will leave us with a system of 4 equations with 4 unknowns, which we can then solve for.
It turns out that there is only one combination that has an integer solution and follows the constraint,
therefore, and 6741 is the only number unchanged by Kaprekar's operation with 4 digits.
You can tinker with this yourself below:
Is this special?
Let's consider this problem in a different light. Suppose there are n people at a party, each of which has a hat. At the end of the night, everyone grabs a hat off the hat rack and leaves. What is the probability that no one left without their hat?
This problem is equivalent to counting permutations with no fixed points (called derangements).
The solution to the hat problem above is given on Wikipedia here. The key result is that the total number of derangements, , is given by:
i.e. there are about derangements and the probability that a random permutation is a derangement is
Therefore, the probability that a random permutation isn't a derangement (i.e. that there is at least 1 fixed point) is
We can think of Kaprekar's routine as a permutation group whose elements are permutations of the natural numbers and whose group operation is a composition of that finds a fixed point, i.e. for some , , .
Even though Kaprekar's Routine is interesting, it probably isn't that
special. There are uncountably many routines that lead to a fixed
point
[5], so there are likely many more digit lengths/bases in
which Kaprekar's Routine gives an interesting result.
Sources/Notes
[1] https://en.wikipedia.org/wiki/Kaprekar's_routine
Good examples, a nice formal specification, a flow chart and an alternative base table
[2] https://ijpam.eu/contents/2012-80-3/8/8.pdf
Cool little discussion of the uniqueness of Kaprekar's constant for n = 4
There are kernels for some other numbers as well:
| Digits | Kernel |
|---|---|
| 2 | None |
| 3 | 495 |
| 4 | 6174 |
| 5 | None |
| 6 | 549945, 631764 |
| 7 | None |
| 8 | 63317664, 97508421 |
| 9 | 554999445, 864197532 |
| 10 | 6333176664, 9753086421, 9975084201 |
[3] https://arxiv.org/pdf/1710.06308.pdf
Some proofs about the composition of intermediate numbers in Kaprekar cycles.
Exploration of Kaprekar's in other bases
Turns out that in base 3, on average it takes 3 iterations to reach a fixed point
[4] https://scholarscompass.vcu.edu/cgi/viewcontent.cgi?article=1287&context=jmsce_vamsc
Different ordering types for these routines, i.e. instead of 123, 321 there is also analogues of Kaprekar's permutation step like 123, 312.
[5] https://math.stackexchange.com/questions/4183370/what-is-the-logic-behind-kaprekars-constant
The way I see it: imagine all the possible procedures you could do on all the possible intervals of numbers. You're looking for one reasonably simple procedure on an easily described interval which has the property that it has a unique fixed point. Loads of procedures have fixed points (e.g. Brouwer's theorem; 1−1/e≈63% of all permutations are not derangements and therefore have at least one fixed point; etc.). Some of them will have unique fixed points. Finding a particularly nice procedure with a unique fixed point is probably going to be challenging, but when crowd-sourced it's not too surprising that somebody found one.
- Joshua P. Swanson