Project Euler 9

来源:互联网 发布:linux运行程序命令gcc 编辑:程序博客网 时间:2024/06/15 23:14
'''Created on 2014年9月1日Special Pythagorean tripletA Pythagorean triplet is a set of three natural numbers, a < b < c, for which,a2 + b2 = c2For 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 = 1000for 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

0 0