Search for a tool
Kaprekar Algorithm

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.

Results

Kaprekar Algorithm -

Tag(s) : Arithmetics, Algorithm

Share
Share
dCode and more

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!


Please, check our dCode Discord community for help requests!
NB: for encrypted messages, test our automatic cipher identifier!


Feedback and suggestions are welcome so that dCode offers the best 'Kaprekar Algorithm' tool for free! Thank you!

Kaprekar Algorithm

Calculus through Kaprekar Algorithm


Answers to Questions (FAQ)

What is Kaprekar's algorithm? (Definition)

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.

How to calculate Kaprekar sequence?

— 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.

What are Kaprekar constants and Kaprekar loops?

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 digitsConstant/Loop/Cycle
3495
46174
553955, 59994 or 62964, 71973, 83952, 74943 or 61974, 82962, 75933, 63954
6420876, 851742, 750843, 840852, 860832, 862632, 642654 or 631764 or 549945
77509843, 9529641, 8719722, 8649432, 7519743, 8429652, 7619733, 8439552
843208766, 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.

How to prove that 6174 is the only 4-digit Kaprekar constant?

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

What happens to numbers containing zeros?

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.

Source code

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.

Cite dCode

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: https://www.dcode.fr/kaprekar-algorithm
In a scientific article or book, the recommended bibliographic citation is: Kaprekar Algorithm on dCode.fr [online website], retrieved on 2025-04-16, https://www.dcode.fr/kaprekar-algorithm

Need Help ?

Please, check our dCode Discord community for help requests!
NB: for encrypted messages, test our automatic cipher identifier!

Questions / Comments

Feedback and suggestions are welcome so that dCode offers the best 'Kaprekar Algorithm' tool for free! Thank you!


https://www.dcode.fr/kaprekar-algorithm
© 2025 dCode — The ultimate collection of tools for games, math, and puzzles.
 
Feedback