Test Your Numeracy Skills
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.
![Click for copyable input](assets.en/test-your-numeracy-skills/In_79.png)
ops = {Plus, Subtract, Times, Divide};
Generate a list of 4 numbers chosen randomly from a given set of numbers.
![Click for copyable input](assets.en/test-your-numeracy-skills/In_80.png)
numbers = RandomChoice[{1, 2, 3, 5, 7, 10}, 4]
![](assets.en/test-your-numeracy-skills/O_82.png)
The total to get from these numbers and operations is also generated randomly.
![Click for copyable input](assets.en/test-your-numeracy-skills/In_81.png)
total = RandomInteger[100]
![](assets.en/test-your-numeracy-skills/O_83.png)
Construct all possible ways of using each number once, keeping in mind that the order matters for some of the arithmetic operations.
![Click for copyable input](assets.en/test-your-numeracy-skills/In_82.png)
orderings = Flatten[Permutations /@ Subsets[numbers, {4}], 1]
![](assets.en/test-your-numeracy-skills/O_84.png)
Generate all possible combinations of each ordering with the given binary operations.
![Click for copyable input](assets.en/test-your-numeracy-skills/In_83.png)
candidates = Groupings[orderings, ops -> 2, HoldForm];
Some candidates produce ComplexInfinity messages due to division by 0, and they can be eliminated by using Quiet.
![Click for copyable input](assets.en/test-your-numeracy-skills/In_84.png)
results = Quiet@ReleaseHold[candidates];
Here are the number of combinations that produce the total requested.
![Click for copyable input](assets.en/test-your-numeracy-skills/In_85.png)
combinations = Thread[Equal[candidates, results]];
Count[Thread[Equal[candidates, results]], _ == total]
![](assets.en/test-your-numeracy-skills/O_85.png)
This is one of the possible combinations.
![Click for copyable input](assets.en/test-your-numeracy-skills/In_86.png)
FirstCase[combinations, _ == total]
![](assets.en/test-your-numeracy-skills/O_86.png)
It may not be possible to obtain the exact total in some cases.
![Click for copyable input](assets.en/test-your-numeracy-skills/In_87.png)
total2 = 76;
Count[combinations, _ == total2]
![](assets.en/test-your-numeracy-skills/O_87.png)
But you can look for the best approximations among the results by using the function Nearest.
![Click for copyable input](assets.en/test-your-numeracy-skills/In_88.png)
total2 = 76;
Count[combinations, _ == total2];
DeleteCases[results, ComplexInfinity];
DeleteDuplicates@Nearest[%, total2]
![](assets.en/test-your-numeracy-skills/O_88.png)