Lably
Skip to content

Kaprekar's routine

Iterative algorithm on numbers

  • 5 min read
  • 4 chapters
  • 1 lab on Lably

Learn it on Lably

As an example, starting with the number 8991 in base 10:

9981 – 1899 = 8082
8820 – 288 = 8532
8532 – 2358 = 6174
7641 – 1467 = 6174

6174, known as Kaprekar's constant, is a fixed point of this algorithm. Any four-digit number (in base 10) with at least two distinct digits will reach 6174 within seven iterations. The algorithm runs on any natural number in any given number base.

Definition and properties

The algorithm is as follows:

  1. Choose any four-digit natural number n in a given number base b. This is the first number of the sequence.
  2. Create a new number α by sorting the digits of n in descending order, and another number β by sorting the digits of n in ascending order. These numbers may have leading zeros, which can be ignored. Subtract α−β to produce the next number of the sequence.
  3. Repeat step 2.

The sequence is called a Kaprekar sequence and the function Kb(n)=α−β is the Kaprekar mapping. Some numbers map to themselves; these are the fixed points of the Kaprekar mapping, and are called Kaprekar's constants. Zero is a Kaprekar's constant for all bases b, and so is called a trivial Kaprekar's constant. All other Kaprekar's constants are nontrivial Kaprekar's constants.

For example, in base 10, starting with 3524,

K10(3524)=5432−2345=3087
K10(3087)=8730−378=8352
K10(8352)=8532−2358=6174
K10(6174)=7641−1467=6174

with 6174 as a Kaprekar's constant.

All Kaprekar sequences will either reach one of these fixed points or will result in a repeating cycle. Either way, the end result is reached in a fairly small number of steps (within seven iterations or steps).

Note that the numbers α and β have the same digit sum and hence the same remainder modulo b−1. Therefore, each number in a Kaprekar sequence of base b numbers (other than possibly the first) is a multiple of b−1.

When leading zeroes are retained, only repdigits lead to the trivial Kaprekar's constant.

In base 4, it can easily be shown that all numbers of the form 3021, 310221, 31102221, 3...111...02...222...1 (where the length of the "1" sequence and the length of the "2" sequence are the same) are fixed points of the Kaprekar mapping.

In base 10, it can easily be shown that all numbers of the form 6174, 631764, 63317664, 6...333...17...666...4 (where the length of the "3" sequence and the length of the "6" sequence are the same) are fixed points of the Kaprekar mapping.

Sign in to keep reading

3 more chapters are free with a Lably account.

More in Mathematics