Kaprekar Routine Calculator

Sort the digits, subtract, repeat: where does it end?

  • Any digits and base
  • Fixed points, cycles
  • Steps for every start
A small rounded clay table with a soft clay funnel on it, rounded clay beads sliding down its sides toward a single bead at the bottom in soft daylight.

The guide

Sort the digits, subtract, repeat: why four digits land on 6174 and other sizes do not

Kaprekar's routine turns a number into the gap between its largest and smallest rearrangement. Four digits always end at 6174, three at 495, and many other sizes end in loops.

  • 6174 where every four-digit start with two different digits ends
  • 7 steps the most any four-digit start needs to reach 6174
  • 495 where every three-digit start with two different digits ends
  • 13 steps the longest run of any six-digit start in base 10
Read the full guide Fold the guide 7 min · 13 sources

One move, repeated

Take a four-digit number, say 3524. Sort its digits from largest to smallest to get 5432. Sort them from smallest to largest to get 2345. Subtract the second from the first: 5432 minus 2345 is 3087. Now do the same to 3087, and then to what comes out. That is Kaprekar’s routine, and it fits on a napkin.

The numbers it produces do not wander. From 3524 the run goes 3087, then 8352, then 6174. From 6174 the move gives 7641 minus 1467, which is 6174 again, so the run has nowhere new to go. A number that the rule sends to itself is called a fixed point, and 6174 is the one that made the routine famous.

D. R. Kaprekar was an Indian recreational mathematician, born in 1905, who taught school in Devlali for his whole career and worked largely alone. MacTutor reports that he first found the four-digit result in 1946, announced it at the Madras Mathematical Conference in 1949 and published it in Scripta Mathematica in 1953. MacTutor adds that many Indian mathematicians laughed at his number ideas and thought them trivial. This one has outlived the dismissal.

The rule is not limited to base 10 or to four digits. Any base and any digit count give a version of the routine, and each one has its own endings. The calculator on this page lets you pick two to eight digits and a base from 2 to 16, then shows every step as a subtraction.

The detail that chatbots drop

There is one rule that makes the whole thing work, and it is easy to lose. The number must keep its digit count. Start at 1000. The largest rearrangement is 1000 and the smallest is 0001, so the step is 1000 minus 0001, which is 999. Written with four digits, that is 0999, and the run continues: 0999 gives 9990 minus 0999, which is 8991, then 8082, 8532 and 6174.

Drop the leading zero and everything changes. Read 999 as a three-digit number and its sorted forms are both 999, so the answer is 0 and the run is over after two steps. Checked by computer, 77 of the 9,000 four-digit numbers from 1000 to 9999 end at 0 when zeros are dropped, which is exactly the count MacTutor gives. The other 8,923 reach 6174. Keep the zeros and only the repdigits, whose digits are all equal, end at 0.

That is why the page writes every step with its full width. A step such as 8820 minus 0288 is not a typing error. The ascending sort of 8082 really does begin with a zero, and Wikipedia notes that the sorted numbers may have leading zeros. A claim that every four-digit number reaches 6174 is wrong for a different reason, because repdigits like 3333 reach 0 and never come near it.

Counting the steps needs one more decision. This page counts the start as step 0, so 8991 reaches 6174 in 3 steps: 8082, 8532, 6174. A start that already lies on its ending needs 0 steps, which is true of 6174 itself. Count the start as step 1 instead and every figure in this essay grows by one, so the four-digit maximum would be 8, not 7.

Why four digits all end up in one place

Wikipedia states that any four-digit number in base 10 with at least two different digits reaches 6174 within seven iterations. The map panel checks this by brute force. It runs every one of the 10,000 starts, 0000 to 9999, with leading zeros kept. Of those, 9,990 are not repdigits, and all 9,990 reach 6174. Counted by steps, one start needs 0 steps (6174 itself), and 383, 576, 2,400, 1,272, 1,518, 1,656 and 2,184 starts need 1 through 7.

So the commonest run is three steps long, 2,400 starts take it, and no start needs more than 7 steps. Only 715 different sets of four digits exist, and the answer depends on the gaps between the sorted digits, so the 10,000 starts give just 55 different first answers. That is a proof, but only by exhaustion. It checks a finite list and finds no exception, which is a complete argument and a fairly unlovely one.

A little algebra explains why the list is short. Write the sorted digits of a four-digit number as a, b, c, d with a at least b at least c at least d. The largest rearrangement is 1000a plus 100b plus 10c plus d, and the smallest is 1000d plus 100c plus 10b plus a. Their difference is 999 times (a minus d) plus 90 times (b minus c). Only the gaps matter, and only after sorting.

That formula also shows why every answer is a multiple of 9. Both 999 and 90 are. The general reason, noted by Wikipedia, is that the two rearrangements have the same digit sum, so they leave the same remainder when divided by one less than the base, and their difference leaves none. The digital root, a repeated digit sum, rests on the same fact.

Three digits are simpler still. The middle digit cancels, the answer is 99 times (a minus c), and after one step a run can only be one of 0, 99, 198, 297, 396, 495, 594, 693, 792 or 891. All 990 starts with two different digits end at 495, in at most 6 steps. Wikipedia reports that in 1981 G. D. Prichett and colleagues showed the only nonzero constants in base 10 are 495 and 6174.

When the ending is a loop

Three and four digits are the tidy cases. Two digits already behave differently. Start at 12 and the run goes 09, 81, 63, 27, 45 and then back to 09, so the ending is a cycle of five numbers rather than a single one. Ninety of the 100 two-digit starts reach that loop, and the ten repdigits reach 0.

A cycle is a set of numbers that the rule visits in turn, and a fixed point is just a cycle of length 1. In dynamics both are examples of an attractor: a set of states that runs tend toward from a wide range of starts, together with the starts that lead into it. The routine is a tiny, fully countable example of the idea, with no randomness and no approximation.

Five digits in base 10 have no single constant. The 100,000 starts split between two cycles of four numbers, with 48,480 starts reaching the first and 48,320 the second, and a third cycle of two numbers, 53955 and 59994, which takes 3,190. Six digits mix both kinds of ending: a cycle of seven numbers collects 935,520 starts, the fixed point 631764 collects 62,520, and 549945 collects 1,950. The longest six-digit run takes 13 steps.

The list of fixed points keeps growing, and OEIS records it: 0, 495, 6174, 549945, 631764, 63317664 and on, with a note that there are no seven-digit fixed points. Wikipedia adds that in base 4 the numbers 3021 and 310221 are fixed points. The calculator confirms both by running the routine, and finds 31102221 as an eight-digit one in the same family.

Why every start must end somewhere

The map has one rule and a finite set of numbers, and that combination is enough to force an ending. A run that keeps producing new numbers would need infinitely many of them. By the pigeonhole principle, if more items are placed than there are places, some place is used twice, so a run must eventually repeat a number. From then on it repeats the same loop for ever.

Graph theorists draw this as a functional graph, one arrow out of every number. Iterating the function follows a path along the arrows, and in a finite graph every path ends in a cycle. The map panel does exactly that for every start at once. It walks from each unlabelled number until it meets a number already seen, names the cycle if the walk closes on itself, and labels the way back.

Each cycle collects a basin: the starts that flow into it. The table of endings in the map panel lists the basins with their sizes, and the histogram shows how long the descent takes. In base 10 the count of nonzero endings runs 1, 1, 1, 3 and 3 for two to six digits, so the page reports the figures for each size instead of promising a pattern.

Reproducing this, and what it cannot tell you

You can follow every figure here with the calculator. Enter 3524 at four digits in base 10 and read the three subtractions, or 1000 and read 1000 minus 0001. Choose base 4 and enter 3021 to see a fixed point at once. The map runs in a separate background thread, which MDN describes as a way to keep heavy work from slowing the page, and a size above four million starts is refused with a message that names the cap.

The numbers here are small. The biggest value is below 16 to the power 8, which is 4,294,967,296, far under the largest integer a JavaScript number holds exactly, 9,007,199,254,740,991, so no rounding enters. The figures were checked against a separate script that runs the routine from every start and agrees on every count.

There are limits. The page covers only this routine. It does not handle other digit-sorting maps, and it does not touch Kaprekar numbers, the squares that split into two parts summing to the number itself, which are a different idea by the same man. It proves nothing beyond the finite sizes it runs. One run is capped at eight digits and the map at four million starts.

Run it on your own sizes, and watch what changes. Two, three and four digits in base 10 each end in one place, while five digits split three ways. Change the base and the endings change again. The routine is a rule a child can follow, and the structure it hides is still being counted.

Sources

Every source consulted for this guide. Links checked November 10, 2026.

  1. Kaprekar's routine Wikipedia
  2. D. R. Kaprekar Wikipedia
  3. D R Kaprekar (1905 - 1986) - Biography - MacTutor History of Mathematics MacTutor, University of St Andrews
  4. A099009 Fixed points of the Kaprekar mapping A151949 (n) = n' - n'', where in n' the digits of n are arranged in descending, in n'' in ascending order. The On-Line Encyclopedia of Integer Sequences
  5. A151949 a(n) = image of n under the Kaprekar map n -> (n with digits sorted into descending order) - (n with digits sorted into ascending order). The On-Line Encyclopedia of Integer Sequences
  6. Attractor Wikipedia
  7. Fixed point (mathematics) Wikipedia
  8. Repdigit Wikipedia
  9. Digital root Wikipedia
  10. Pigeonhole principle Wikipedia
  11. Pseudoforest Wikipedia
  12. Number.MAX_SAFE_INTEGER MDN Web Docs
  13. Web Workers API MDN Web Docs