# Game: 24 Points (v1.3)

Game: 24 Points (v1.3)

In this very simple game, 4 cards are randomly drawn from a deck. The goal is to combine the cards’ values using the four arithmetic operators (plus, minus, times, and integer division) to obtain 24. A, J, Q, and K are worth, respectively, 1, 11, 12, and 13. The first one to find a solution wins.

The solution is calculated by the functions in the cell below. Note the use of recursion. This cell should run once before doing anything else.

ClearAll["Global`*"]combine[numberset_, op_] := If[Length[numberset] == 1, numberset, Flatten[ Table[ Tuples[{op, combine[i, op], combine[Complement[numberset, i], op]}], {i, Complement[Subsets[numberset], {{}, numberset}]} ], 1 ] ];compute[v_] := If[Length[v]3, (v[[1]][compute[v[[2]]],compute[v[[3]]]]), v];solve[draw_,target_] := Module[{rule,x,symvector}, symvector = ToExpression["n" <> ToString[#]] & /@ Range[Length[draw]]; rule = MapThread[#1 If[#21,HoldForm[1],#2]&, {symvector,draw}]; x = Select[ (HoldForm /@ uniquecombos) /. rule, Quiet[ReleaseHold[ReleaseHold[#]] target, {Power::"infy",General::"indet"}]& ]; x];tabulate[x_] := TraditionalForm[Transpose[{x}]]solve24[draw_,display_:False,debug_:False] := Module[{sol,n,msg}, sol=solve[draw,24]; n=Length[sol]; msg=Switch[n,0,"no solutions.",1,"1 solution.",_,ToString[n]<>" solutions."]; Print["The combination ",draw," has ",msg]; If[debug,Print[FullForm[sol]]]; If[n>0&&display,tabulate[sol]]];challenge[] := Module[{draw}, While[Length[solve[draw=RandomInteger[{1,13},4],24]]0,Null]; solve24[draw];];

This cell define a list of valid operators and a vector of symbolic numbers, and calculates a symbolic solution. As an added bonus, the cell returns the number of distinct algebraic combinations that need to be tested. For the classic game, using the four arithmetic operators, this is 1,192. However, one can be creative and add any binary operators of the form Operator[x_,y_]. Examples are Power and Mod. Similarly, the classic game is played with four numbers but it is OK to specify a different, um, number of numbers such as {n1, n2, n3, n4, n5}. This cell too should be run once, and it should be run again if changing any of the rules.

operators = {Plus, Subtract, Times, Divide};symvector = {n1, n2, n3, n4};allcombos = combine[symvector,operators];uniquecombos = DeleteDuplicates[FullSimplify[compute[#]]& /@ allcombos];Length[uniquecombos]

In[]:=

1192

Out[]=

## Examples

Examples

If no second argument is specified, the function only returns the number of solutions. This way one can still have all the fun.

solve24[{10,2,1,11}]

In[]:=

The combination {10,2,1,11} has 1 solution.

If a second argument is specified as True, the solutions are tabulated. Some are hard to find without knowing they exist, such as the third one in this example. Knowing they exist can make the game more fun.

solve24[{3,5,9,13},True]

In[]:=

The combination {3,5,9,13} has 3 solutions.

-3+5+9+13 |

9 3 |

1 5 |

Out[]//TraditionalForm=

If there are no solutions, the function (obviously) returns no table even if the second argument is True.

solve24[{5,5,5,11},True]

In[]:=

The combination {5,5,5,11} has no solutions.

If two or more numbers are the same, they are treated as distinct. One could opine that {7, 3, 2, 2} has 2, not 4 solutions, but even if I bought that, it would be its own project to teach Mathematica that these are in fact the same.

solve24[{7,3,2,2},True]

In[]:=

The combination {7,3,2,2} has 4 solutions.

37+ 2 2 |

37+ 2 2 |

2(7+3+2) |

(7+3+2)2 |

Out[]//TraditionalForm=

Of course, having a "1" in the combination may result in a large number of solutions. If 24 can be formed by combining the other three numbers, then dividing or multiplying each of the three numbers by "1" gives a valid solution.

solve24[{10,1,2,12},True]

In[]:=

The combination {10,1,2,12} has 14 solutions.

10+1(2+12) |

10+2+112 |

10+2+ 12 1 |

10+12+12 |

10+ 2 1 |

10+ 2+12 1 |

1(10+2+12) |

2+1(10+12) |

101+2+12 |

10 1 |

2+ 10+12 1 |

1(10+2)+12 |

10+2 1 |

10+2+12 1 |

Out[]//TraditionalForm=

This “problem” (if we can call it a problem) happens also when, e.g., two numbers make 24 (e.g. 12 x 2 or 13 + 11) and the other two make 0 (e.g. 5 - 5) or 1 (e.g. 5 / 5 or 3 - 2).

solve24[{5,5,12,2},True]solve24[{2,3,13,11},True]

In[]:=

The combination {5,5,12,2} has 9 solutions.

5+5+12+2 |

5-5+12×2 |

5×12×2 5 |

-5+5+12×2 |

5×12×2 5 |

12(5-5+2) |

12(-5+5+2) |

(5-5+12)2 |

(-5+5+12)2 |

Out[]//TraditionalForm=

The combination {2,3,13,11} has 8 solutions.

13+(-2+3)11 |

13+ 11 -2+3 |

13- 11 2-3 |

-213+3×13+11 |

13 -2+3 |

- 13 2-3 |

(-2+3)(13+11) |

13+11 -2+3 |

Out[]//TraditionalForm=

Finally, you can request a random challenge. Unlike drawing four cards from a deck, this method guarantees that the random combination will have at least one solution. Enjoy!

challenge[]

In[]:=

The combination {4,2,3,13} has 3 solutions.