'''
Created on 2014年9月1日
Special Pythagorean triplet
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.
@author: wxp2971
'''
# 最直接的方式,大概分析下范围
for a in range(1,900):
for b in range(a+1,900):
for c in range(b+1,900):
if ((a+b+c)==1000) and (a*a+b*b)==(c*c):
print(a,b,c)
print(a*b*c)
# 答案中的解答
# 直接的方式
s = 1000
for a in range(3,s-3):
for b in range(a+1,int((s-1-a)/2)):
flag = 0
c = s - a -b
if (a*a + b*b) == (c*c):
print(a,b,c)
print(a*b*c)
flag = 1
break
if flag:
break
Project Euler 9
最新推荐文章于 2019-08-27 19:40:04 发布