In [16]:
"""
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a^2 + b^2 = c^2
For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.
"""
def findPythagoreanTriplet(x):
"""Returns Pythagorean triplet(s) for limit x."""
# There's likely a better way with multisets, might look into
# http://code.activestate.com/recipes/218332-generator-for-integer-partitions/ for speed.
solutions = []
for a in range(1,x+1):
for b in range(a+1,x+1):
for c in range(b+1,x+1):
if((a**2 + b**2 == c**2) & (a + b + c == x)):
solutions.append([a, b, c])
print(a, b, c)
return solutions
findPythagoreanTriplet(1000)
Out[16]:
In [17]:
200*375*425
Out[17]: