Andy
Andy

Reputation: 3170

Python Script Hangs, Potentially an Infinite Loop?

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 advance!

Upvotes: 0

Views: 300

Answers (2)

aberry
aberry

Reputation: 447

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....

Upvotes: 0

Matthew Flaschen
Matthew Flaschen

Reputation: 284786

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

Upvotes: 3

Related Questions