How Kaprekar’s Routine Works

Kaprekar’s routine is one of the simplest algorithms in recreational mathematics, and one of the most surprising. Given almost any four-digit number, the routine always arrives at the same destination: 6174. Here is how it works.

The Four Steps

The routine is a loop. Each iteration performs four actions:

  1. Choose a four-digit number with at least two different digits. Leading zeros are allowed, so numbers like 0378 and 1000 are valid starting points.
  2. Rearrange the digits in descending order to form the largest possible four-digit number.
  3. Rearrange the digits in ascending order to form the smallest possible four-digit number, padding with leading zeros if needed to keep four digits.
  4. Subtract the smaller from the larger. Take the result and repeat from step 2.

Keep looping. You will reach 6174 within seven iterations. Once you arrive, the routine is stable: 7641 − 1467 = 6174, so the number produces itself and the loop becomes permanent.

A Worked Example: 1459

Start with the number 1459.

Iteration 1: 9541 − 1459 = 8082
Iteration 2: 8820 − 0288 = 8532
Iteration 3: 8532 − 2358 = 6174 ✓

Three iterations and we have arrived. Try it yourself with the solver on the homepage or see more worked examples.

Why Seven Steps?

Mathematicians have proven that no four-digit starting number (with at least two different digits) requires more than seven iterations to reach 6174. The number 9831 is one famous example that takes the full seven. For most starting numbers you will get there in three or four.

This upper bound is not a coincidence. It is a consequence of the fact that 6174 is the only four-digit fixed point of Kaprekar’s routine, and the routine’s structure forces rapid convergence. The mathematics page explains why in more detail.

What Numbers Don’t Work?

The only four-digit numbers that fail are repdigits — numbers where all four digits are the same, like 1111, 2222, or 9999. For these, the ascending and descending arrangements are identical, so subtracting them gives 0000 immediately and the routine collapses.

Every other four-digit number reaches 6174. That includes edge cases with lots of zeros, such as 1000, which becomes 1000 − 0001 = 0999, and then continues through the routine normally.

Try It Yourself

Head back to the solver and enter any four-digit number. You will watch it converge, step by step, in real time.

← Back to the solver