Kaprekar Routine Analyzer

Explore how repeated digit sorting and subtraction can drive a number toward a fixed point such as 6174, collapse to 0, or reveal a repeating pattern.

Calculate Kaprekar Routine

How the Kaprekar routine turns digits into 6174

The Kaprekar routine is a small arithmetic loop with a surprisingly stubborn habit. Take a number, write it with a fixed count of digits, rearrange those digits into the largest number you can and the smallest number you can, and subtract the small one from the large one. Then do the same thing to the answer, and keep going. Each step is easy enough for the back of an envelope, yet the destinations stick in your memory: most four-digit starting values drift toward 6174, a value D. R. Kaprekar singled out in 1949 and which now carries his name. A handful of starts drop straight to zero, and other digit lengths settle into short repeating loops rather than one resting place.

This analyzer is built for watching that drift happen, not just for reporting where it ends. Switch on the full sequence and every row shows the descending number, the ascending number, and the difference, so you can check the arithmetic by eye and see exactly which step lands on the constant. The digit length is the detail that makes it all work. In a four-digit system, 378 behaves as 0378, and that leading zero reshuffles the ascending arrangement, changes the subtraction, and sends the whole chain down a different route — which is why the result panel always echoes the padded form of your starting number.

The subtraction rule behind each step

Fix a digit length and write the current value n with leading zeros where it needs them. Let D(n) be the number you get by sorting the digits from high to low, and A(n) the number you get by sorting the same digits from low to high. A single Kaprekar step is the difference between the two.

K(n) = D(n) A(n)

If you apply that transformation repeatedly, you generate a sequence of values. In symbols, you can think of the starting number as n0, then compute n1, n2, and so on by repeating the same digit-sorting subtraction. The sequence may stop changing, which means it has reached a fixed point, or it may revisit an earlier value and enter a cycle.

nk+1 = Ddesc (nk) Dasc (nk)

Although the notation looks formal, the idea is straightforward: sort high to low, sort low to high, subtract, and repeat. The calculator automates those steps and records the intermediate values so you can see exactly how the sequence evolves.

Walking 3524 down to 6174

Start with 3524 in the four-digit routine. Sorting high to low gives 5432; sorting low to high gives 2345; the difference is 3087. Feed 3087 back in: its descending form is 8730, its ascending form is 0378, and 8730 − 378 = 8352. One more pass gives 8532 − 2358 = 6174, and from there the routine is stuck, because 7641 − 1467 lands on 6174 again. Three subtractions, and the sequence has locked onto Kaprekar's constant.

That short run quietly makes two points. The zero in 3087 only matters because we hold four digits, so 0378 is a genuinely different arrangement from 378. And the endpoint is not luck: run the analyzer on 2071, 9831, or almost any other four-digit number whose digits are not all equal, and you will watch it funnel into 6174 within seven steps.

Filling in the form and reading the result

Three controls drive the analysis. Starting Number is the value you want to trace. Maximum Iterations caps how many subtraction steps run before the page gives up, so a stubborn or long input can never loop without end. Show Full Sequence switches on the step-by-step table; leave it off for a one-line summary, turn it on when you want to read the path row by row.

Once you press Analyze Routine, the panel reports your starting number, the padded digit string the routine actually uses, and the status. A status of converged means the latest difference matched the previous value, so the chain stopped moving — in the four-digit case that is usually 6174, and for an all-same-digit number it is 0. If the panel instead says the iteration limit was reached, the routine has not necessarily failed to settle; the analyzer simply stopped after the number of steps you allowed. Keep in mind the difference between a fixed point, which repeats after a single step the way 6174 does, and a longer cycle, which only returns after two or more steps. The surest way to catch a cycle is to scan the table for a block of values that starts repeating.

Important implementation note: the form accepts 4–7 digit inputs, and the preserved JavaScript uses padStart(4, '0'). That guarantees a minimum width of four digits, which matches the classic 6174 routine cleanly. Longer inputs are accepted too, but the script does not lock the sequence into a strict 5-, 6-, or 7-digit width on every later step.

Practical takeaway: use a 4-digit starting number when you want the textbook Kaprekar-constant interpretation. Use longer entries as exploratory inputs, understanding that the current implementation preserves the original script behavior rather than enforcing a separate fixed-width routine for each digit count above four.

What the analyzer assumes about your number

The tool works with non-negative whole numbers only — no decimals, no negatives. Sorting always happens on a padded, fixed-length digit string, which is why leading zeros count as part of the routine even though everyday decimal notation hides them. The iteration cap is a safety valve, not a mathematical claim: if the run stops because it hit that ceiling, there may still be a longer transient or a repeating loop waiting past the last row you see. Treat the page as a lab bench for the routine — a place to compare starting values and confirm examples by hand — rather than a prover of convergence theorems for every digit length. If you want to keep poking at iterative number games after this one, you can also browse more abstract mathematics calculators.

Why such a simple rule produces an attractor

Nothing in a Kaprekar step is advanced — you sort digits and subtract — yet stacking the step on itself produces the kind of behavior usually associated with much heavier machinery: a value that pulls its neighbors toward it, an instant collapse for special inputs, and honest cycles at some digit lengths. That is exactly why the routine turns up in classrooms and puzzle columns. It is a hands-on first taste of iteration, invariants, and attractors, with every step open to inspection. When the table prints 8730 − 0378 = 8352, you can confirm it on sight, and that transparency is the point: learners take away as much from how the routine arrives as from where it lands, because the intermediate rows expose the role of repeated digits, zeros, and digit order.

A few habits of the routine reward direct attention. Numbers with all identical digits, like 4444 or 7777, collapse to zero at once, since their largest and smallest arrangements are the same number. Numbers carrying zeros throw off dramatic middle rows, because the ascending form may open with one or more zeros and swing the subtraction hard. And since the routine only cares about which digits are present, not their original order, two starts built from the same digits — 3524 and 4253, say — merge after the very first sort and share a path from then on. The whole process is deterministic, so the moment two runs land on a common value their futures are identical. Good things to try: a repdigit for the instant zero, a zero-heavy value like 1000 or 2005 to watch padding bite, and a low iteration cap against a high one to tell a brief transient apart from a settled fixed point. A nice classroom follow-up is to check the fixed point by hand — 7641 − 1467 = 6174 — or to argue why a repdigit must fall to 0 in a single step.

Reference outcomes by digit count

Kaprekar constants and typical behavior by digit count
Digit Count Common Reference Outcome Typical Behavior
2 Short cycles are common Repeated looping rather than a single famous constant
3 495 Many non-repdigit starts converge to a fixed point
4 6174 Many non-repdigit starts converge quickly
5 and above Behavior varies by definition Cycles and multiple attractors become more common

Read the table as context, not as a promise about every implementation. Sources define the routine slightly differently, mostly in how they fix the digit length and handle leading zeros. This page uses the fixed-length, leading-zero convention whenever padding matters, because it makes each step unambiguous — the same choice most classroom treatments make for the four-digit case.

Questions people ask about 6174 and the routine

Why does the result show a padded "Digits" value?

The routine is defined on a fixed digit length, so padding with leading zeros keeps every step working with the same number of digits when it forms the ascending and descending numbers. In a four-digit run, 378 becomes 0378, and that single leading zero changes the subtraction and the whole path that follows.

What exactly does "converged" mean on this page?

It means the freshly computed difference matched the value before it, so the sequence has stopped changing. For most four-digit starts that fixed point is 6174; for an all-same-digit start it is 0. If the iteration limit is reached first, the panel reports the last value it computed instead.

Will it flag a cycle longer than one step, and what about 5–7 digit inputs?

The script explicitly tests for a one-step fixed point, not for longer loops, so if you suspect a cycle of length two or more, read the full sequence table and look for a repeating block. The form accepts 4–7 digit entries and will process them, but the code only guarantees a minimum width of four digits rather than enforcing a separate fixed-width system per length — so four-digit starts are the clean match for the classic routine, and longer ones are best treated as experiments.

Input Parameters

All fields are required unless marked optional. Results will appear after you activate Analyze Routine.

Enter a whole number with 4 to 7 digits. Four-digit inputs are the best match for the classic 6174 version of the routine, while longer inputs are available for experimentation.
Choose how many subtraction steps the calculator may perform before stopping.

Mini-game: Kaprekar Sequence Sort

Want to feel the routine instead of only reading it? This optional arcade mini-game turns the same digit-sorting idea into a fast reflex puzzle. Each round shows a 4-digit seed in the middle of the board. Your job is to tap the moving digit orbs in descending order first to build the large number, then in ascending order to build the small number. When both builds are complete, the game fires the subtraction, advances the sequence, and rewards you for speed, accuracy, and streak control. Wrong taps cost time, later waves add decoys, and reaching 6174 or 0000 gives special bonuses tied to the mathematics on the page.

Score0
Time75.0s
Streak0
Round1
Best0
Your browser does not support canvas. The calculator above still works, and you can analyze Kaprekar sequences without the mini-game.

Optional challenge

Click to play

Build the descending number first, then the ascending number, using the digits from the seed shown on the board. Reach 6174 or 0000 for bonuses. Wrong taps cost time. Pointer or touch works best, and number keys also collect the matching next digit.

Goal: score as many clean Kaprekar steps as you can before the timer runs out.

Current seed: 3524

Phase: Descending build

Build:

Tip: the classic 4-digit routine uses leading zeros, so a seed like 3087 becomes 8730 − 0378. The mini-game uses that same idea and rewards you for noticing repeated digits and zero-heavy patterns quickly.

Educational takeaway: each completed round performs the same step as the calculator, D(n) − A(n), where D sorts digits high to low and A sorts them low to high.

Embed this calculator

Copy and paste the HTML below to add the Kaprekar Routine Analyzer (4–7 Digit Inputs) | Kaprekar Sequence Calculator to your website.