首页 > 其他 > 详细

Problem 9

时间:2019-06-01 10:21:40      阅读:57      评论:0      收藏:0      [点我收藏+]

Problem 9

# Problem_9.py
"""
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
pow(a, 2) + pow(b, 2) = pow(c, 2)
For example, pow(3, 2) + pow(4, 2) = 9 + 16 = 25 = pow(5, 2).
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.
找出满足a + b + c = 1000的毕达哥拉斯三元数组(Pythagorean triplet)
找出三数之积
"""
import math

ans = []
for a in range(1, 1000):
    for b in range(a, 1000):
        power = pow(a, 2) + pow(b, 2)
        c = math.sqrt(power)
        if c % int(c) != 0.0:
            continue
        c = int(c)
        print(a, b, c, a+b+c)
        if a+b+c == 1000:
            ans.extend([a, b, c])
            break

print(ans)
tot = 1
for i in ans:
    tot *= i
print(tot)

 

Problem 9

原文:https://www.cnblogs.com/noonjuan/p/10958645.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!