Skip to calculator

Kaprekar Routine Analyzer

Explore the mathematics of digit rearrangement and discover fixed points and cycles in Kaprekar sequences.

Calculate Kaprekar Routine

How this calculator works

What is the Kaprekar routine?

The Kaprekar routine is an iterative digit process. Starting from a number with a fixed digit length, you repeatedly rearrange its digits to form two new numbers and subtract them. The transformation is simple, but the behavior is surprisingly rich: many starting values quickly settle into a stable value (a fixed point), while others fall into short repeating loops (cycles). This page is designed for hands-on experimentation, so you can see each iteration and learn what patterns are common.

In the classic 4-digit version, most starting values with at least two distinct digits eventually reach 6174, often called Kaprekar’s constant. The routine is a popular example in recreational mathematics because it demonstrates how deterministic rules can create “attractors” in a sequence.

Step-by-step procedure

One iteration (one “Kaprekar step”) follows the same recipe each time:

  1. Write the current value using a fixed number of digits (padding with leading zeros if needed).
  2. Sort the digits in descending order to form the largest possible number.
  3. Sort the digits in ascending order to form the smallest possible number (leading zeros allowed).
  4. Subtract: descending − ascending.
  5. Use the difference as the next value and repeat.

Formula and definitions

For a fixed digit length, define:

  • n: the current value written with a fixed number of digits (using leading zeros if needed),
  • D(n): digits of n sorted descending, interpreted as a number,
  • A(n): digits of n sorted ascending, interpreted as a number.

One Kaprekar step is:

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

Repeated application generates a sequence n, K(n), K(K(n)), … until it stabilizes (fixed point), repeats (cycle), or you stop after a chosen iteration limit.

Worked example (4 digits)

Start with 3524 (4 digits). The routine produces:

  1. 5432 − 2345 = 3087
  2. 8730 − 0378 = 8352
  3. 8532 − 2358 = 6174

Once you reach 6174, the next step returns 6174 again, so the routine has converged to a fixed point.

How to use the analyzer

  • Starting Number: enter an integer. The routine keeps a fixed digit length by padding with leading zeros when sorting digits.
  • Maximum Iterations: caps the number of steps to avoid long runs.
  • Show Full Sequence: displays the iteration table (descending, ascending, difference) for each step.

How to interpret results

  • Fixed point: the difference equals the previous value (the sequence stops changing).
  • Repdigit special case: if all digits are identical (e.g., 1111), then descending = ascending and the difference is 0 immediately.
  • Iteration cap reached: the tool stops after your maximum iteration count even if a cycle might exist beyond that point.

Important implementation note (digit length): This page accepts 4–7 digit inputs, but the current JavaScript implementation pads and processes values as 4 digits internally (it uses padStart(4, '0')). That means 5–7 digit inputs are accepted by the form, but the routine shown in the results is computed as a 4-digit Kaprekar routine. If you need true 5–7 digit analysis, the JavaScript logic would need to be extended to use the input’s digit length.

Practical takeaway: For the most accurate interpretation of the results table, use a 4-digit starting number (including numbers with leading zeros conceptually, such as 1000).

Typical behaviors (quick reference)

The outcomes below are common patterns you may observe when experimenting:

Typical Kaprekar routine outcomes by digit length and starting number type
Digit length & starting type Typical outcome Sequence behavior
4 digits, at least two distinct digits Often converges to 6174 Short transient, then fixed point at 6174
4 digits, all digits identical (e.g., 1111) Trivial fixed point at 0 First step yields 0000; remains 0000
Inputs with repeated digits May converge quickly Often stabilizes faster; some starts can loop
More digits (theory) More varied behavior Cycles and longer transients are more common

Assumptions and limitations

  • Integer inputs only: decimals and negatives are not supported.
  • Fixed digit count: digits are treated as a fixed-length string with leading zeros during sorting.
  • Iteration cap: results may be partial if the cap is reached.
  • Educational tool: it demonstrates behavior but does not prove convergence for all cases.

If you want to explore related tools, you can also browse more abstract mathematics calculators and compare how different digit transformations behave.

Input Parameters

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

Enter a number with 4 to 7 digits. When rearranging digits, the routine uses leading zeros to keep a fixed digit length.
Limit iterations to prevent infinite loops.

More background: Kaprekar routines in context

A recurrence-style view

You can also describe the routine as a recurrence. Let n0 be your starting value (written with a fixed digit length). Then define:

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

Convergence to a fixed point occurs when nk+1 = nk. A cycle occurs when some value repeats after multiple steps, for example nk = nk+m for some m > 1. In practice, the iteration table is the easiest way to spot these behaviors: a fixed point shows the same difference repeating, while a cycle shows a repeating block of differences.

Properties you can observe

Even without advanced theory, the iteration table reveals useful structure:

  • Digit sorting is deterministic: for a given padded value, the descending and ascending numbers are uniquely determined.
  • Zero-padding matters: treating 378 as 0378 changes the ascending/descending numbers and therefore changes the next difference.
  • Repdigits collapse: when all digits match, descending equals ascending, so the difference is 0 and stays 0.
  • Fast attraction (4 digits): many starts reach 6174 quickly, which makes it a classic classroom example of an attractor.
  • Iteration count is informative: the number of steps before stabilization (sometimes called the transient length) varies by starting value and digit pattern.

Kaprekar constants and related attractors

Different digit lengths can have different attractors. For example, 3-digit Kaprekar routines (with the usual fixed-length padding) famously lead to 495 for many starts, while 4-digit routines often lead to 6174. For other digit lengths, cycles can be more common than a single constant. Because this calculator’s current implementation uses 4-digit padding in its computation, the results panel is best interpreted as a 4-digit Kaprekar analyzer even if you type a longer number.

When you experiment, it helps to remember that “Kaprekar routine” can be defined in slightly different ways across sources (for example, whether you always fix the digit length, and how you treat leading zeros). This page uses the fixed-length, leading-zero approach because it makes the iteration well-defined and easy to tabulate.

Comparison table (common references)

Kaprekar constants and typical behavior by digit count
Digit Count Fixed Point/Constant Typical Iterations (often cited) Behavior
2 Cycle (e.g., 09 → 81 → 63 → 27 → 45 → 09) Varies Cycles are typical
3 495 Often within 6 Fixed point for many non-repdigit starts
4 6174 Often within 7 Fixed point for many non-repdigit starts
5+ Often cycles (varies by definition) Varies More complex; multiple attractors may exist

Special cases worth trying

If you enter a number like 1000, the ascending arrangement becomes 0001 and the descending arrangement becomes 1000, so the first difference is 999. With fixed-length padding, that becomes 0999 on the next step. These “leading zero” cases are a big part of why Kaprekar routines are interesting: the digit length is fixed, but the numeric value can shrink and grow as zeros move around.

Another interesting family is numbers with repeated digits but not all the same, such as 2111 or 9090. These often reach 6174 quickly, but the intermediate steps can look quite different because the ascending number may start with one or more zeros.

Practical tips for exploration

  • Turn on Show Full Sequence to see exactly how the descending/ascending numbers are formed.
  • Try a repdigit like 4444 to see the immediate collapse to 0.
  • Try values with zeros (e.g., 2005, 1000) to see how padding affects the next step.
  • Increase Maximum Iterations if you suspect a longer transient.
  • Compare two starts that share the same digits in a different order (for example 3524 and 4253). Because the routine sorts digits each step, many such starts quickly merge into the same trajectory.

Classroom and self-study ideas

If you are using this calculator for learning or teaching, here are a few structured activities that work well with the iteration table:

  • Convergence race: pick 10 random 4-digit starts (avoiding repdigits) and record how many steps each takes to reach 6174. Which digit patterns tend to converge fastest?
  • Zero experiment: compare starts with no zeros (e.g., 3524) to starts with one or more zeros (e.g., 2005, 1000). How do zeros change the ascending number and the early differences?
  • Fixed point verification: once you reach 6174, perform one more step by hand to confirm it maps to itself: 7641 − 1467 = 6174.
  • Repdigit proof sketch: explain why any repdigit must go to 0 in one step: the descending and ascending arrangements are identical, so their difference is 0.

These exercises help connect the calculator output to the underlying definitions: sorting digits, padding, and repeated application of the same transformation.

FAQ

Why does the calculator show “Digits (padded)”?

The Kaprekar routine is defined for a fixed digit length. Padding with leading zeros ensures each step uses the same number of digits when forming the ascending and descending numbers. For example, 378 is treated as 0378 in a 4-digit routine, which changes the sorted forms and therefore changes the next subtraction.

What does “converged” mean here?

In this tool, “converged” means the routine reached a value where the next difference equals the current value. In other words, the sequence has reached a fixed point. For many 4-digit starts, that fixed point is 6174. If the maximum iteration limit is reached first, the tool reports the last computed value instead.

Does this tool detect cycles longer than 1?

The current JavaScript checks for a fixed point by testing whether the new difference equals the previous value. It does not explicitly detect longer cycles (such as a 2-cycle). You can still spot cycles by enabling “Show Full Sequence” and looking for repeated patterns in the table.

Why are 5–7 digit inputs mentioned if the routine is computed as 4 digits?

The form accepts 4–7 digits, but the implementation note explains that the current computation uses 4-digit padding internally. The page keeps the broader description for context and future extension, but for now the results reflect the 4-digit routine. If you need true 5–7 digit behavior, the code would need to pad to the input’s digit length rather than always using 4.

Embed this calculator

Copy and paste the HTML below to add the Kaprekar Routine Analyzer (4–7 Digits) | Abstract Mathematics Calculator to your website.