A 24 game solver algorithm is a recursive pair-and-combine procedure that treats the four numbers you enter as a pool of terms, picks any two, joins them with one of the four basic arithmetic operations, replaces the pair with the new value, and repeats until one term is left to test against 24. The algorithm that drives our 24 Game Solver tool stores every intermediate value as a reduced integer numerator over a positive integer denominator, so the final comparison becomes numerator = 24 × denominator and rejects any answer that is only close to 24 instead of exactly equal to it. Because subtraction and division are not commutative, the algorithm tries both orders of every pair for those two operators and skips division when the divisor is zero. With four inputs capped at 99, intermediate integer products stay inside the safe-integer range, and the search stays exact without overflow workarounds. The procedure is exhaustive for its disclosed rule set: when it returns no solution, that result is a proof that no expression under the displayed operations equals 24 rather than an unlucky miss by this particular run.

24 game solver algorithm
24 game solver algorithm

How the Pair-and-Combine Algorithm Walks the Pool

The algorithm behind the 24 Game Solver treats the four numbers as a pool of term objects, each one a reduced fraction rather than a raw integer or float. At every recursion step it chooses an unordered pair of terms, generates every legal combination of those two terms using one of the four basic operations, returns the new term to a smaller pool, and recurses on the smaller pool of three. It keeps doing this until the pool contains a single term.

Starting from (a, b, c, d), the first step produces a pool of three: ((a ⊕ b), c, d), where ⊕ is one of the four operations. The next step picks another unordered pair from that three-term pool — ((a ⊕ b) ⊕ c, d) or (a, (b ⊕ c) ⊕ d), or another permutation — and recurses again. Different orderings of which pair is selected first correspond to different parenthesizations, so by rotating its first-pair choice across recursion branches the algorithm walks every binary tree that four inputs and the four basic operations can produce.

Addition and multiplication are commutative, so the algorithm generates one result per unordered pair for those operators. Subtraction and division are not, so each pair produces two candidates for those operators — (a − b) and (b − a), then (a ÷ b) and (b ÷ a) when the divisor is nonzero. The denominator-positive rule is enforced at every reduction step so the algorithm never has to reason about ambiguous signs on a single intermediate value. A worked walkthrough of this pair-and-combine method appears in the exhaustive-search guide.

Why Reduced Fractions Beat Floating-Point Math

The final equality check

Every candidate produced by the recursion is checked by writing its reduced form as p / q with q positive and demanding that p = 24 × q. Because every reduction step divides both numerator and denominator by their greatest common divisor, repeated operations do not accumulate common factors that would change the meaning of the comparison. A value that only approximates 24 — such as 23.99999998 produced by floating-point division on 47/3 × 24/6 — fails this check and is rejected as a candidate. A value that is exactly 24 as a fraction, like 192/8 from a chain of legal divisions, passes it correctly.

Why binary floats miss valid answers

Some hands legitimately require fractional intermediate values. The classic pair (3, 3, 8, 8) reaches 24 only by hitting 8/3 somewhere in the middle, then pulling the rest of the numbers to a final value of 24. A solver that rounds every intermediate to a binary float can turn 8/3 into 2.6666666666666665 and then miss the rest of the chain that would have closed out at exactly 24. Storing the intermediate as the reduced fraction 8/3 instead of that decimal preserves every later operation that depends on the exact numerator and denominator. According to the 24 Game program documentation at UC Santa Cruz and the cross-checked hand list at 4nums, this fraction-exact approach is the standard convention for serious 24 game solvers, so using reduced fractions here is a faithful implementation of the published rule set rather than a custom choice. For a deeper comparison with integer-guessing heuristics, see the fraction-math guide.

How to Use the 24 Game Solver to Run the Algorithm

Open the 24 Game Solver in your browser. The page needs no login, runs entirely in the local tab, and handles one hand per query.

  1. Enter exactly four whole numbers from 1 through 99, one per field. Leading zeros, negative values, and decimal inputs are not accepted.
  2. Select Solve. The algorithm then walks every unordered pair and every legal combination, switching to reduced fractions at every step so the arithmetic stays exact.
  3. Read each returned expression from the inside out. Start with the innermost parenthesized pair, evaluate it as a fraction, then work outward one pair at a time.
  4. Compare up to 50 returned constructions against your own attempt. The list is sorted by length first and by locale order next, so the shortest credible answers appear first.
  5. If the page reports no solution, treat that verdict as final under the disclosed rules. A different rule set — powers, factorials, concatenation, or repeated numbers — is a separate problem and may have answers this constrained solver will not show.

For most hands this completes in well under a second. Hands with many solutions may push the page near its 50-expression cap; that cap exists to keep the tab readable when an answer has many equivalent forms.

Worked Example: Running the Algorithm on 3, 3, 8, 8

The hand (3, 3, 8, 8) is the textbook case where the algorithm must use a fractional intermediate, so it is a useful first walkthrough. The valid family of expressions under the basic rules reduces to:

8 ÷ (3 − 8 ÷ 3) = 24

Working from the inside outward:

  • The inner pair is 8 ÷ 3, which reduces to 8/3 as a fraction.
  • The next pair is 3 − 8/3, which is (9/3 − 8/3) = 1/3 as a fraction.
  • The outer pair is 8 ÷ 1/3, which is 8 × 3 = 24/1.
  • Final equality check: numerator = 24, denominator × 24 = 24 × 1 = 24, and the values match.

Plugging (3, 3, 8, 8) into the tool and selecting Solve returns expressions from that family in the results list, alongside reordered forms that express the same arithmetic idea in slightly different notation. A solver restricted to integer divisions would never reach this path because the inner 8/3 step collapses to a rounded value that breaks the chain.

Hands That Test the Algorithm's Edge Cases

Four published anchors exercise the implementation continuously. The verification numbers below are not computed on this page; they are the hands the algorithm is checked against.

HandSolvable?What the algorithm must demonstrate
1, 2, 3, 4YesA pure multiplication chain reaches 24 with every input used exactly once.
3, 3, 8, 8YesA fraction of 8/3 appears mid-expression and is preserved through the rest of the chain.
1, 5, 5, 5YesMore than one reduced fraction is required before the chain closes at 24.
1, 5, 11, 13NoThe exhaustive search proves that no exact 24 exists under the basic operations.

Beyond solvability, the same test deck covers input-length validation, the 99-integer bound, negative-denominator normalization, division presence, and the 50-result cap. The implementation's contract is verified against the UC Santa Cruz rule statement and the hand list at 4nums.

Operations the Algorithm Rejects

The 24 game solver algorithm accepts only the four basic operations as defined in the published rule set. It rejects every other transformation because the verification anchors above would not hold with looser rules.

OperationAllowed here?Reason
AdditionYesPart of the basic operation contract.
SubtractionYesPart of the basic operation contract; both orders are tried.
MultiplicationYesPart of the basic operation contract.
DivisionYesPart of the basic operation contract with a positive-denominator rule.
ConcatenationNoWould let 1 and 23 form 123, breaking the four-numbers-once rule.
ExponentiationNoOutside the basic operation contract.
FactorialsNoOutside the basic operation contract.
RootsNoOutside the basic operation contract.
Decimal-point insertionNoTreats a whole number as a non-integer and violates the integer input contract.
Number reuseNoEvery input must appear exactly once in the final expression.
Unary negation as a separate operationNoSubtraction with a zero term is a different problem and is not invoked separately.

This narrow contract matches the 24 Game resource at UC Santa Cruz and the cross-checked hand list at 4nums, so the algorithm stays faithful to the published 24 Game while not silently expanding to cover puzzles that allow powers, factorials, or concatenation.

Reading Returned Expressions From the Inside Out

Every returned expression shows its full parenthesized form followed by = 24, so an answer can be checked directly instead of accepted as an unexplained success message. Parentheses are not decorative. They disclose the order of operations that produced the value, and removing them can reorder the implied groupings into a different expression that no longer equals 24.

The inspection flow is to start with the innermost parenthesized pair, reduce it to a single fraction, work outward one pair at a time, and confirm the final equality by the same numerator-equals-24-times-denominator rule the algorithm uses. If the expression does not reduce to exactly 24 under that process, the construction is wrong. If it does, the construction is right and the algorithm's verdict is vindicated for that hand under the rule set displayed above the input fields.