TOPICS
Search

Kaprekar Routine


The Kaprekar routine is an algorithm for rearranging and subtracting digits, introduced for four-digit numbers by D. R. Kaprekar (Deutsch and Goldman 2004) and generalizable to k-digit numbers. To apply the Kaprekar routine to a number n, arrange the digits in descending (n^') and ascending (n^('')) order. Now compute K(n)=n^'-n^('') (discarding any initial 0s) and iterate, where K(n) is sometimes called the Kaprekar function. The algorithm reaches 0 (a degenerate case), a constant, or a cycle, depending on the number of digits and the starting value n. The list of values is sometimes called a Kaprekar sequence, and the result K(n) is sometimes called a Kaprekar number (Deutsch and Goldman 2004), though this nomenclature should be deprecated because of confusion with the distinct sort of Kaprekar number.

In any integer base b>=2, the two rearrangements have the same digit sum and are congruent to it modulo b-1. Their difference is therefore divisible by b-1, so every output in decimal notation is divisible by 9.

In decimal notation, the nonzero fixed points n satisfying K(n)=n are given by 495, 6174, 549945, 631764, ... (OEIS A099009). Similarly, the numbers n lying on cycles of the map K with length k>=2 are given by 53955, 59994, 61974, 62964, 63954, 71973, ... (OEIS A099010).

Iterating the Kaprekar map in decimal notation, all one- and two-digit numbers give 0. Exactly 60 three-digit numbers, namely 100, 101, 110, 111, 112, 121, 122, 211, 212, 221, ... (OEIS A090429), reach 0, while the rest give 495 in at most 5 iterations. Exactly 77 four-digit numbers, namely 1000, 1011, 1101, 1110, 1111, 1112, 1121, 1211, ... (OEIS A069746), reach 0, while the remainder give 6174 in at most 7 iterations. The value 6174 is sometimes known as Kaprekar's constant (Deutsch and Goldman 2004). For five-digit numbers, the nonzero outcomes are three cycles of lengths 2, 4, and 4, as listed below. Iterations are counted as applications of the map K, without counting the initial number.

If leading 0s are retained so that each iteration uses the same number of digits, the outcome can change. For example, dropping leading 0s gives 100->99->0, while retaining three digits gives 100->099->891->792->693->594->495.

For four decimal digits, all starting values whose digits are not all equal reach 6174 in at most 7 iterations, while the values with four identical digits reach 0 (Nishiyama 2006). Numberphile (2011) illustrates the four-digit routine.

The following table lists nonzero cycles in various bases b, with 0 shown when the zero cycle is the only outcome. The columns start with d=2 digits, and all cycle values are written in decimal notation.

bpossible cycles for d=2, 3, ... base-b digits
20, 0, 9, 21, {(45), (49)}, ...
30, 0, (32, 52), 184, (320, 580, 484), ...
40, 30, {201, (126, 138)}, (570, 765), {(2550), (3369), (3873)}, ...
58, (48, 72), 392, (1992, 2616, 2856, 2232), (7488, 10712, 9992, 13736, 11432), ...
60, 105, (430, 890, 920, 675, 860, 705), {5600, (4305, 5180)}, {(27195), (33860), (42925), (16840, 42745, 35510)}, ...
70, (144, 192), (1068, 1752, 1836), (9936, 15072, 13680, 13008, 10608), (55500, 89112, 91800, 72012, 91212, 77388), ...
821, 252, {(1589, 3178, 2723), (1022, 3122, 3290, 2044, 2212)}, {(17892, 20475), (21483, 25578, 26586, 21987)}, ...
9(16, 48), (320, 400), {(2256, 5312, 3856), (3712, 5168, 5456)}, {41520, (34960, 40080, 55360, 49520, 42240)}, ...
100, 495, 6174, {(53955, 59994), (61974, 82962, 75933, 63954), (62964, 71973, 83952, 74943)}, ...

For four digits in an even base b, Galyavin (2026ab) classified the cycles when leading zeros are retained at every iteration. The initial strings with all four digits equal are excluded. There is exactly one nonzero cycle precisely for the bases 3·2^(2k+1) and 5·2^(2k+1) with k>=0, and 7·2^j with j>=1 not divisible by 3. Their least periods are 6(k+1), 1, and 3, respectively. The decimal case belongs to the second family, with fixed point 6174. In base 2, padding gives two nonzero fixed points, 7 and 9 in decimal notation. The table above uses the convention that drops the leading zeros, leaving only 9 in the four-digit column for base 2. The complete classification and the single-cycle criterion are recorded separately in the Palomar Registry as Lean formalizations.

KaprekarRoutine

The figure above (similar to that appearing on the cover of the above issue of The Mathematics Teacher) shows the number of steps required for the Kaprekar routine to reach a fixed point for values of n=0 to 9999, partitioned into rows of length 100 (Deutsch and Goldman 2004). In this plot, numbers having fewer than four digits are padded with leading 0s. The ten values with four identical digits reach 0, while all other values reach 6174.


See also

196-Algorithm, Kaprekar Number, Kaprekar's Constant, RATS Sequence

Explore with Wolfram|Alpha

References

Deutsch, D. and Goldman, B. "Kaprekar's Constant." Math. Teacher 98, 234-242, 2004. https://doi.org/10.5951/mt.98.4.0234.Eldridge, K. E. and Sagong, S. "The Determination of Kaprekar Convergence and Loop Convergence of All 3-Digit Numbers." Amer. Math. Monthly 95, 105-112, 1988.Galyavin, R. "Even Bases with Exactly One Four-Digit Kaprekar Cycle." Palomar Registry, Nov. 21, 2026a. https://palomar-registry.org/entry.html?id=PALOMAR-2026-11-21-000001&version=1.Galyavin, R. "Cycles of the Four-Digit Kaprekar Map in Even Bases." Palomar Registry, Nov. 21, 2026b. https://palomar-registry.org/entry.html?id=PALOMAR-2026-11-21-000006&version=1.Kaprekar, D. R. "An Interesting Property of the Number 6174." Scripta Math. 15, 244-245, 1955.Nishiyama, Y. "Mysterious Number 6174." Plus. Mar. 1, 2006. https://plus.maths.org/mysterious-number-6174?page=0.Numberphile. "6174." Featuring R. Bowley; video by B. Haran. Dec. 5, 2011. https://www.youtube.com/watch?v=d8TRcZklX_Q.Sloane, N. J. A. Sequences A069746,A090429, A099009, and A099010 in "The On-Line Encyclopedia of Integer Sequences."Trigg, C. W. "All Three-Digit Integers Lead to ...." The Math. Teacher, 67, 41-45, 1974.Young, A. L. "A Variation on the 2-Digit Kaprekar Routine." Fibonacci Quart. 31, 138-145, 1993.

Referenced on Wolfram|Alpha

Kaprekar Routine

Cite this as:

Weisstein, Eric W. "Kaprekar Routine." From MathWorld--A Wolfram Resource. https://mathworld.wolfram.com/KaprekarRoutine.html

Subject classifications