# Kaprekar's routine

Jump to navigation Jump to search

Kaprekar's routine is an algorithm in recreational mathematics devised by the Indian mathematician D. R. Kaprekar which produces a sequence of numbers which either converges to a constant value or results in a repeating cycle. The algorithm is as follows:

1. Choose any natural number n in some base (usually base 10). This is the first number of the sequence.
2. Create a new number n′ by arranging the digits of n in descending order, and another new number n″ by arranging the digits of n in ascending order. These numbers may have leading zeros, which can be discarded (or alternatively, retained). Subtract n′n″ to produce the next number of the sequence.
3. Repeat step 2.

For example, if we start with 3524 then we get:

5432 − 2345 = 3087
8730 − 378 = 8352
8532 − 2358 = 6174
7641 − 1467 = 6174 (this value repeats indefinitely)

A sequence like this is sometimes called a Kaprekar sequence and the function K(n) = n′n″ is the Kaprekar mapping.

Some numbers map to themselves: for instance if n = 0, then 0 − 0 = 0; if n = 495, then 954 − 459 = 495; if n = 6174, then 7641 − 1467 = 6174. These are the fixed points of the Kaprekar mapping. If the sequence reaches one of these numbers, it will repeat indefinitely.

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.

Note that the numbers n′ and n″ have the same digit sum and hence the same remainder modulo 9 (if we are using base 10). Therefore, each number in a Kaprekar sequence of base 10 numbers (other than possibly the first) is a multiple of 9. In the general case of base b, the numbers in the sequences will be multiples of b − 1.

## Numbers of length four digits

In 1949 D. R. Kaprekar discovered that if the above process is applied to base 10 numbers of 4 digits, the resulting sequence will almost always converge to the value 6174 in at most 8 iterations, except for a small set of initial numbers which converge instead to 0. The number 6174 is sometimes known as Kaprekar's constant.

The set of numbers that converge to zero depends on whether leading zeros are discarded (the usual formulation) or are retained (as in Kaprekar's original formulation).

In the usual formulation, there are 77 four-digit numbers that converge to zero, for example 2111. However, in Kaprekar's original formulation the leading zeros are retained, and only repdigits such as 1111 or 2222 map to zero. This contrast is illustrated below:

discard leading zeros retain leading zeros

2111 − 1112 = 999

```999 −  999 = 0
```

2111 − 1112 = 0999
9990 − 0999 = 8991
9981 − 1899 = 8082
8820 − 0288 = 8532
8532 − 2358 = 6174

Below is a flowchart. Leading zeros are retained, however the only difference when leading zeros are discarded is that instead of 0999 connecting to 8991, we get 999 connecting to 0.

## Numbers of length three digits

If the Kaprekar routine is applied to numbers of 3 digits in base 10, the resulting sequence will almost always converge to the value 495 in at most 6 iterations, except for a small set of initial numbers which converge instead to 0.

The set of numbers that converge to zero depends on whether leading zeros are discarded (the usual formulation) or are retained (as in Kaprekar's original formulation). In the usual formulation, there are 60 three-digit numbers that converge to zero, for example 211. However, in Kaprekar's original formulation the leading zeros are retained, and only repdigits such as 111 or 222 map to zero.

Below is a flowchart. Leading zeros are retained, however the only difference when leading zeros are discarded is that instead of 099 connecting to 891, we get 99 connecting to 0.

## Other digit lengths

For digit lengths other than three or four (in base 10), the routine may terminate at one of several constant values or may enter one of several cycles instead, depending on the starting value of the sequence.

The following table is applicable when leading zeros are retained. If leading zeros are discarded, the only difference is that the 09→81→63→27→45 cycle for digit length 2 does not occur, because then 54 − 45 = 9 (instead of 09), and 9 − 9 = 0, so all 2-digit sequences will instead end in 0. When leading zeros are retained, only repdigits will result in 0.

 Digit length Cycles Cycle lengths Number of cycles 1 {0} 1 1 2 {00}, {09→81→63→27→45} 1, 5 2 3 {000}, {495} 1, 1 2 4 {0000}, {6174} 1, 1 2 5 {00000}, {53955→59994}, {61974→82962→75933→63954}, {62964→71973→83952→74943} 1, 2, 4, 4 4 6 {000000}, {420876→851742→750843→840852→860832→862632→642654}, {549945}, {631764} 1, 7, 1, 1 4 7 {0000000}, {7509843→9529641→8719722→8649432→7519743→8429652→7619733→8439552} 1, 8 2 8 {00000000}, {43208766→85317642→75308643→84308652→86308632→86326632→64326654}, {63317664}, {64308654→83208762→86526432}, {97508421} 1, 7, 1, 3, 1 5

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 in base 10. Other such "families" also exist, for instance 495, 549945, 554999445, 5(n)499(n)4(n)5, where d(n) means n repetitions of the digit "d".

The number of cycles increases rapidly with larger digit lengths, and all but a small handful of these cycles are of length three. For example, for 20-digit numbers in base 10, there are fourteen constants (cycles of length one) and ninety-six cycles of length greater than one, all but two of which are of length three. Odd digit lengths produce fewer different end results than even digit lengths.

## Other bases

Kaprekar sequences can also be calculated in bases other than 10.

### Hexadecimal

For instance, the table below is applicable to hexadecimal, and leading zeros are retained. If leading zeros are discarded, the only difference is that the 0f→e1→c3→87 cycle for digit length 2 does not occur, because then 87 − 78 = f (instead of 0f), and f − f = 0, so all 2-digit sequences will instead end in 0. When leading zeros are retained, only repdigits will result in 0.

Note that each number in a Kaprekar sequence in base 16 is a multiple of F (decimal 15 = 16−1).

 Digit length Cycles Cycle lengths Number of cycles 1 {0} 1 1 2 {00}, {0f→e1→c3→87}, {2d→a5→4b→69} 1, 4, 4 3 3 {000}, {7f8} 1, 1 2 4 {0000}, {3ffc→c2c4→a776}, {a596→52cb}, {e0e2→eb32→c774→7ff8→8688→1ffe}, {e952→c3b4→9687→30ed} 1, 3, 2, 6, 4 5 5 {00000}, {86f88→8fff7}, {a3fb6→c4fa4→b7f75}, {c5f94→b6f85→a4fa6→b3fb5} 1, 2, 3, 4 4 6 {000000}, {310eed→ed9522→cb3b44→976887}, {840eb8→e6ff82→d95963→a42cb6→a73b86}, {87ff78}, {a95966→532ccb}, {e40eb2→ec6832→c91d64→c82c74→a80e76}, {e82c72→ca0e54→e84a72→c60e94} 1, 4, 5, 1, 2, 5, 4 7 7 {0000000}, {b62fc95→d74fa83→c92fc64→d85f973→c81fd74→e94fa62→da3fb53→ca5f954→b74fa85}, {c63fb94→c84fa74→b82fc75→d73fb83→ca3fb54→c85f974→b71fd85→e83fb72→db3fb43→ca6f854→b73fb85}, {c83fb74} 1, 9, 11, 1 4 8 {00000000}, {3110eeed→edd95222→cbb3b444→97768887}, {5332cccb→a9959666}, {b974a865→7530eca9→e951da62→db52ca43}, {ca30ec54→e984a762→c630ec94→e982c762}, {cba0e544→e874a872→c610ee94→ed82c722}, {e852ca72→ca50ea54→e854aa72→c650ea94}, {e872c872→ca10ee54→ed84a722→cb60e944}, {ec94a632→c962c964→a832cc76→a940eb66→e742cb82→ca70e854→e850ea72→ec50ea32} 1, 4, 2, 4, 4, 4, 4, 4, 8 9