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.
import itertools
def execute(num, ops):
s = "float("
ops += " "
for i in range(6):
a = num[i]
b = ops[i]
s += a + b
if b == "=":
s += "float("
s += ")"
s = s.replace("6", "22").replace("=", ")==")
left, right = s.split("==")
print(s, end="")
exec(f"print({s}, {left}, {right})")
def iterate():
ops = '+-*/='
numbers = '123456'
perms = list(itertools.permutations(ops))
perms = [''.join(i) for i in perms]
nums = list(itertools.permutations(numbers))
nums = [''.join(i) for i in nums]
for perm in perms:
for num in nums:
execute(num, perm)
iterate()
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.