Kaprekar Routine

DOWNLOAD Mathematica Notebook

The Kaprekar routine is an algorithm discovered in 1949 by D. R. Kaprekar for 4-digit numbers, but which can be generalized 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 in k and the value of 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 confusing with the distinct sort of Kaprekar number.

In base-10, the numbers n for which K(n)=n are given by 495, 6174, 549945, 631764, ... (OEIS A099009). Similarly, the numbers n for which iterating K(n) gives a cycle of length k>=2 are given by 53955, 59994, 61974, 62964, 63954, 71973, ... (OEIS A099010).

Iterating the Kaprekar map in base-10, all 1- and 2-digit numbers give 0. Exactly 60 3-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 6 iterations. Exactly 77 4-digit numbers, namely 1000, 1011, 1101, 1110, 1111, 1112, 1121, 1211, ... (OEIS A069746), reach 0, while the remainder give 6174 in at most 8 iterations. The value 6174 is sometimes known as Kaprekar's constant (Deutsch and Goldman 2004). This pattern breaks down for 5-digit numbers, which may converge to 0 or one of the 10 constants 53955, 59994, 61974, 62964, 63954, 71973, 74943, 75933, 82962, 83952.

The following table summarizes the possible cycles in various bases b and the first few numbers of digits.

bpossible cycles for d=1, 2, ... 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)}, ...
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 4 digits are padded with leading 0s, thus resulting in all values converging to 6174.

Wolfram Web Resources

Mathematica »

The #1 tool for creating Demonstrations and anything technical.

Wolfram|Alpha »

Explore anything with the first computational knowledge engine.

Wolfram Demonstrations Project »

Explore thousands of free applications across science, mathematics, engineering, technology, business, art, finance, social sciences, and more.

Computerbasedmath.org »

Join the initiative for modernizing math education.

Online Integral Calculator »

Solve integrals with Wolfram|Alpha.

Step-by-step Solutions »

Walk through homework problems step-by-step from beginning to end. Hints help you try the next step on your own.

Wolfram Problem Generator »

Unlimited random practice problems and answers with built-in Step-by-step solutions. Practice online or make a printable study sheet.

Wolfram Education Portal »

Collection of teaching and learning tools built by Wolfram education experts: dynamic textbook, lesson plans, widgets, interactive Demonstrations, and more.

Wolfram Language »

Knowledge-based programming for everyone.