Kaprekar's_constant

Kaprekar's routine

Kaprekar's routine

Iterative algorithm


In number theory, Kaprekar's routine is an iterative algorithm named after its inventor, Indian mathematician D. R. Kaprekar. Each iteration starts with a number, sorts the digits into descending and ascending order, and calculates the difference between the two new numbers.

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

9981 – 1899 = 8082
8820 – 0288 = 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.[1] The algorithm runs on any natural number in any given number base.

Definition and properties

The algorithm is as follows:[2]

  1. Choose any natural number in a given number base . This is the first number of the sequence.
  2. Create a new number by sorting the digits of in descending order, and another number by sorting the digits of 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 is the Kaprekar mapping. Some numbers map to themselves; these are the fixed points of the Kaprekar mapping,[3] and are called Kaprekar's constants. Zero is a Kaprekar's constant for all bases , and so is called a trivial Kaprekar's constant. All other Kaprekar's constant are nontrivial Kaprekar's constants.

For example, in base 10, starting with 3524,

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.

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

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

Families of Kaprekar's constants

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.

b = 2k

It can be shown that all natural numbers

are fixed points of the Kaprekar mapping in even base b = 2k for all natural numbers n.

Proof

More information k, b ...

Kaprekar's constants and cycles of the Kaprekar mapping for specific base b

All numbers are expressed in base b, using A−Z to represent digit values 10 to 35.

More information Base b, Digit length ...
  1. Leading zeroes retained.

Kaprekar's constants in base 10

Numbers of length four digits

In 1949 D. R. Kaprekar discovered[6] that if the above process is applied to four-digit numbers in base 10, the sequence converges to 6174 within seven iterations or, more rarely, converges to 0. The number 6174 is the first Kaprekar's constant to be discovered, and thus is sometimes known as Kaprekar's constant.[7][8][9]

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,[10] 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:

More information discard leading zeros, retain leading zeros ...

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.

Sequence of Kaprekar transformations ending in 6174

Numbers of length three digits

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

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,[11] 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.

Sequence of three digit Kaprekar transformations ending in 495

Other digit lengths

For digit lengths other than three or four (in base 10), the routine may terminate at one of several fixed points or may enter one of several cycles instead, depending on the starting value of the sequence.[7] See the table in the section above for base 10 fixed points and cycles.

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.[12][13]

Sometimes these numbers (495, 6174, and their counterparts in other digit lengths or in bases other than 10) are called "Peyush constants" named after Peyush Dixit who solved this routine as a part of his IMO 2000 (International Mathematical Olympiad, Year 2000) thesis. [14]

Programming example

The example below implements the Kaprekar mapping described in the definition above to search for Kaprekar's constants and cycles in Python.

import string
from collections import deque
from collections.abc import Sequence, Generator


BASE36_DIGITS = f"{string.digits}{string.ascii_uppercase}"


def digit_count(x: int, /, base: int = 10) -> int:
    count = 0
    while x > 0:
        count += 1
        x //= base
    return count


def get_digits(x: int, /, base: int = 10, init_k: int = 0) -> str:
    if init_k > 0:
        k = digit_count(x, base)
    digits = deque()
    while x > 0:
        digits.appendleft(BASE36_DIGITS[x % base])
        x //= base
    if init_k > 0:
        for _ in range(k, init_k):
            digits.appendleft("0")
    return "".join(digits)


def kaprekar_map(x: int, /, base: int = 10, init_k: int = 0) -> int:
    digits = "".join(sorted(get_digits(x, base, init_k)))
    descending = int("".join(reversed(digits)), base)
    ascending = int(digits, base)
    return descending - ascending


def kaprekar_cycle(
    x: int | str | bytes | bytearray, /, base: int = 10
) -> list[int | str]:
    """
    Return Kaprekar's cycles as list

    >>> kaprekar_cycle(8991)
    [6174]
    >>> kaprekar_cycle(865296432)
    [865296432, 763197633, 844296552, 762098733, 964395531, 863098632, 965296431, 873197622, 865395432, 753098643, 954197541, 883098612, 976494321, 874197522]
    >>> kaprekar_cycle('09')
    [9, 81, 63, 27, 45]
    >>> kaprekar_cycle('0F', 16)
    ['0F', 'E1', 'C3', '87']
    >>> kaprekar_cycle('B71FD85', 16)
    ['B71FD85', 'E83FB72', 'DB3FB43', 'CA6F854', 'B73FB85', 'C63FB94', 'C84FA74', 'B82FC75', 'D73FB83', 'CA3FB54', 'C85F974']

    """
    leading_zeroes_retained = not isinstance(x, int)
    init_k = len(x) if leading_zeroes_retained else 0
    x = int(x) if base == 10 else int(x, base)
    seen = []
    while x not in seen:
        seen.append(x)
        x = kaprekar_map(x, base, init_k)
    cycle = []
    while x not in cycle:
        cycle.append(x)
        x = kaprekar_map(x, base, init_k)
    return cycle if base == 10 else [get_digits(x, base, init_k) for x in cycle]


if __name__ == "__main__":
    import doctest

    doctest.testmod()

See also


Citations

  1. Hanover 2017, p. 1, Overview.
  2. Hanover 2017, p. 3, Methodology.
  3. (sequence A099009 in the OEIS)
  4. "Sample Kaprekar Series".
  5. Kaprekar DR (1955). "An Interesting Property of the Number 6174". Scripta Mathematica. 15: 244–245.
  6. Kaprekar DR (1980). "On Kaprekar Numbers". Journal of Recreational Mathematics. 13 (2): 81–82.
  7. (sequence A069746 in the OEIS)
  8. (sequence A090429 in the OEIS)
  9. Hanover 2017, p. 14, Operations.

References

  • Hanover, Daniel (2017). "The Base Dependent Behavior of Kaprekar's Routine: A Theoretical and Computational Study Revealing New Regularities". International Journal of Pure and Applied Mathematics. arXiv:1710.06308.

Share this article:

This article uses material from the Wikipedia article Kaprekar's_constant, and is written by contributors. Text is available under a CC BY-SA 4.0 International License; additional terms may apply. Images, videos and audio are available under their respective licenses.