🔢

Kaprekar Constant Calculator

Discover the magic of Kaprekar's constant: enter any 4-digit number and watch it converge to 6174 step by step. Every iteration shown with full working — descending, ascending, and subtraction.

Kaprekar's Constant

Enter any 4-digit number (not all same digits) and watch every step of the journey to 6174.

How to Use the Kaprekar Constant Calculator

Type any 4-digit number with at least two different digits and the tool runs the Kaprekar routine step by step, sorting digits descending, sorting ascending, subtracting, and repeating until it reaches 6174. The journey takes at most seven iterations and the tool animates each step so you can follow the convergence in real time.

Students exploring recreational number theory

A maths teacher assigns a project on fixed-point algorithms. Enter 1234 and watch the sequence unfold: 4321 - 1234 = 3087, then 8730 - 0378 = 8352, then 8532 - 2358 = 6174. The result banner confirms convergence in three steps, and the progress bar tracks each iteration visually.

Maths bloggers researching convergence properties

You are writing a post about attractors in discrete dynamical systems and need to demonstrate that every non-repdigit 4-digit number converges to the same fixed point. Enter several starting values — 1000, 9876, 5551 — and record the step counts. The tool shows that 9876 takes exactly seven steps, which is the worst case.

Programmers benchmarking iterative algorithms

You are teaching a class on loop invariants and need a simple example of a function that always terminates. The Kaprekar routine is a perfect demonstration because it reaches a known fixed point in bounded steps. Use the Random button to generate a new starting number each time and show students that convergence is guaranteed.

Parents helping children with maths homework

A child brings home a worksheet asking what happens when you apply the Kaprekar routine to 2025. Enter the number and the tool produces the full chain: 5220 - 0225 = 4995, then 9954 - 4599 = 5355, then 5553 - 3555 = 1998, continuing until 6174. The step-by-step display makes it easy to copy into the homework.

How the Kaprekar Constant Calculator Works

The calculator implements the Kaprekar routine, an iterative subtraction algorithm that converges to the fixed point 6174 for every valid 4-digit starting number.

The Kaprekar Routine Algorithm

At each step the calculator takes the current 4-digit number, pads it with leading zeros if necessary, sorts its digits in descending order to form the largest number, sorts them in ascending order to form the smallest number, and subtracts the smaller from the larger. Starting with 3524, the digits sort descending as 5432 and ascending as 2345, giving 5432 - 2345 = 3087. The result becomes the input for the next iteration. This process repeats until the result is 6174, at which point 7641 - 1467 = 6174 forever.

Why Every Number Converges

Mathematicians proved through exhaustive enumeration of all 9000 possible 4-digit numbers that every non-repdigit input reaches 6174 in at most 7 iterations. The set of possible 4-digit numbers is finite, and the Kaprekar routine acts as a contraction mapping that reduces the state space at each step. No starting number escapes the attractor. The worst case is 7 steps, achieved by numbers like 1000 and 9876.

Leading Zero Preservation

Every intermediate result is padded to exactly 4 digits using leading zeros. This is critical because without it, a result like 999 would be treated as a 3-digit number and the digit sorting would fail. For example, starting from 1000 gives the subtraction 1000 - 0001 = 0999. The tool pads all results with padStart(4, 0) before each sort operation.

Repdigit Detection

Numbers where all four digits are identical — 0000, 1111, 2222, through 9999 — are excluded because they produce zero at the first step. Since zero has no valid 4-digit representation under the Kaprekar routine, the process cannot continue. The tool validates input by checking that at least two distinct digits exist before running the routine.

Frequently Asked Questions

What is Kaprekar's constant 6174?

6174 is the unique fixed point of the Kaprekar routine for 4-digit numbers. When you repeatedly sort a number's digits in descending and ascending order and subtract the smaller from the larger, you will always arrive at 6174. Once there, the process loops forever because 7641 minus 1467 equals 6174.

How many steps does it take to reach 6174?

No valid 4-digit number takes more than 7 iterations to reach 6174. Most numbers converge in 3 to 5 steps. For example, 1234 reaches 6174 in exactly 3 steps, while 1000 takes 7 steps — the worst case. The exact step count depends on the specific digits in the starting number, but convergence is guaranteed for all non-repdigit inputs.

Does this work for 3-digit or 5-digit numbers?

Yes, but the constants differ. For 3-digit numbers the Kaprekar constant is 495, which is reached in at most 6 steps. For 5-digit numbers there is no single constant; instead the routine enters one of several cycles such as 63954 minus 49536 equals 14418, continuing until it loops. The 4-digit case is special because it converges to exactly one fixed point.

Why are repdigits like 1111 excluded?

Repdigits (numbers where all digits are the same) produce zero at the first subtraction step. For example, 1111 minus 1111 equals 0, which has no valid 4-digit representation. Since the Kaprekar routine requires 4-digit numbers at every step, repdigits cannot participate in the process.

Who discovered the Kaprekar constant?

The Kaprekar routine and its constant 6174 were discovered by Indian mathematician D. R. Kaprekar in 1949. Kaprekar studied recreational number theory and published extensively on properties of digit manipulation, with this routine being one of his most celebrated findings.

Why does leading zeros matter in the calculation?

Leading zeros ensure every intermediate result stays exactly 4 digits long. For example, starting with 1000 produces the subtraction 1000 minus 0001, which equals 0999. Without preserving leading zeros, the digit sorting and subtraction would break down for numbers that produce results below 1000.

kaprekar6174mathematicsnumber theoryrecreational mathpuzzlealgorithm

More Tools You Might Like

πPi (π) Explorer: The Infinite Geometric Constant
📊Percentage Calculator
🔠Anagram & Word Puzzle Finder
⚖️BMI Calculator
📝Word Counter
🔑JWT Debugger