Project Euler Problem9

Special Pythagorean triplet

Problem 9

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,python

a 2 +  b 2 =  c 2

For example, 32 + 42 = 9 + 16 = 25 = 52.code

There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.blog

 
The code is simple:
a = 0
b = 0
c = 0
totalSum = 1000
cMax = int(totalSum/2)
cMin = int(totalSum/3)
aMax = cMin
c = cMax
while c > cMin:
    b = c - 1
    while b > 1:
        a = totalSum - c - b
        if a > b or a < 1 or a > aMax:
            break
        if a*a + b*b == c*c:
            print(a, b, c)
            print(a*b*c)
            break
        b -= 1
    c -= 1
相關文章
相關標籤/搜索