# The 24 Game

The Python program below finds solutions to the 24 game: use four numbers and any of the four basic arithmetic operations (multiplication, division, addition and subtraction) to produce the number 24 (or any number you choose).

Execute the program, choose four numbers (separated by commas) and the target number (24 by default).


```python
from sympy import symbols, parsing
from sympy.parsing.sympy_parser import parse_expr
from itertools import permutations, combinations_with_replacement
# list the default numbers to use here
numbahs = [2,4,6,8]
# enter the default number to calculate here
ansah = 24

numberlist = input("Enter a list of four integers, separated by commas [default: 2,4,6,8]: ")
if numberlist:
    numbahs = [int(n) for n in numberlist.split(',')]
answer = input("Enter a number compute to [default: 24]: ")
if answer:
    ansah = int(answer)

# generate all possible arrangements of parentheses and operators
# ways of combining arguments with parenthesis
pgroups = [
    "{0}{1}{2}{3}{4}{5}{6}",
    "({0}{1}{2}){3}{4}{5}{6}",
    "({0}{1}{2}{3}{4}){5}{6}",
    "(({0}{1}{2}){3}{4}){5}{6}",
    "({0}{1}({2}{3}{4})){5}{6}",
    "{0}{1}({2}{3}{4}){5}{6}",
    "{0}{1}({2}{3}({4}{5}{6}))",
    "{0}{1}(({2}{3}{4}){5}{6})",
    "{0}{1}({2}{3}{4}{5}{6})",
    "{0}{1}{2}{3}({4}{5}{6})"
]

# available operators
operators = ['*','/','+','-']
# available symbols
variables = ['a','b','c','d']
# symbols for sympy to work with
a, b, c, d = symbols('a b c d')

# does a file exist with unique expressions?
try:
    with open("uniqueexpressions.txt") as f:
        expressions = [parse_expr(s) for s in f.readlines()]
        
# no file.. generate one
except:
    # collect unique expressions in a set
    expressions = set()
    for p in pgroups: # for every combination of parenthesis
        for combo in combinations_with_replacement(operators, 3): # and every combination of operators
            for o in permutations(combo):  # permuted
                for v in permutations(variables): # and every permutation of variables
                    s = p.format(v[0],o[0],v[1],o[1],v[2],o[2],v[3]) # construct the expression and...
                    s = parse_expr(s)
                    # add to expressions set -- this will drop equivalent expressions
                    expressions.add(s) 
    with open("uniqueexpressions.txt", "w") as f:
        [f.write(str(s)+'\n') for s in expressions]
                    
# for every unique expression, substitute the values and evaluate
for x in expressions:
    try:
        val = x.subs({a:numbahs[0],b:numbahs[1],c:numbahs[2],d:numbahs[3]})
        if ansah == val:
            s = str(x)
            for sym, num in zip(variables, numbahs):
                s = s.replace(sym, str(num))
            print(s)
    except:
        pass
#print(expressions)
```

    Enter a list of four integers, separated by commas [default: 2,4,6,8]:  1,2,3,4
    Enter a number compute to [default: 24]:  


    2*3*4/1
    1*2*3*4
    4*(1 + 2 + 3)


d9f898f5abf528b45cb643eb4a6df90023210fdf
ipynb
Jupyter Notebook
24i.ipynb
tiggerntatie/24
49ad4d06230ee001518ff2bb41b24f2772b744c3
MIT
24i.ipynb
tiggerntatie/24
49ad4d06230ee001518ff2bb41b24f2772b744c3
MIT
24i.ipynb
tiggerntatie/24
49ad4d06230ee001518ff2bb41b24f2772b744c3
MIT
Qwen/Qwen-72B
1. YES
2. YES