Solve a Number Decomposition
The new function Groupings provides a powerful method to generate all possible results of combining certain objects and operations. In particular, it is very useful for solving some numeric games like those in the French TV program Des chiffres et des lettres, or some popular recent arithmetic questions posted in social networks.
Determine how to obtain 10 using the integers 1, 1, 5, 8 and the four basic arithmetic operations.
In[1]:=
ints = {1, 1, 5, 8};
ops = {Plus, Subtract, Times, Divide};
There exist 3,840 possible combinations of those four integers and the given binary operations.
In[2]:=
Length[combs = Groupings[Permutations[ints], ops -> 2, HoldForm]]
Out[2]=
Here is a sample of them.
In[3]:=
RandomSample[combs, 10]
Out[3]=
Compute the result of each combination, many of them involving division by 0.
In[4]:=
results = Quiet@ReleaseHold[combs];
This is the only combination that produces 10.
In[5]:=
Cases[Thread[Equal[combs, results]], _ == 10]
Out[5]=
The most frequent result is 13, generated in 240 different ways.
In[6]:=
TakeLargestBy[Tally[results], Last, 5]
Out[6]=