0

I made this script for finding the solution to a puzzle. I had to put the numbers 1 to 9 through some arithmetic to get 66. But for some reason I get a list of approximates that include 68.5333333333, 68.6666666667, 69.6 and so on.

Here is my code (I'd be very grateful for peer review as well if that's ok):

def equation():
    # intertools gives me the posibility to permutate a list of numbers, floating points for this case so I have decimals
    import itertools
    permutationlist = list(itertools.permutations([1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0]))
    # I make a loop for every permutation, the last number is 9! for the last one
    for n in xrange (0,362880):
        currentlist = permutationlist[n]
        # every variable is taken for each item in the current list
        a = currentlist[0]
        b = currentlist[1]
        c = currentlist[2]
        d = currentlist[3]
        e = currentlist[4]
        f = currentlist[5]
        g = currentlist[6]
        h = currentlist[7]
        i = currentlist[8]
        # if statement to pick up what computes to 66
        # here is the problem, what part of == is not clear to Python?
        if ((((((((((((a+13)*b)//c)+d)+12)*e)-f)-11)+g)*h)//i)-10) == 66:
            print ('[%s]+13*[%s]/[%s]+[%s]+12*[%s]-[%s]-11+[%s]*[%s]/[%s]-10 =') % (a,b,c,d,e,f,g,h,i), ((((((((((((a+13)*b)/c)+d)+12)*e)-f)-11)+g)*h)/i)-10)
        else:
            pass
        # next list...
        n = n + 1

equation()
Ronen
  • 335
  • 4
  • 11
  • 2
    `((((((((((((` - what the hell? – ThiefMaster May 31 '15 at 19:19
  • 1
    @ThiefMaster python is getting confused probably – ha9u63a7 May 31 '15 at 19:19
  • 2
    `//` works as integer division only if both operands are integers. – Stefano Sanfilippo May 31 '15 at 19:21
  • 1
    remove `n = n + 1` please. Furthemore, you do not need to loop on the range nor use n as a index on permutationslist at all. Just do `for currentlist in permutationslist` instead. – Pynchia May 31 '15 at 19:26
  • 1
    You use `//` when computing the equality, but `/` when computing what to print. This isn't a duplicate of what it's been closed as a duplicate of, but I'm leaving it closed anyway, since it'd just be reclosed as a typo. – user2357112 May 31 '15 at 19:42
  • @user2357112 you solved my confusion. Now I know that the problem is floating point arithmetics and not the double equal. @ Pynchia I corrected that, thanks. I searched for previous questions but I didn't learn what I should do to correct this in Python. Should I learn a scripting language for math? – Ronen Jun 01 '15 at 07:03

0 Answers0