# Wolfram Language™

The French TV program Des chiffres et des lettres and its English adaptation Countdown test contestants on their numeracy skills. Construct a simple version of this game and solve it with the new function Groupings.

Specify the arithmetic operations that can be used to perform the calculations.

In:= `ops = {Plus, Subtract, Times, Divide};`

Generate a list of 4 numbers chosen randomly from a given set of numbers.

In:= `numbers = RandomChoice[{1, 2, 3, 5, 7, 10}, 4]`
Out= The total to get from these numbers and operations is also generated randomly.

In:= `total = RandomInteger`
Out= Construct all possible ways of using each number once, keeping in mind that the order matters for some of the arithmetic operations.

In:= `orderings = Flatten[Permutations /@ Subsets[numbers, {4}], 1]`
Out= Generate all possible combinations of each ordering with the given binary operations.

In:= `candidates = Groupings[orderings, ops -> 2, HoldForm];`

Some candidates produce ComplexInfinity messages due to division by 0, and they can be eliminated by using Quiet.

In:= `results = Quiet@ReleaseHold[candidates];`

Here are the number of combinations that produce the total requested.

In:= ```combinations = Thread[Equal[candidates, results]]; Count[Thread[Equal[candidates, results]], _ == total]```
Out= This is one of the possible combinations.

In:= `FirstCase[combinations, _ == total]`
Out= It may not be possible to obtain the exact total in some cases.

In:= ```total2 = 76; Count[combinations, _ == total2]```
Out= But you can look for the best approximations among the results by using the function Nearest.

In:= ```total2 = 76; Count[combinations, _ == total2]; DeleteCases[results, ComplexInfinity]; DeleteDuplicates@Nearest[%, total2]```
Out= 