Tool to explore and visualize Kaprekar's algorithm, a fascinating mathematical routine involving sorting and subtracting numbers in a particular order to reveal constants like 6174 or 495.
Kaprekar Algorithm - dCode
Tag(s) : Arithmetics, Algorithm
dCode is free and its tools are a valuable help in games, maths, geocaching, puzzles and problems to solve every day!
A suggestion ? a feedback ? a bug ? an idea ? Write to dCode!
Kaprekar's algorithm is a mathematical procedure that involves taking an integer $ N $, rearranging its digits in descending and ascending order, and then subtracting the two resulting numbers.
Repeating this operation (also called routine) often results in a constant or cycle.
— Take an integer $ N $.
— Arrange its digits in descending order to form the largest possible number $ N_1 $ (Sorting from 9 to 0)
— Arrange its digits in ascending order to form the smallest possible number $ N_2 $ (Sorting from 0 to 9)
— Calculate the difference $ N_1 - N_2 $
— Repeat the operation with the result obtained until you reach a number already obtained (a constant or a cycle).
Example: $ N = 7533 $, $ N_1 = 3357 $, $ N_2 = 7533 $, replace $ N $ with $ 7533 - 3357 = 4176 $
$ N = 4176 $, $ N_1 = 1467 $, $ N_2 = 7641 $ then replace $ N $ with $ 7641 - 1467 = 6174 $
$ N = 6174 $, $ N_1 = 1467 $, $ N_2 = 7641 $ replace $ N $ with $ 7641 - 1467 = 6174 $, which creates an infinite loop on the constant 6174, which is the Kaprekar constant for 4 digits.
The size of the number must remain constant. Add leading 0s if necessary.
A Kaprekar constant is a number at which the algorithm eventually converges and no longer changes.
A Kaprekar cycle is a sequence of numbers that repeats indefinitely after a certain number of iterations.
The loops of the algorithm are a function of the size of the number $ N $.
Number of digits | Constant/Loop/Cycle |
---|---|
3 | 495 |
4 | 6174 |
5 | 53955, 59994 or 62964, 71973, 83952, 74943 or 61974, 82962, 75933, 63954 |
6 | 420876, 851742, 750843, 840852, 860832, 862632, 642654 or 631764 or 549945 |
7 | 7509843, 9529641, 8719722, 8649432, 7519743, 8429652, 7619733, 8439552 |
8 | 43208766, 85317642, 75308643, 84308652, 86308632, 86326632, 64326654 or 64308654, 83208762, 86526432 or 97508421 or 63317664 |
When one of these numbers is reached, either it remains constant or it follows the cycle by looping to infinity.
All these numbers are divisible by 9.
The mathematical proof of the existence of 6174 as the only fixed point number of the algorithm is a bit long and consists in enumerating a few possible cases and proving that only one case has no contradiction. The proof: here
Zeros do not change the algorithm. If they appear at the beginning of the number, they can be ignored.
Example: $ 1000 $ becomes $ 0001 $ (or simply $ 1 $), and the subtraction continues normally.
dCode retains ownership of the "Kaprekar Algorithm" source code. Any algorithm for the "Kaprekar Algorithm" algorithm, applet or snippet or script (converter, solver, encryption / decryption, encoding / decoding, ciphering / deciphering, breaker, translator), or any "Kaprekar Algorithm" functions (calculate, convert, solve, decrypt / encrypt, decipher / cipher, decode / encode, translate) written in any informatic language (Python, Java, PHP, C#, Javascript, Matlab, etc.) or any database download or API access for "Kaprekar Algorithm" or any other element are not public (except explicit open source licence like Creative Commons). Same with the download for offline use on PC, mobile, tablet, iPhone or Android app.
Reminder: dCode is an educational and teaching resource, accessible online for free and for everyone.
The content of the page "Kaprekar Algorithm" and its results may be freely copied and reused, including for commercial purposes, provided that dCode.fr is cited as the source.
Exporting the results is free and can be done simply by clicking on the export icons ⤓ (.csv or .txt format) or ⧉ (copy and paste).
To cite dCode.fr on another website, use the link:
In a scientific article or book, the recommended bibliographic citation is: Kaprekar Algorithm on dCode.fr [online website], retrieved on 2025-04-16,