Computer Science

Kaprekar Four-Digit Attractor

Sort four digits in opposite orders and subtract: thousands of starting numbers fall into the same 6174 attractor.

By Riano · Published · Updated · Report a correction

Kaprekar Four-Digit Attractor

The Kaprekar routine for four-digit numbers is a finite dynamical system where every initial number, except those with four identical digits, eventually reaches the attractor 6174 (Kaprekar's constant). The routine involves sorting the digits of the number in descending and ascending order, and subtracting the smaller from the larger, padding with leading zeros to maintain exactly four digits.

Worked Example

Let's trace the Kaprekar routine starting with the four-digit number 3524.

  1. Start with the initial digits 3524. Sort them in descending order to get 5432, and ascending order to get 2345.
  2. Subtract the ascending number from the descending number: 5432 - 2345 = 3087.
  3. Now, take the result 3087. Sort descending to get 8730, and ascending to get 0378 (note the leading zero is retained).
  4. Subtract: 8730 - 0378 = 8352.
  5. Take the result 8352. Sort descending to get 8532, and ascending to get 2358.
  6. Subtract: 8532 - 2358 = 6174.
  7. Since we reached 6174, the process stops. The number of iterations to reach the attractor is 3. If we continued, 7641 - 1467 would just give 6174 again.

Experiment

In the interactive module, follow these steps to see the Kaprekar routine in action:

  1. Select a string from the "Initial digits" control to pick a starting number.
  2. Select a number from the "Inspection step" control to view the subtraction at a particular iteration.
  3. Observe the "Current four digits" text to trace the trajectory.

Limitations

This bounded decimal theorem is specific to exactly four-digit numbers in base 10. The result does not claim that a single universal attractor exists for every base or every digit length. For example, three-digit numbers reach 495, but other lengths can enter larger cycles rather than a single fixed point. Furthermore, numbers with all identical digits (like 1111) reach 0000 in one step and stay there, which is a separate trivial attractor.

References

The original observation of the 6174 attractor was published by D. R. Kaprekar.

Experience it interactively

Adjust parameters, observe in real time, and build deep intuition with Riano’s interactive Kaprekar Four-Digit Attractor module.

Try Kaprekar Four-Digit Attractor on Riano →

More in Computer Science