题目:A Pythagorean triplet is a set of three natural numbers, a b
c, for which,
a2 + b2 = c2
For example, 32 + 42 = 9 + 16 = 25 = 52.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.
中文:一个毕达哥拉斯三元组是一个包含三个自然数的集合,a<b<c,满足条件:
a2 + b2 = c2
例如:32 + 42 = 9 + 16 = 25 = 52.
已知存在并且只存在一个毕达哥拉斯三元组满足条件a + b + c = 1000。
找出该三元组中abc的乘积。
分析:
从a<b<c以及a + b + c = 1000的条件可以看出来a最大不会超过333,b不会超过500,因此代码如下:
for x in range(1,332): for y in range(x,499): z=(x*x+y*y)**0.5 if x+y+z==1000: print x*y*z