Python
20 Jul 2011
 
 
 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/usr/bin/env python
"""
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.
"""
import math
def solve():
limit = 500
squares = [x*x for x in xrange(1, limit)]
for x in xrange(1, limit):
for y in xrange(1, limit):
product = x*x + y*y
if product in squares:
z = int(math.sqrt(product))
if x + y + z == 1000:
return x * y *z
if __name__ == '__main__':
print solve()