Playground · Kaprekar
Kaprekar’s routine.
Every four-digit number ends up at 6174. I didn’t believe it either.
I found a video about 6174 and couldn’t leave it alone. The claim sounded too easy, take almost any four-digit number, follow one simple rule, and you always land on the same answer. I went digging (looking for calculators, read articles, watched more videos) until I understood why, then built this so you can see it for yourself.
The rule
Take a four-digit number with at least two different digits. Sort its digits from high to low, then from low to high, and subtract the smaller arrangement from the larger. Do it again with the answer. Keep going and you will always reach 6174, Kaprekar’s constant, in seven steps or fewer. Once you’re there, the routine sticks forever.
The maths
Zeros count. 3087 sorts to 8730 and 0378, with the zero kept at the front. The one exception to the maths is a number where all four digits match, like 1111, or 7777, these won’t work.