A web app that finds all arithmetic expressions using 4 integers that equal 24.
Live: https://srihari-ganesh.github.io/24solver
Given 4 integers, the solver enumerates every valid Reverse Polish Notation expression using those numbers and the operators +, -, *, /. Arithmetic is done with exact rational fractions to avoid floating-point errors (e.g. 8 / (3 - 8/3) = 24 resolves correctly).
Algorithm:
- Generate all combinations of 3 operators from
{+, -, *, /}(4³ = 64 combos) - For each combo, form a 7-token sequence (4 numbers + 3 operators)
- Enumerate all permutations of those 7 tokens (up to 5040)
- Filter to valid RPN sequences, evaluate with rational arithmetic
- Collect infix representations in a
Setfor deduplication
Total work per query: ≤ 64 × 5040 = 322,560 evaluations — runs instantly in the browser.
| Input | Result |
|---|---|
3 3 8 8 |
(8 / (3 - (8 / 3))) (and others) |
1 1 1 1 |
No solution |
4 4 4 4 |
(4 - 4 + 4) * 4 (and others) |
1 1 11 11 |
Addition-only solutions |
Pure static HTML/CSS/JS — no build step needed.
- Create a repo named
24solveron GitHub under your account - Push
index.html(and this README) - In repo Settings → Pages, set source to the
mainbranch root - Site goes live at
https://<username>.github.io/24solver