Pages

Project Euler #9 with Python

While doing Project Euler exercise 9, I realized that python has no formal mechanism to break out of an outside loop. Here is my answer:

from itertools import count
p=False #to break or not to break
for b in count():
    for a in count():
        if a >= b: break
        c = (a**2 + b**2)**.5
        if a < b < c and a + b + c == 1000:
            p = True
            break
    if p:
        print a * b * c
        break

0 comments:

Post a Comment