Definition:Kaprekar Mapping

Definition
The Kaprekar mapping is the arithmetic function $K: \Z_{>0} \to \Z_{>0}$ defined on the positive integers as follows:

Let $n \in \Z_{>0}$ be expressed in some number base $b$ (where $b$ is usually $10$).

Let $n'$ be the positive integer created by arranging the digits of $n$ into descending order of size.

Let $n''$ be the positive integer created by arranging the digits of $n$ into ascending order of size.

Then:
 * $K \left({n}\right) = n' - n''$

making sure to retain any leading zeroes to ensure that $K \left({n}\right)$ has the same number of digits as $n$.

Also defined as
Some sources do not retain the leading zeroes, and so, for example:
 * $K \left({1121}\right) = 2111 - 1112 = 999$

and so:
 * $K \left({999}\right) = 999 - 999 = 0$

instead of:
 * $K \left({1121}\right) = 2111 - 1112 = 0999$
 * $K \left({0999}\right) = 9990 - 0999 = 8991$

The mapping as initially specified does retain all leading [Definition:Zero Digit|zeroes]].

Also see

 * Definition:Kaprekar's Process