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.
ops = {Plus, Subtract, Times, Divide};Generate a list of 4 numbers chosen randomly from a given set of numbers.
numbers = RandomChoice[{1, 2, 3, 5, 7, 10}, 4]The total to get from these numbers and operations is also generated randomly.
total = RandomInteger[100]Construct all possible ways of using each number once, keeping in mind that the order matters for some of the arithmetic operations.
orderings = Flatten[Permutations /@ Subsets[numbers, {4}], 1]Generate all possible combinations of each ordering with the given binary operations.
candidates = Groupings[orderings, ops -> 2, HoldForm];Some candidates produce ComplexInfinity messages due to division by 0, and they can be eliminated by using Quiet.
results = Quiet@ReleaseHold[candidates];Here are the number of combinations that produce the total requested.
combinations = Thread[Equal[candidates, results]];
Count[Thread[Equal[candidates, results]], _ == total]This is one of the possible combinations.
FirstCase[combinations, _ == total]It may not be possible to obtain the exact total in some cases.
total2 = 76;
Count[combinations, _ == total2]But you can look for the best approximations among the results by using the function Nearest.
total2 = 76;
Count[combinations, _ == total2];
DeleteCases[results, ComplexInfinity];
DeleteDuplicates@Nearest[%, total2]