An interactive, high-performance Constraint Satisfaction Problem (CSP) solver and visual carry sandbox for alphametic puzzles (cryptarithmetic). Built with pure modern web technologies with zero dependencies or build steps.
Live Demo: https://freakyvendetta.github.io/cryptarithmetic-csp-solver/
A cryptarithmetic puzzle (also called an alphametic) is a mathematical game where digits (
$$\begin{array}{rccccc} & & \text{S} & \text{E} & \text{N} & \text{D} \
- & & \text{M} & \text{O} & \text{R} & \text{E} \ \hline & \text{M} & \text{O} & \text{N} & \text{E} & \text{Y} \end{array} \qquad \Longrightarrow \qquad \begin{array}{rccccc} & & 9 & 5 & 6 & 7 \
- & & 1 & 0 & 8 & 5 \ \hline & 1 & 0 & 6 & 5 & 2 \end{array}$$
-
Uniqueness: Each letter corresponds to one and only one unique decimal digit from
${0, 1, \dots, 9}$ . -
Leading Zero Prohibition: The first letter of any multi-digit word cannot be
$0$ . - Arithmetic Correctness: The sum of the left-hand words must strictly equal the right-hand word.
In Artificial Intelligence, cryptarithmetic is formally modeled as a Constraint Satisfaction Problem (CSP) defined by the triple
Let
To model column addition efficiently, we introduce auxiliary carry variables
-
Letter Variables:
$D(L_j) = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}$ -
Leading Letters:
$D(L_{\text{leading}}) = {1, 2, 3, 4, 5, 6, 7, 8, 9}$ -
Carry Variables:
$D(C_i) = {0, 1, \dots, k-1}$ (for two addends,$C_i \in {0, 1}$ )
-
Global AllDifferent Constraint:
$$\text{AllDifferent}(L_1, L_2, \dots, L_n)$$ -
Column-Wise Arithmetic Relations:
For each column position$i$ (from units column$i=0$ to highest column$m-1$ ):$$\left(\sum_{j=1}^{k} \text{val}(w_j[i])\right) + C_{i-1} = \text{val}(\text{result}[i]) + 10 \cdot C_i$$ with initial carry$C_{-1} = 0$ and boundary carry$C_{m-1} = 0$ (or matched to the leftmost result digit).
| Strategy | Search Space | Pruning Moment | Average Solve Time |
|---|---|---|---|
| Naive Brute Force | Evaluates after all |
2,000 to 15,000 ms (1.8M–3.6M checks) | |
| Column-Wise CSP Backtracking (This Engine) | Bound by valid carry branches | Pruned at each individual column addition | 0.2 to 13 ms (~500x Speedup) |
Instead of generating full word assignments, the engine traverses from right to left (least significant column to most significant):
-
Assigns addends within column
$i$ : Only branches on unused digits that satisfy non-zero leading constraints. -
Deterministic RHS deduction: Once addend digits in column
$i$ and carry$C_{i-1}$ are known, the target digit$D_{\text{target}} = (\text{sum} + C_{i-1}) \bmod 10$ is computed directly rather than guessed. -
Instant Backtrack: If
$D_{\text{target}}$ is already claimed by another letter or violates leading zero rules, the entire subtree is pruned immediately without descending deeper.
- ⚡ High-Speed CSP Engine: Implements column-wise backtracking with carry propagation, solving 10-variable puzzles in under 15 ms in browser JavaScript.
- 🎮 Interactive Sandbox Mode:
- Hands-on exploration mode for students and educators.
- Drag or click digits to assign to letters with instant visual carry calculations (
$C_1, C_2, C_3, \dots$ ). - Real-time column validity check with color-coded alerts (valid column sum, invalid carry, leading zero violation).
- 📊 Search Telemetry & Metrics: Displays search node steps, backtrack counts, and millisecond execution times.
- 📋 One-Click Solution Copy: Copy solved mappings (
S=9, E=5, N=6, ...) formatted for papers and reports. - 🎨 Modern Dark UI: Designed with glassmorphic cards, lime accent tokens, Fira Code typography, and responsive CSS Grid layout.
- 📱 Zero Dependencies: Pure HTML5, CSS3, and modern vanilla ES6+. No Webpack, npm install, or compilation needed.
Empirical performance measured on standard academic benchmark puzzles:
| Puzzle Equation | Unique Letters | Solutions | Search Steps | Backtracks | Solve Time |
|---|---|---|---|---|---|
SEND + MORE = MONEY |
8 | 1 | 8,517 | 5,554 | 13.10 ms |
TEN + TEN + FORTY = SIXTY |
10 | 1 | 4,339 | 2,116 | 3.22 ms |
ONE + ONE = TWO |
5 | 16 | 327 | 50 | 0.19 ms |
BASE + BALL = GAMES |
7 | 1 | 522 | 158 | 0.31 ms |
CROSS + ROADS = DANGER |
9 | 1 | 3,510 | 1,393 | 3.50 ms |
APPLE + GRAPE = CHERRY |
9 | 1 | 2,830 | 1,509 | 5.06 ms |
LOGIC + LOGIC = PROLOG |
7 | 1 | 442 | 72 | 1.47 ms |
WRONG + WRONG = RIGHT |
8 | 21 | 1,639 | 529 | 1.48 ms |
KYOTO + OSAKA = TOKYO |
6 | 1 | 2,032 | 659 | 1.39 ms |
LETS + WAVE = LATER |
8 | 1 | 8,517 | 5,554 | 9.64 ms |
Since this project requires zero dependencies, you can run it immediately without installing any tools:
Simply open index.html in any modern web browser (Chrome, Firefox, Safari, Edge):
# Windows
start index.html
# macOS
open index.html
# Linux
xdg-open index.htmlpython -m http.server 8000
# Visit http://localhost:8000cryptarithmetic-csp-solver/
├── index.html # Semantic HTML5 markup & app layout
├── css/
│ └── style.css # Glassmorphism design system & responsive grid layout
├── js/
│ ├── csp-solver.js # Core CSP column-wise backtracking & carry propagation engine
│ └── app.js # UI controller, sandbox events & interactive digit pool
├── README.md # Documentation, CSP mathematical theory & benchmarks
└── LICENSE # MIT Open Source License
- Author: freakyvendetta
- License: MIT License. See LICENSE for details.
- Theory: Based on classical Constraint Satisfaction Problems (Russell & Norvig, Artificial Intelligence: A Modern Approach).