Function Repository Resource:

KryptoGame

Source Notebook

Reach a goal number using given numbers and simple operands

Contributed by: Ed Pegg Jr

ResourceFunction["KryptoGame"][numbers,goal]

with basic operands, reach goal using all numbers.

Details

The basic operands are +, -, × and ÷.
This game is known as Krypto, 24 and Countdown.
For 3, 4, 5 and 6 numbers there are 68, 1170, 27142 and 793002 inequivalent expressions. An earlier version of this code would have returned the 5! permutations of 1+2+3+4+5 for a goal of 15, for starters.

Examples

Basic Examples (4) 

Reach a goal number of 11 with the values (2,3,5):

In[1]:=
ResourceFunction["KryptoGame"][{2, 3, 5}, 11]
Out[1]=

Reach a goal number of 11 with the values (2,3,5,8):

In[2]:=
ResourceFunction["KryptoGame"][{2, 3, 5, 8}, 11]
Out[2]=

Compare to ResourceFunction Game24Solutions:

In[3]:=
ResourceFunction["Game24Solutions"][{2, 3, 5, 8}, "Goal" -> 11]
Out[3]=

Reach a goal number of 16:

In[4]:=
result = ResourceFunction["KryptoGame"][{2, 3, 5, 8}, 16]
Out[4]=

Convert the result to a computable form:

In[5]:=
ToExpression[result, StandardForm, Inactivate]
Out[5]=

Reach a goal number of 7 with the values (2,5,8,13,21):

In[6]:=
ResourceFunction["KryptoGame"][{2, 5, 8, 13, 21}, 7]
Out[6]=

Scope (3) 

Some combinations are very tricky:

In[7]:=
ResourceFunction["KryptoGame"][{6, 7, 8, 14, 15}, 59]
Out[7]=

Repeated numbers are supported:

In[8]:=
ResourceFunction["KryptoGame"][{6, 7, 8, 8, 8}, 59]
Out[8]=

Some number sets reach the goal in many ways:

In[9]:=
ResourceFunction["KryptoGame"][{1, 2, 3, 4, 5}, 15]
Out[9]=

Possible Issues (4) 

Only sets of 3, 4 or 5 numbers are allowed:

In[10]:=
ResourceFunction["KryptoGame"][{1, 2, 3, 4, 5, 6}, 21]
Out[10]=

With three numbers, many goal numbers cannot be reached:

In[11]:=
Grid[Table[{k, ResourceFunction["KryptoGame"][{11, 2, 7}, k]}, {k, 1, 10}]]
Out[11]=

With four numbers, many more goal numbers can be reached:

In[12]:=
Grid[Table[{k, ResourceFunction["KryptoGame"][{11, 17, 9, 13}, k]}, {k, 1, 10}]]
Out[12]=

With five numbers, most goal numbers can be reached in many ways, but the solutions may be difficult:

In[13]:=
Text@Grid[
  Prepend[Table[
    With[{res = ResourceFunction["KryptoGame"][{11, 17, 9, 13, 22}, k]}, {k, Length[res], First[res], Last[res]}], {k, 1, 10}], {"goal", "solutions", "simplest", "hardest"}], Frame -> All]
Out[13]=

Neat Examples (1) 

A set of digits with ten unique solutions:

In[14]:=
Grid[Table[{k, ResourceFunction["KryptoGame"][{3, 17, 23, 36}, k]}, {k, 1, 10}]]
Out[14]=

Requirements

Wolfram Language 14.0 (January 2024) or above

Version History

  • 1.0.0 – 13 December 2024

Related Resources

License Information