In 1949, at a mathematical conference in Pune, a self-taught schoolteacher named Dattatreya Ramchandra Kaprekar introduced a discovery that would eventually captivate the global mathematical community. The process, now known as the Kaprekar Routine, is simple: take any four-digit number (provided at least two digits are different). Rearrange the digits to form the largest and smallest possible numbers, then subtract the smaller from the larger. Repeat with the result — treating leading zeros as digits to maintain the four-digit length (e.g.,
8532 → 8532 - 2358 = 6174
6174 → 7641 - 1467 = 6174
Within seven iterations, the sequence always converges to 6174. Once reached, the routine enters a self-referential loop:
Kaprekar spent much of his career in the modest town of Devlali, publishing in local Indian journals without the backing of institutional academia. He wasn't entirely overlooked — Martin Gardner, the legendary mathematical communicator, wrote about Kaprekar's work in Scientific American in 1975, bringing it to global attention during his lifetime. And we can prove that 6174 is the unique fixed point through exhaustive computation: there are only finitely many four-digit numbers, and every one of them can be checked. But checking that it works is not the same as understanding why it works — why this particular number, why this particular base, why exactly seven steps and no more.
We decided to find out.
We started the way any explorer would — by looking at everything at once.
All 10,000 four-digit numbers (0000–9999) laid out on a grid, colored by how many steps each takes to reach 6174. Black for numbers like 1111 or 3333 (all digits the same) that collapse to zero. Blue for 6174 itself. Green through purple for 1 to 7 steps.
This approach — plotting every possible state of a system on a single map — was pioneered by the French mathematician Henri Poincaré in the 1880s. He was trying to predict how three planets pull on each other, and realized that tracking one orbit at a time was hopeless. But if you draw every possible orbit on one picture, hidden patterns jump out. Instead of asking "what does this number do?", you ask "what does the whole universe of numbers look like?"
Our grid was beautiful, but it was still just a picture. You could click a cell and watch the arithmetic play out — the sorting, the subtraction — but the reason was invisible. The pattern was clearly not random. Something was hiding inside the grid, and checking numbers one by one would never reveal it.
The first instinct was to reach for comparisons — whirlpools, magnets, gravity wells. But comparisons describe what something looks like, not what it is. The constraint was set early:
"We are here to discover why 6174 behaves like it does mathematically."
No comparisons. Only the actual arithmetic.
The breakthrough came from asking a deceptively simple question: what information does the subtraction throw away?
This is the same question Emmy Noether — one of the most important mathematicians of the 20th century, who had to lecture under a male colleague's name because women weren't allowed to hold academic positions in 1920s Germany — built her entire framework around. When you apply a rule to something, some information survives and some is permanently lost. The surviving part is the signal. The lost part is the noise. Understanding which is which tells you what the rule actually does.
Take any 4-digit number and sort its digits from biggest to smallest. Call them
Look at that formula. The actual digits are noise — the routine throws them away. The signal is just two numbers: the gap between the biggest and smallest digits (
Four digits collapse to two gaps. The routine is blind to everything else — the absolute size of the digits, their specific arrangement. It can only see the spread.
This means 10,000 numbers don't actually produce 10,000 different behaviors. Since the outer gap
The same kind of information-stripping appears everywhere. Claude Shannon — the father of information theory, working at Bell Labs in 1948 — showed that you can compress a message by throwing away everything the receiver doesn't need. The key insight is the same: once you identify what a process is blind to, you can throw that part away and work with a much smaller, cleaner picture.
The mystery of why 10,000 different starting points all converge had already begun to dissolve. The question was never about 10,000 numbers. It was about 54 states.
With only 54 states, the next step felt obvious: describe the rule as a simple formula. One formula, one proof, done.
It didn't work.
The problem is the sorting step. After each subtraction, you have to rearrange the digits from biggest to smallest before subtracting again. The permutation required to sort the result of one subtraction (say, 1089) is fundamentally different from the permutation required for another (like 7263). Because the sorting "shuffle" changes based on the value of the digits, you cannot describe the entire routine with a single, continuous algebraic formula.
Donald Knuth — who began writing the definitive encyclopedia of computer science in 1962, a project he's still working on — identified this as a fundamental property: sorting is not one operation, it's a family of shuffles, and which shuffle you get depends on what you're sorting. You can't capture that in a single formula.
The fix was to stop trying to write one formula and instead write several. This is an approach that Leon Chua at Berkeley used in the 1980s to analyze electronic circuits with switches — circuits that behave simply when the switch is in one position and simply when it's in the other, but the switching itself is complicated. His trick: analyze each switch position separately using simple math, then stitch the results together.
We did the same thing. The 54 states naturally group into regions — clusters where the digits always sort into the same order. Within each region, the rule for getting from one state to the next is a clean, predictable formula:
where
Then a sharp objection came up. Many different 4-digit numbers share the same
This is the question of attractor basins — a concept from dynamical systems theory, formalized in the study of chaos and turbulence in the 1960s and 70s by mathematicians like Edward Lorenz and Stephen Smale. An attractor basin is the set of all starting points that eventually flow to the same destination, regardless of which specific point you pick. The question isn't "which raindrop are you?" — it's "which river do you flow into?"
Here, the answer is clean: the ambiguity only exists for the very first step. After one round of subtract-and-sort, the carry rules of base-10 arithmetic lock everything into a specific form. The system sheds its ambiguity after a single iteration and never looks back. It doesn't matter which number you started with. The gaps are all that matter, and the gaps are unambiguous from step 1 onward. Every one of the 9,990 eligible numbers falls into the same attractor basin — the one that drains to 6174.
Up to this point, everything was arithmetic — formulas, gap calculations, region analysis. Now we crossed into a different territory: topology, the study of how things are connected. The arithmetic tells you what each state does locally. The topology tells you what the entire system does globally.
With the arithmetic locked down, we built the machine to verify it.
The tool is a directed graph — a map of 54 dots connected by arrows. Each dot is one of the 54 states. Each arrow points to the next state. Every dot has exactly one arrow out, so the system is deterministic — there are no choices, no random paths. The destination is baked into the starting digits from the moment you begin. This kind of structure was formalized by Michael Rabin and Dana Scott in a 1959 paper that won them the highest prize in computer science.
Convergence comes down to three checks:
1. Exhaustion — map every state. We built the full graph of all 54 states. At every state, the engine checks: does the clean formula (✓ Verified. The arithmetic and the formulas agree perfectly.
2. Attractor identification — find the destination. A "sink" is a state whose arrow points back to itself — it has arrived and stays. The analysis confirms there is only one sink in the entire graph: the state where the outer gap is 6 and the inner gap is 2, which corresponds to the number 6174.
3. Cycle detection — check for traps. In 1972, Robert Tarjan — then a young computer scientist at Bell Labs — published a method for finding loops in directed graphs. A loop is a set of states that cycle among themselves forever without going anywhere else. If loops exist other than the destination, some numbers would get trapped and never reach 6174. Tarjan's method found:
-
Exactly 1 loop — the single point
$(6,2)$ , which is 6174 mapping to itself -
53 states with no loops — every other state eventually reaches
$(6,2)$ and stays there - Nothing escapes — there are no traps, no infinite wanderings, no black holes
- 7 steps maximum — the longest possible journey through the graph is bounded
At
Kaprekar's 1949 discovery wasn't a parlor trick. It was the identification of a fixed point in a system where simple arithmetic rules, acting on digit gaps, funnel every possible starting state through a 54-dot map to a single inevitable destination. What began as recreational mathematics in a small Indian town turns out to be a bridge connecting number theory, graph analysis, and the formal verification methods that underpin modern computer science.
The final application unifies everything into three views:
The 100×100 grid. Every number, every path, every convergence depth. Click any cell to trace its arithmetic. The raw observation that started the journey.
The 54-state directed graph. States colored by which region they belong to. Arrows showing transitions. The loop analysis, the maximum path length, and the complete breakdown of every region's formula — all verified. The machinery that explains why the observation holds.
Both views running simultaneously for any input number. The grid path and the graph path side by side. A table showing every step's raw digit arithmetic and its formula computation, confirming they agree at every step. The proof that the observation and the algebra are the same thing.
The formal analysis of Kaprekar's constant using state-space reduction and directed graphs is well established in mathematical literature. Several researchers have published solutions using nearly identical core logic:
- The 54-State Reduction. Since the 1970s, papers by Gordon Prichett, Hermann Hasse, and Ludwig P. Houck have used this exact "difference-tuple" (gap) method, identifying that 10,000 numbers collapse into 54 equivalence classes based on digit differences.
-
The Gap Formula (
$999x + 90y$ ). This algebraic decomposition is the core of most formal proofs. In a 1981 paper, The Determination of Kaprekar Convergence, Prichett et al. used a variation of this to map the state space across different bases. - Directed Graph Analysis. The use of a directed graph to prove convergence is the formal standard in computational number theory. Yutaka Nishiyama has written extensively on the "Kernel" (6174) using these graph structures, documenting the 7-step maximum.
- Piecewise Linear Systems. Mathematicians often describe the Kaprekar routine as a piecewise linear map or a "finite dynamical system" — precisely the framing used here via Leon Chua's switched-circuit analogy.
While the math — the 54 states and the gap formula — is a known proof, the narrative structure here is original. Most academic papers focus strictly on the linear algebra. This project frames the solution through Noetherian invariants (signal vs. noise), Shannon compression (information-stripping), and Tarjan's cycle detection (formal verification) — building a computer science bridge to a recreational math problem.
| Feature | Academic Precedent | This Project |
|---|---|---|
| State Count | 54 (standard in literature) | 54 (derived via gap logic) |
| Logic | Difference Tuples | Signal vs. Noise (Noether) |
| Mechanism | Iterative Mapping | Piecewise Linear Switches (Chua) |
| Verification | Exhaustive search | Directed Graph Analysis (Tarjan) |
- The Mysterious 6174 — Numberphile — Roger Bowley demonstrates the Kaprekar routine and its history
- Prichett, G. D. (1981). The Determination of Kaprekar Convergence
- Nishiyama, Y. The Kaprekar Routine — graph-based analysis of the 7-step bound
npm install
npm run dev # → http://localhost:5173/
npm run build # → dist/
npm run preview # preview production buildPushes to main auto-deploy via GitHub Actions.
React 18 · Vite · Canvas API · SVG · GitHub Pages
├── Kaprekar.jsx # Math engine + 3 views (~950 lines)
├── src/main.jsx # Entry point
├── index.html # Shell
├── vite.config.js # Vite config (GH Pages base path)
├── package.json
└── .github/workflows/deploy.yml # CI/CD
MIT