What Challenge 24 Actually Is

Challenge 24 is a mathematical puzzle game where you take four numbers and try to combine them using basic arithmetic operations to reach exactly 24. The rules are simple: use all four numbers exactly once, use addition, subtraction, multiplication, division, and parentheses freely, and produce a result of 24. That's it. No exponentiation, no factorials, no concatenating digits together. I've been running this puzzle at dinner tables and bus rides for years. It sounds trivial until you hit a set where the solution isn't obvious, and then you realize there's actually a non-trivial search space hiding behind those four numbers. The puzzle has been around since the 1980s, originally sold as a physical card game, and it still shows up in coding interviews because it tests a specific kind of combinatorial thinking.

Why Challenge 24 Is a Decent Interview Question

It forces you to think about recursive enumeration without immediately resorting to brute force. The naive approach is to generate all permutations of the four numbers, try all possible binary operation trees, and check each one. That works fine for four numbers but gets unwieldy fast if you ever scale it up. The real insight is recognizing that you only need to track the intermediate results and recursively reduce the set size by one each step. Here's what I did wrong on my first pass at coding it. I tried to enumerate all expression trees as strings, which meant dealing with redundant parenthesis configurations and a mess of edge cases around division by zero. Much cleaner to just work with the numbers directly: pick two numbers from the current set, apply every valid operation, put the result back, and recurse. When you're down to a single number, check if it's close enough to 24 (using a small epsilon for floating point comparison).

The Recursive Reduction Approach

The algorithm works like this. Start with a list of four numbers. In each step, select every pair of numbers from the list. For each pair, compute all possible results of combining them with +, -, *, and /. Replace the two selected numbers with each result, producing a new list that's one element shorter. Recurse on that shorter list. Base case: when the list has one element, see if it equals 24 within tolerance. This handles the expression tree implicitly. When you compute (a + b) * (c - d), for example, the algorithm first reduces a and b into their sum, then c and d into their difference, then multiplies those two intermediate results. The order of pair selection doesn't matter because you try every possible pair at every level. One important detail: subtraction and division are not commutative, so for each pair you need to try both a - b and b - a, and both a / b and b / a (with a guard against division by zero). Addition and multiplication only need one direction since order doesn't change the result.

Get the Full Details

Broadford Year 5/6: The 24 game challenge
Broadford Year 5/6: The 24 game challenge

Floating Point Gotchas

This is where most implementations trip up. You're doing repeated division, and floating point precision means you'll rarely land on exactly 24.0. Use a tolerance check instead of exact equality. Something like abs(result - 24)

1e-6 works well in practice. If you set the epsilon too tight, you'll miss valid solutions. Too loose and you get false positives on inputs that are close but shouldn't count. I ran into a case once where a set that should have had a solution was returning none because I was comparing against 24 with no tolerance, and the floating point arithmetic left me at 23.9999999997 or something like that. Changed to epsilon comparison and it resolved immediately. Also worth noting that you should work in floats from the start, not ints, because intermediate divisions produce fractions that matter for the final answer.

Common Pitfalls and Shortcuts

Some sets of four numbers have no solution at all. That's not a bug, it's a feature of the puzzle. About a third of random four-number combinations are unsolvable. Don't waste time trying to force one. If you're building a generator, you need a solver that can report impossibility cleanly rather than looping until timeout. Another thing to watch: duplicate numbers. If your input has repeated values like [3, 3, 8, 8], the pairwise selection approach naturally handles this because it picks by position, not by value. But if you're optimizing with memoization or pruning, be careful not to skip valid branches just because two numbers look the same. For speed, you can prune early. If at any recursion level all remaining numbers are positive and their product is already less than 24, you can sometimes rule out that branch if the only operations available can't recover the deficit. This pruning isn't always safe to apply because subtraction and division can create larger intermediate values, so use it conservatively or skip it entirely for correctness.

A Working Implementation Sketch

Here's the core logic in Python. It's concise because the recursion does most of the heavy lifting. def can_make_24(nums): if len(nums) == 1: return abs(nums[0] - 24) < 1e-6 for i in range(len(nums)): for j in range(len(nums)): if i != j: rest = [nums[k] for k in range(len(nums)) if k != i and k != j] a, b = nums[i], nums[j] candidates = [a + b, a - b, b - a, a * b] if abs(b) > 1e-9: candidates.append(a / b) if abs(a) > 1e-9: candidates.append(b / a) for val in candidates: if can_make_24(rest + [val]): return True return False This is O(n!) in the worst case but for four numbers it runs in microseconds. The constant factor is small enough that even without memoization you're fine. If you extend this to six or seven numbers, you'd want to cache results keyed by sorted tuples of the current state to avoid re-exploring identical subproblems.

24 Cards Math Challenge | 24 game math puzzle, How to solve the 24 game, Math card games
24 Cards Math Challenge | 24 game math puzzle, How to solve the 24 game, Math card games

Where Challenge 24 Falls Short

As a standalone puzzle, it's great for warm-ups and casual play. As a training tool for algorithm design, it covers recursion and backtracking adequately but doesn't push much beyond that. Once you understand the pairwise reduction trick, there's not a lot of new ground to cover. If you're looking for a harder variant, try adding exponentiation as an allowed operation or requiring the use of all four numbers in their original order. Both changes significantly increase the search space and make manual solving considerably more painful. There are also online versions and mobile apps if you want to practice without writing code. The physical card game version is still available from various retailers. Nothing particularly exciting about the hardware, but it's a reliable way to keep the puzzle accessible without a screen. If you hit a set that stumps you, the best move is usually to write a quick solver and let the computer do the enumeration. Manual solving works for easy sets, but the harder ones benefit from letting the pairwise recursion explore every branch. That's the real value of Challenge 24, honestly. It teaches you to stop trying to see the answer and start building a systematic way to find it or prove it doesn't exist.