Function Repository Resource:

Game24Solutions

Source Notebook

Find the list of solutions to the 24 Game by trying all possible options

Contributed by: Rauan Kaldybayev

ResourceFunction["Game24Solutions"][ cards ]

solve the 24 Game with an arbitrary number of cards.

Details and Options

ResourceFunction["Game24Solutions"] takes the following options:
"Goal"24the target integer
"AllowPower"Falsewhether to allow Power as a possible operation
"OutputForm"Stringdetermines the format of the output
The option "OutputForm" accepts String, Hold and Inactive as values. The result will be a list of solutions with the specified form. This can make the output more convenient for further operations. However, this makes it harder to read and less aesthetic. Moreover, multiplication and division by 1 gets omitted if "OutputForm" is set to Hold, and division and subtraction are presented through multiplication and addition if "OutputForm" is set to Inactive.
ResourceFunction["Game24Solutions"] returns an empty list if the puzzle is impossible.

Examples

Basic Examples (2) 

Use the function to find solutions to the 24 Game with four cards:

In[1]:=
ResourceFunction["Game24Solutions"][{6, 2, 7, 4}]
Out[1]=

Each element of the output list is a String expression representing a solution to the puzzle. If no solutions are found, an empty list is returned:

In[2]:=
ResourceFunction["Game24Solutions"][{8, 13, 21, 34}]
Out[2]=

Solve the puzzle with five cards given:

In[3]:=
Column[ResourceFunction["Game24Solutions"][{8, 4, 7, 1, 9}]]
Out[3]=

Scope (2) 

The values of all cards and the goal number must be positive integers:

In[4]:=
ResourceFunction["Game24Solutions"][{-1, Sqrt[2], 3 I, Log[4]}, Goal -> 24/7]
Out[4]=

Even though in the original 24 Game there is a limit to how high the value of a card can be, Game24Solve allows solutions with any positive integer values of cards:

In[5]:=
Column[ResourceFunction["Game24Solutions"][{104, 92, 1973, 3946}]]
Out[5]=

Options (3) 

OutputForm can be set to String, Hold or Inactive:

In[6]:=
Column[ResourceFunction["Game24Solutions"][{4, 3, 1, 3}, "OutputForm" -> Hold]]
Out[6]=
In[7]:=
Column[ResourceFunction["Game24Solutions"][{4, 3, 1, 3}, "OutputForm" -> Inactive]]
Out[7]=

Set the goal number to 36:

In[8]:=
Column[ResourceFunction["Game24Solutions"][{6, 3, 5, 3}, "Goal" -> 36]]
Out[8]=

Solve the game when using powers is allowed. Note that allowing Power as an operation increases the computation time as there are more combinations to be checked:

In[9]:=
Column[ResourceFunction["Game24Solutions"][{4, 2, 3, 5}, "AllowPower" -> True]]
Out[9]=

Publisher

Wolfram Summer Camp

Version History

  • 1.0.0 – 13 August 2020

Related Resources

Author Notes

The running time of the function quickly rises with the number of input cards. Setting AllowPower True increases the computation time as well.

License Information