-3

I'm learning to code in python using Project Euler. I've written the following program, which I think should work:

a=1
b=1
c=1

while(a<=998):
while(b<=998):
    c=(1000-(a+b)

    if (a*a+b*b==c*c):
        print a,b,c
    b=b+1
a=a+1

However, when I actually run the program from the terminal, the interpreter says that line 9

if (a*a+b*b==c*c):

is invalid. Can anyone tell me why this is?

thanks

4

2 回答 2

4

The immediately preceding line is missing a closing parenthesis:

    c=(1000-(a+b)
于 2013-03-30T20:59:40.737 回答
3

Assuming you are looking for Pythagorean triplets that satisfy a condition and you don't want triangles with negative sides:

for a in range(1, 999):
    for b in range(1, 1000 - a):
        c = 1000 - (a + b)
        if a * a + b * b == c * c:
            print a, b, c

And you can get the unique triplets in increasing order like this:

for a in range(1, 999):
    for b in range(a, 1000 - a):
        c = 1000 - (a + b)
        if a * a + b * b == c * c:
            print a, b, c

And there was a typo in your original code: c=(1000-(a+b)

于 2013-03-30T21:02:03.423 回答