views:

71

answers:

2

Once again working on Project Euler, this time my script just hangs there. I'm pretty sure I'm letting it run for long enough, and my hand-trace (as my father calls it) yields no issues. Where am I going wrong?

I'm only including the relevant portion of the code, for once.

def main():
    f, n = 0, 20
    while f != 20:
        f = 0
        for x in range(1,21):
            if n % x != 0: break
            else: ++f
        if f == 20: print n
        n += 20

Thanks in advanced!

+3  A: 

Python doesn't have increment (++). It's interpreted as +(+(a)). + is the unary plus operator, which basically does nothing. Use += 1

Matthew Flaschen
I changed that as I was entering it in, I'll change it back. Thanks for the heads up. :)
Andrew
The expression `++f` means "positive positive f", in other words, f. You aren't incrementing your main loop variable.
Ned Batchelder
A: 

Here in your case 'f' value can never reach 20 and hence never exit 1) At 1st break (when n=20 and x =3) it again set f=0. Similarly for next loop also n get increased 20 but when 'x' is 3 again same f=0

So this will go in infinite loop....

aberry
It's not an infinite loop. It will stop when n is the least common multiple of the first 20 natural numbers.
Matthew Flaschen
@Matthew so perhaps my script is okay, but it hasn't run long enough? Because it still doesn't work. :(
Andrew
my apology for wrong ans...@ Matthew thanxloop stopped finally at 232792560 :)
aberry
I have no idea if the script is okay. But it does require over 11 million iterations to terminate.
Matthew Flaschen
In my case script get completed in appx 2 Minute time (exactly 118 secs) to find number 232792560
aberry