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
Introduction and how this calculator works
The Kaprekar routine is a classic number experiment in recreational mathematics. You begin with a whole number written using a fixed number of digits, rearrange those digits to make the largest and smallest possible numbers, subtract the smaller from the larger, and then repeat the same process with the result. The rule is simple enough to do by hand, but the outcomes are surprisingly memorable. Many 4-digit starting values quickly move toward 6174, a famous fixed point known as Kaprekar's constant. Other starting values collapse to 0 right away, and some digit-length variants can produce short cycles instead of a single stable destination.
This calculator is built to make that process easy to inspect. Instead of only showing the final answer, it can also display the full sequence of descending and ascending digit arrangements at each step. That makes it useful both as a quick calculator and as a teaching tool. If you are learning the routine for the first time, the explanation below walks through the meaning of each input, the formula behind the transformation, the assumptions used by the page, and the best way to interpret the result panel.
One important detail is that Kaprekar routines depend on a fixed digit length. A value such as 378 is not treated the same way as 0378 when you are working in a 4-digit system. The leading zero changes the ascending and descending arrangements, which changes the subtraction and therefore changes the entire path of the sequence. That is why the calculator reports the padded form of the starting number and why the explanation repeatedly refers to fixed-length digit strings rather than only to ordinary decimal values.
In the most familiar 4-digit version, the routine often behaves like a funnel. Different starting values may look unrelated at first, but after a few steps they merge into the same trajectory and settle at 6174. This is one reason the routine is so popular in classrooms and puzzle books: it gives a vivid example of how a deterministic rule can create an attractor. You always follow the same instructions, yet many different inputs are drawn toward the same endpoint.
Formula and definitions
For a fixed digit length, let n be the current value written with leading zeros when needed. Let D(n) be the number formed by sorting the digits of n in descending order, and let A(n) be the number formed by sorting the same digits in ascending order. One Kaprekar step is then defined by the subtraction below.
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.
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.
Worked example
Suppose you start with 3524 in the 4-digit routine. First sort the digits in descending order to get 5432. Then sort them in ascending order to get 2345. Subtracting gives 3087. Repeat the same process with 3087: descending order gives 8730, ascending order gives 0378, and the difference is 8352. One more step gives 8532 − 2358 = 6174. If you continue, 6174 maps back to itself, so the sequence has converged to a fixed point.
This example shows several ideas at once. It shows why leading zeros matter, because 0378 is not the same digit arrangement as 378 when you are preserving four digits. It also shows why the routine is interesting: the arithmetic is elementary, but the repeated transformation creates a recognizable destination. Many different 4-digit starts eventually pass through the same final stage.
How to use the analyzer
The form has three interactive controls. The Starting Number field is the value you want to analyze. The Maximum Iterations field limits how many steps the calculator will perform before stopping. This is useful because some routines can take several steps before settling, and a limit prevents the page from running indefinitely. The Show Full Sequence checkbox tells the calculator whether to display the detailed iteration table. If you only want a quick summary, leave it unchecked. If you want to study the path step by step, turn it on.
After you press Analyze Routine, the results area reports the starting number, the padded digit form used by the routine, and the current status. If the sequence reaches a fixed point under the page's logic, the result will say that it converged and will show the final value. If the iteration limit is reached first, the result will instead report the last computed value. When the full sequence option is enabled, the table lists the descending number, ascending number, and difference for each iteration so you can verify the arithmetic yourself.
How to interpret the result
A result labeled as converged means the newly computed difference matched the current value, so the sequence stopped changing. In the familiar 4-digit setting, that often means the routine reached 6174. A repdigit such as 1111 or 4444 is a special case: the descending and ascending arrangements are identical, so the subtraction is 0 immediately. Once the routine reaches 0, it stays there. If the page says the maximum iteration count was reached, that does not necessarily mean the routine has no pattern. It only means the calculator stopped after the number of steps you allowed.
It is also helpful to distinguish between a fixed point and a longer cycle. A fixed point repeats after one step, such as 6174 mapping to 6174 again. A longer cycle would revisit a previous value after two or more steps. The current script explicitly checks for the fixed-point case, while longer cycles are best spotted by reading the sequence table. If you see a repeating block of values, you have found a cycle even if the summary line does not name it directly.
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.
Assumptions and limitations
This calculator is intended for non-negative integers. It does not support decimals or negative values. It also assumes a fixed digit length during sorting whenever padding is needed, which is why leading zeros are conceptually part of the routine even though ordinary decimal notation usually hides them. The maximum-iteration setting is a safety limit rather than a mathematical statement. If the calculator stops because it reached that limit, the sequence may still have a longer transient or a repeating cycle beyond the displayed range.
The page is best used as an exploratory and educational tool. It helps you see what the routine does, compare starting values, and verify examples by hand. It does not attempt to prove general theorems about convergence for every digit length. Instead, it gives you a clear view of the mechanics: how the digits are rearranged, how the subtraction is formed, and how repeated application can create stable behavior from a very simple rule.
If you want to explore related topics after using this page, you can also browse more abstract mathematics calculators and compare how other iterative number processes behave.
More background: why Kaprekar routines are interesting
Kaprekar routines are a good example of how a very small rule can produce structure that feels larger than the rule itself. At each step, you are not doing anything advanced: you are only sorting digits and subtracting. Yet repeated application creates recognizable behavior such as attraction to a fixed point, immediate collapse in repdigit cases, and in some settings the appearance of cycles. This makes the routine a useful bridge between arithmetic practice and broader mathematical ideas such as iteration, dynamical systems, invariants, and attractors.
One reason the routine is so approachable is that every step is transparent. There is no hidden formula beyond the digit sorting itself. If the calculator shows a row like 8730 − 0378 = 8352, you can verify it instantly. That transparency makes the sequence table especially valuable. It lets you see not only where the routine ends up, but also how it gets there. For students, that often matters more than the final number because the intermediate rows reveal the role of repeated digits, zeros, and digit order.
Another interesting feature is that many starting values that look different at first become equivalent after one or two steps. If two numbers contain the same digits in a different order, the first sorting step wipes out that difference. For example, 3524 and 4253 quickly merge because the routine depends on the multiset of digits rather than on their original arrangement. This is a useful observation when you are experimenting: changing the order of the same digits often changes the first displayed value but not the long-term behavior.
Patterns you can observe in the table
The iteration table often reveals several recurring patterns. Numbers with all identical digits collapse immediately because the ascending and descending forms are the same. Numbers containing zeros can produce especially striking intermediate rows because the ascending arrangement may begin with one or more zeros, which greatly changes the subtraction. Many ordinary 4-digit non-repdigit starts reach 6174 in only a handful of steps, which is why the constant is so famous. The number of steps before stabilization can vary, and that variation itself is interesting because it shows that some digit patterns are drawn toward the attractor more quickly than others.
It is also worth noticing that the routine is deterministic. Once the padded digit string is fixed, the next value is forced. There is no randomness anywhere in the process. That means if two different experiments ever land on the same current value, their future paths will be identical from that point onward. This is why sequence tables from different starting numbers often merge after a few rows.
Common reference points 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 |
The table above is meant as context rather than as a promise about every implementation. Different sources define the routine slightly differently, especially when discussing how digit length is fixed and how leading zeros are handled. This page uses the fixed-length, leading-zero interpretation whenever padding matters because it makes each step unambiguous and easy to compute. That convention is standard in many educational treatments of Kaprekar routines, especially in the classic 4-digit case.
Special cases worth trying
If you want to get a feel for the routine, try a few contrasting starting values. A repdigit such as 4444 shows the immediate collapse to 0. A value with zeros, such as 1000 or 2005, shows how strongly leading zeros affect the ascending arrangement. A value like 3524 gives a classic path to 6174. You can also compare two numbers made from the same digits in a different order to see how quickly their paths merge. These experiments are simple, but they make the structure of the routine much easier to remember.
Another good experiment is to vary the maximum iteration count. If you set it very low, you may only see the early transient part of the sequence. If you raise it, you can confirm whether the path settles into a fixed point or begins to repeat. Even when the summary line is short, the detailed table can tell a richer story about how the digits are moving around from one step to the next.
Ideas for learning and teaching
This calculator works well in a classroom, tutoring session, or self-study setting because it supports both quick checking and deeper pattern hunting. One useful activity is to collect several random 4-digit starting values and record how many steps each one takes to reach 6174. Another is to compare numbers with and without zeros to see how padding changes the ascending number. You can also ask learners to predict the next row before revealing it, which turns the sequence table into a short reasoning exercise rather than just a display of answers.
A particularly effective exercise is to verify the fixed point by hand. Once the calculator reaches 6174, compute one more step yourself: 7641 − 1467 = 6174. That single check helps students understand what a fixed point means in concrete terms. Similarly, asking why a repdigit must go to 0 in one step encourages a short proof: if all digits are the same, then the ascending and descending arrangements are identical, so their difference is 0. These small arguments connect the calculator output to mathematical reasoning.
FAQ
Why does the calculator show Digits (padded)?
The Kaprekar routine is defined using a fixed digit length. Padding with leading zeros ensures that each step uses the same number of digits when forming the ascending and descending numbers. For example, 378 becomes 0378 in a 4-digit routine, and that changes the subtraction.
What does converged mean here?
On this page, converged means the newly computed difference matched the current value, so the sequence stopped changing. In many 4-digit examples, that fixed point is 6174. If the maximum iteration limit is reached first, the page reports the last computed value instead.
Does this tool detect cycles longer than 1?
The current JavaScript explicitly checks for a fixed point, not for every possible longer cycle. You can still inspect the full sequence table to look for repeated patterns if you suspect a loop of length 2 or more.
Why are 5–7 digit inputs mentioned if the routine is most standard at 4 digits?
The form accepts 4–7 digit entries, and the preserved script will process those values. However, the code guarantees a minimum width of four digits rather than enforcing a separate fixed-width system for every longer length. That means 4-digit starts are the clearest match for the classic Kaprekar routine, while longer starts are best treated as exploratory experiments with the page's current implementation.
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.
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.
