I saw this question on the newspaper and can't solve it, help. Use the four operation sign, substitute them into the question marks between the digits such that the outcome is two(the order of operation doesn't applied, each operation is done one at a time) -7?6?22?2?-5?14?9=2
Asked
Active
Viewed 27 times
2
-
Try all possibilities using a computer program. – Yuval Filmus Sep 27 '14 at 05:50
-
What do you mean by "use the four operation sign"? Which operations are we allowed to use? – Yuval Filmus Sep 27 '14 at 05:50
-
I am pretty sure Nerdsome is referring to the four orerators $+, -, \times, \div$. – J. W. Perry Sep 27 '14 at 06:01
1 Answers
1
My computer program found two solutions: $$ (((((-7+6)*22)+2)/-5)+14)/9 = 2 \\ (((((-7-6)+22)/2)+-5)*14)+9 = 2 $$
Here is my sage program:
def combs(S,k):
if k == 0:
yield []
else:
for x in S:
for y in combs(S,k-1):
yield [x] + y
def op(x):
if x == '+': return lambda y,z:y+z
if x == '-': return lambda y,z:y-z
if x == '*': return lambda y,z:y*z
if x == '/': return lambda y,z:y/z
def app(O):
res = -7
for (oper,operand) in zip(O,[6,22,2,-5,14,9]):
res = op(oper)(res,operand)
return res
[comb for comb in combs('+-*/',6) if app(comb)==2]
The output is
[['+', '*', '+', '/', '+', '/'], ['-', '+', '/', '+', '*', '+']].
Yuval Filmus
- 57,157