How the numbers solver works

The aim of the Countdown numbers game is to get as close as possible to a target number using some or all of the given number tiles. Each tile can be used at most once.

The allowed operations are addition, subtraction, multiplication, and division. Intermediate results must be positive whole numbers, so the solver ignores negative results and divisions that do not divide exactly.

Example: 100, 75, 8, 6, 3, 1 with a target of 927

One exact solution is:

100 + 6 = 106
106 * 8 = 848
75 + 3 = 78
848 + 78 = 926
926 + 1 = 927

The solver finds answers by doing a depth-first search over possible arithmetic expressions.

At each step, it picks two currently-available numbers and tries every valid operation on them. The result replaces those two numbers, reducing the selection by one number. The solver then recurses with this new smaller selection. This continues until there are no more useful combinations to try.

For example, if the available numbers include 100 and 6, the solver can combine them to make 106. It then searches the remaining problem using 106 as a new available number.

The search keeps track of the best result found so far. If an exact answer exists, it will find one. If no exact answer exists, it keeps the closest results it can find and reports how far away they are from the target.

As an optimisation, the solver skips operations that are valid mathematically but not useful for Countdown solutions, such as multiplying by 1 or dividing by 1. It also ignores calculations that simply recreate one of the input numbers.

The solver gives each calculation a rough complexity score. This is not an official Countdown rule; it is just a way to choose a cleaner-looking answer when there are several equally good results.

Each arithmetic step adds a cost based on the absolute value of the number made by that step. Trailing zeroes are ignored for this purpose, so making 400 has the same base cost as making 4. Addition has the base cost, subtraction is multiplied by 1.05, multiplication by 1.2, and division by 1.3. When a step is multiplication by 10, the solver sets the base cost to 1 before applying the multiplication weighting, so that step costs 1.2 regardless of the size of the result.

The total difficulty score is the sum of those step costs. When two calculations get equally close to the target, the solver prefers the one with the lower score. The number is only a rough ordering: it usually favours shorter, rounder, more mental-arithmetic-friendly solutions, but it is not intended to measure human difficulty perfectly.

After the search finishes, the solver tidies up the arithmetic before displaying it. It groups chains of addition or multiplication where possible, removes duplicate displayed solutions, and formats each answer as a sequence of calculation steps.