1+2-3*4/5=22
Published: Aug 3, 2024

Somebody asked in some channel:

“Given six numbers: 1, 2, 3, 4, 5, 22; and 5 operators: +, -, ×, ÷ and =. Is it possible to arrange these numbers and operators into a statement that is true? You must use all of the numbers and operators given.

Example: 1 + 2 - 3 × 4 ÷ 5 = 22 (not true)”

My approach

SPOILER ALERT!

At first I tried to sort the numbers and operators manually but with no luck.

I wrote a quick Python script to programmatically generate all possible statements and evaluate all of them.

 1import itertools
 2
 3def execute(num, ops):
 4    s = "float("
 5    ops += " "
 6    for i in range(6):
 7        a = num[i]
 8        b = ops[i]
 9
10        s += a + b
11
12        if b == "=":
13            s += "float("
14
15    s += ")"
16    s = s.replace("6", "22").replace("=", ")==")
17
18    left, right = s.split("==")
19    print(s, end="")
20    exec(f"print({s}, {left}, {right})")
21
22def iterate():
23    ops = '+-*/='
24    numbers = '123456'
25    perms = list(itertools.permutations(ops))
26    perms = [''.join(i) for i in perms]
27    nums = list(itertools.permutations(numbers))
28    nums = [''.join(i) for i in nums]
29    
30    for perm in perms:
31        for num in nums:
32            execute(num, perm)
33
34iterate()

The code is horribly unoptimized and ugly, but at least it gets the job done…

The code prints out a total of 86400 statements. After searching the output dump, it looks like none of them is true.

Afterwords

I wonder if these kinds of problems are possible to mathematically prove that there are no solutions (or otherwise)? If you do came up with a different approach, I would love to see it.


← Go to parent
Category: 
Tags:  math shitpost