Kaprekar's Routine
Sort the digits descending and ascending, subtract, repeat. 4-digit inputs converge to 6174; 3-digit inputs to 495. Other lengths settle into a fixed point or short cycle — try it.
What do these numbers and charts mean?
The routine, in one sentence
Pad the number to N digits, sort the digits descending (call it A) and ascending (B), and replace the number with A − B. Repeat. The sequence of values produced is the trajectory; where it ends up is the terminal.
Per-run statistics
Steps — how many Kaprekar steps the routine took. For inputs that converge (e.g. 4-digit), this is the number of steps to reach the fixed point. For inputs that fall into a cycle, this is how many steps you took to enter the cycle (once you're in, you loop forever).
Unique values — how many distinct numbers appeared in the trajectory, counting the start. For convergent runs this equals steps + 1. For cycles it's smaller, because cycle members repeat.
Max reached / Min reached — the largest and smallest numbers seen during the routine. The trajectory chart visualises how the value moves between these.
Digit-sum range — the smallest and largest digit sum across all values in the trajectory. Notice: after the very first step, every digit sum is a multiple of 9 (see the invariant below).
Terminal — the end state. Either a fixed point (a number that maps to itself, like 6174 or 495) or a k-cycle (a loop of k distinct numbers the routine repeats forever).
Width — the number of digits the routine is operating on, including leading zeros. The width changes the behavior: 52 as 2 digits enters a 5-cycle, while 0052 as 4 digits converges to 6174.
All step diffs ÷ 9 — a mathematical invariant of the routine, not a measurement.
Charts
Value trajectory — the actual value at each step. Convergent runs settle to a flat line at the fixed point; cycles oscillate between the cycle members forever (the green dot marks where the terminal begins).
Digit sum chart — the digit sum at each step, with dashed horizontal lines at every multiple of 9. Because of the divisibility invariant, after step 1 every point lands exactly on a dashed line. The starting point may not.
Population analysis (widths ≤ 15)
This runs the Kaprekar routine on every N-digit starting value (all 10N of them, including leading-zero forms like 0052) and aggregates the results.
Histogram — steps to terminal — the x-axis is "how many steps until this starting value reached its terminal", the y-axis is "how many starting values fell in that bucket". Bucket 0 holds the terminals themselves (fixed points and cycle members). Your input's bucket is highlighted in amber.
Terminal table — one row per distinct end state across all starts. Fixed rows are single self-mapping numbers; cycle rows are loops the routine repeats forever. The Share column shows what fraction of all N-digit starts eventually ends there.
Max steps to terminal — the worst case across every start: the slowest-converging value. For 4-digit inputs this is exactly 7 — the famous Kaprekar bound.
Slowest starts — the actual digit multisets that hit max steps. Click any pill to load it as your input.
Basin of attraction — a radial map of every digit multiset that converges to your terminal. The terminal sits at the center, each ring is one step farther out, and edges show the Kaprekar successor. Two numbers with the same digits collapse to one node (e.g. 3524, 5432, 4253 all live on the "2345" point).
Note: "steps" in the population view counts multiset transitions to the terminal's digit-set. For some inputs this is one less than the integer-steps shown in the per-run summary — that happens when a chain enters the terminal's multiset via a non-fixed-point integer (only matters for widths 3, 5, 6 in practice).
What to look for at each width
2 digits: no fixed point besides 00. Every non-trivial start funnels into the 5-cycle 09 → 27 → 45 → 63 → 81 → 09.
3 digits: 99% of starts converge to 495 in ≤ 6 steps. The other 1% are the ten all-same-digit values, which collapse to 000.
4 digits: the classic case. 99.9% of starts reach 6174, the original Kaprekar's constant, in ≤ 7 steps.
5 digits: no fixed point exists. Starts split among three different cycles — the famous "Kaprekar's constant doesn't generalize" result.
6 digits: two distinct fixed points coexist (549945 and 631764) along with a 7-cycle, so the answer depends on where you start.
About Kaprekar's routine — history, math, references
Origin
Dattatreya Ramachandra Kaprekar (1905–1986) was an Indian recreational mathematician and schoolteacher in Devlali, Maharashtra. Working largely outside academic circles, he discovered the 4-digit constant 6174 in 1949 and published the result in An Interesting Property of the Number 6174 (Scripta Mathematica 15, pp. 244–245). Kaprekar published widely on number-theoretic curiosities; several other ideas carry his name — Kaprekar numbers, Demlo numbers, Harshad numbers — though they are distinct concepts from this routine.
The classical results
Every 4-digit number whose digits are not all identical reaches 6174 within at most 7 iterations of the routine, and 6174 is the unique non-trivial fixed point at width 4. The 3-digit analogue is 495, reached in at most 6 iterations. For widths 5 and above, no single Kaprekar constant exists — the routine produces a mix of fixed points and short cycles, which the population analysis above enumerates exhaustively up to width 15.
The structural reason every step result is divisible by 9
The descending and ascending arrangements of a number's digits share the same digit multiset, so they have the same digit sum, so they have the same residue mod 9. Their difference is therefore always a multiple of 9. This invariant is the structural reason 6174 = 9 × 686 and 495 = 9 × 55, and it is what the dashed gridlines on the digit-sum chart visualise.
Generalisations
The routine extends naturally to other number bases — base 2, base 16, etc. — each with its own fixed-point and cycle structure. Variants have been studied with signed digits, complex digits, and on permutation groups more broadly. The map is also of interest in the theory of dynamical systems on finite sets, where every trajectory eventually enters a periodic orbit because the state space is finite.
References
- D. R. Kaprekar, An Interesting Property of the Number 6174, Scripta Mathematica 15 (1949), pp. 244–245.
- Yutaka Nishiyama, Mysterious number 6174, Plus Magazine (2006).
- Wikipedia: Kaprekar's routine · D. R. Kaprekar
- OEIS has several sequences related to the routine — search for "Kaprekar map" to find fixed-point and trajectory-length sequences.
- Numberphile (YouTube) has several popular videos on 6174 — search "Numberphile 6174".
Value trajectory
Digit sum (multiples of 9 dashed)
Show steps
Population analysis: all -digit starts
Steps to terminal — histogram across all starts
Slowest starts
These take the maximum number of steps to reach the terminal — click any to load and run it.