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

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.


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.