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 '19 at 16:26
3 Answers
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.
- 34,903
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.
-
1How would parenthesis work in polish notation? For example: $6 + \frac{7}{8\cdot 9}$ ? Thanks! – Bruno Reis Jun 30 '19 at 03:53
-
2The 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 '19 at 04:28
-
-
No problem. This would be a systematic way to look at this type of problems. – Cristian Dumitrescu Jun 30 '19 at 04:38
-
Yes! It seems like a really good approach. With that, it's kinda easy to create a brute force algorithm to check the solutions (as Calvin Khor did). – Bruno Reis Jun 30 '19 at 04:55
-
2Not 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 '19 at 05:24
-
The search space would become much bigger, because now you have to distribute the operators and operands in all possible positions, where the expression makes sense. I 'm sure you can find shortcuts and simplifying criteria, though. – Cristian Dumitrescu Jun 30 '19 at 05:33
-
That makes sense... I was trying to prove that there is not a solution for 6, but it's too hard since it's hard to formalize what's going on with all the permutations – Bruno Reis Jun 30 '19 at 05:38
-
No, it's not too that hard either , it's perfectly doable on a PC. For each of the (at most) 2880 cases considered in my answer, you have 35 possible combinations (combinations of 7 positions taken 3 at a time). These are the ways you can distribute the operators in the 7 length string of operators and operands. Spme of them will be parsable, some not (some won't make sense). In total the search space will be at most 2880 × 35 = 100800 possible configurations. Perfectly doable on a computer, and you can guarantee that you didn't miss anything. – Cristian Dumitrescu Jun 30 '19 at 05:53
-
-
Philosophically speaking. In a mathematical proof, you start from a system of axioms, follow the logical inference laws, and reach your conclusions in finite time. This is a Turing machine that halts in finite time (and it's more than figure of speech, it can be done in ZFC). I look at an algorithm (computer program) that halts in finite time as mathematical proof. And some problems just don't have short mathematical proofs. – Cristian Dumitrescu Jun 30 '19 at 06:09
-
I get your point of view and it's valid and makes sense. In the case of the problem that we're talking about, for me, the most direct proof would be an algorithm either. – Bruno Reis Jun 30 '19 at 06:11
-
1It 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 '19 at 14:05
-
You have to be careful how you define the operands. I work with strings of 7 objects (3 operators and 4 operands), where the operands are in the set {6, 7, 8, 9}. You now work with the set of operands {6, -6, 7, -7, 8, -8, 9, -9}. The sequential order and positioning of the operators and operands is essential (see the comments above). The algorithm can be easily extended for this case. The Polish notation doesn't miss anything if a careful analysis is employed. Thank you for the feedback @Bartek – Cristian Dumitrescu Jun 30 '19 at 18:45
-
And in the second comment in this section I meant innermost operators first. – Cristian Dumitrescu Jun 30 '19 at 19:01
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))
results.add(stack.pop())
results = sorted(results)
for i in results:
if(i == int(i) and i > 0):
print(int(i))
- 2,475
-
I've studied your code over many small breaks and I think I finally understand why it works and why its correct, thank you very much. – Calvin Khor Jul 02 '19 at 19:47