Problem 9 "Special Pythagorean Triple"
A Pythagorean triple (a natural number that satisfies the Pythagorean theorem) is a set of numbers that satisfy the following equation with a <b <c.
a^2 + b^2 = c^2
For example
3^2 + 4^2 = 9 + 16 = 25 = 5^2
. There is only one Pythagoras triplet with a + b + c = 1000. Calculate the product abc of these.
Python
n = 1000
seq = range(1, n+1)
def is_pythagorean(a, b, c):
return a**2 + b**2 == c**2
pythagorean = None
for a in seq:
if(pythagorean): break
for b in range(a+1, n+1):
c = n - b - a
if(c > b and is_pythagorean(a, b, c)):
pythagorean = (a, b, c)
break
result = reduce(lambda x,y: x*y, pythagorean)
print result
print result == 31875000
print pythagorean
result
31875000
True
(200, 375, 425)
Recommended Posts