# How to get $6$ from the numbers $\{6, 7, 8, 9\}$ using only addition, subtraction, division, and multiplication.

Is there a way you can get the number $$6$$ from the numbers $$6, 7, 8$$, and $$9$$ using only addition, subtraction, multiplication, and division, without combining two numbers e.g. using the $$6$$ and $$7$$ to create $$67$$. You may use parentheses and you can have to use each of the $$4$$ numbers once.

• Comments are not for extended discussion; this conversation has been moved to chat. – Aloizio Macedo Jun 30 at 16:26

The following disgraceful python code performs an exhaustive search and gives no solutions for 6. Thw closest hit is $$6+7/(8*9)\approx 6.097222$$ (or $$6-7/(8*9)$$). You can change line 29, if K==6: from 6 to any other number to find other solutions. For instance, it finds correctly that $$6/(7-9)+8=5$$ and $$(6+8)/(9-7)=7$$. You can also remove the last 3 lines to make it spit out all possible answers. For instance, I believe all solutions for 7 are (with some dupes)

(6+8)/(9-7)=7.0
(8+6)/(9-7)=7.0
9-(6+8)/7=7.0
9-((6+8)/7)=7.0
9-(8+6)/7=7.0
9-((8+6)/7)=7.0


I didn't bother with checking if two choices of bracketing resulted in the same expression. In particular, it checks $$4!\times 4^3\times 11=16896$$ expressions(if the code doesnt terminate first). I hard-coded all possible bracketings because I don't know yet how to code better, but these are all of them because of How many ways are there to put parentheses between $n$ numbers? . It seems like I can learn from this website which solves a very similar problem. Anyway, the code-

import itertools

numbers = "6789"
functions = "+-*/"

b = "("
B = ")"
found_six_flag = False
for n in itertools.permutations(numbers):
for f in itertools.product(functions,repeat=3) :
results = []
results.append(n[0]+f[0]+n[1]+f[1]+n[2]+f[2]+n[3] )      #1 a+b+c+d

results.append(b+n[0]+f[0]+n[1]+B+f[1]+n[2]+f[2]+n[3])   #2 (a+b)+c+d
results.append(b+n[0]+f[0]+n[1]+f[1]+n[2]+B+f[2]+n[3])    #3 (a+b+c)+d
results.append(n[0]+f[0]+b+n[1]+f[1]+n[2]+B+f[2]+n[3])    #4 a+(b+c)+d
results.append(n[0]+f[0]+b+n[1]+f[1]+n[2]+f[2]+n[3]+B)    #5 a+(b+c+d)
results.append(n[0]+f[0]+n[1]+f[1]+b+n[2]+f[2]+n[3]+B)    #6 a+b+(c+d)

results.append(b+n[0]+f[0]+n[1]+B+f[1]+b+n[2]+f[2]+n[3]+B)    #7 (a+b)+(c+d)
results.append(b+b+n[0]+f[0]+n[1]+B+f[1]+n[2]+B+f[2]+n[3])    #8 ((a+b)+c)+d
results.append(b+n[0]+f[0]+b+n[1]+f[1]+n[2]+B+B+f[2]+n[3])    #9 (a+(b+c))+d

results.append(n[0]+f[0]+b+b+n[1]+f[1]+n[2]+B+f[2]+n[3]+B)    #10 a+((b+c)+d)
results.append(n[0]+f[0]+b+n[1]+f[1]+b+n[2]+f[2]+n[3]+B+B)    #11 a+(b+(c+d))

for result in results:
K=eval(result)
if K==6:
found_six_flag = True
print(result+"="+str(K))
break
if found_six_flag:
break


You can compile this code on this website.

• you can use tio.run it works with many languages. – zwim Jun 30 at 15:46

Use Polish notation. Four numbers, each used only once. Three operators (out of 4). The sequence order matters. Operators can be repeated. That would be 20 groups of 3 operators (taken from 4, with possible repetitions), {+, -, ×}, {+, -, :}, {+, ×, :}, {-, ×, :}, {+, +, -} , {+, + ×} , {+, +, :}, {-, -, +}, {-, -, ×}, {-, -, :}, {×, ×, +}, {×, ×, -}, {×, ×, :}, {:, :, +}, {:, :, -}, {:, :, ×}, {+, +, +}, {-, -, -}, {×, ×, ×}, {:, :, :}. Now, for each group there are at most 6 permutations of these operators if they are distinct (or less if some operators are repeated). The four numbers are distinct, so we have 24 permutations in each case.

To be optimistic, you have at most 24 × 20 × 6 = 2880 Polish sequences to check.

I will do the first one.

+++6789 = 30 (not a 6). Well, you have at most 2879 more to go (a bit less actually). Good luck.

Edit. Following the comments, this analysis is incomplete , but a systematic approach (for the purpose of implementing a search algorithm) is possible. It won't be done here.

• How would parenthesis work in polish notation? For example: $6 + \frac{7}{8\cdot 9}$ ? Thanks! – Bruno Reis Jun 30 at 3:53
• The purpose of the Polish notation is to eliminate the need for parenthesis. Instead, the sequential order (from left to right ) of the operators and operands is essential. For example , the expression 6 + 7/(8×9) would become + : 7×896 . The operators "wait" until the operands are ready. (innermost operands first). Read the link. – Cristian Dumitrescu Jun 30 at 4:28
• Got it... Thanks! – Bruno Reis Jun 30 at 4:34
• Not so fast my friend, it wouldn't be too easy. In our example note that there is a difference between between + : × 7896 and + : 7 × 896. In my answer I missed that, the positioning of the 7 symbols (3 operators and 4 operands) makes a difference.. The problem can be solved though, with a more careful analysis. I won't bother, but I'll make a note in my answer. I didn't see that. Thanks. – Cristian Dumitrescu Jun 30 at 5:24
• It is worth pointing out that polish notation (or reverse polish notation) doesn't get every possible result, for example we cannot achieve $-30=-6-7-8-9$ – Bartek Jun 30 at 14:05

Below python code generates all possible positive integer combinations of these numbers together with all possible combinations of signs in all possible places with the help of the reverse polish notation. It is worth pointing out that we can't achieve certain negative results (e.g. $$-30=-6-7-8-9$$) as we cannot put minus at the beginning. However all possible combinations ($$4! \cdot C_3 \cdot 4^3 = 24 \cdot 5 \cdot 256 = 7680$$ to be exact) are checked quite quickly yielding $$127$$ positive integer solutions none of which are equal to $$6$$.

permutations = [[6,7,8,9],[6,7,9,8],[6,8,7,9],[6,8,9,7],[6,9,7,8],[6,9,8,7],
[7,6,8,9],[7,6,9,8],[7,8,6,9],[7,8,9,6],[7,9,6,8],[7,9,8,6],
[8,6,7,9],[8,6,9,7],[8,7,6,9],[8,7,9,6],[8,9,6,7],[8,9,7,6],
[9,6,7,8],[9,6,8,7],[9,7,6,8],[9,7,8,6],[9,8,6,7],[9,8,7,6]]
rpn = [[4,5,6],[3,5,6],[3,4,6],[2,5,6],[2,4,6]]
signs = [lambda x, y: x + y, lambda x, y: x - y, lambda x, y: x * y, lambda x, y: x / y]

stack = []
results = set()
for i in permutations:
for j in rpn:
for a in signs:
for b in signs:
for c in signs:
per = i.copy()
per.insert(j[0], a)
per.insert(j[1], b)
per.insert(j[2], c)
for k in per:
if(type(k) == int):
stack.append(k)
else:
p, q = stack.pop(), stack.pop()
stack.append(k(p,q))