zoukankan      html  css  js  c++  java
  • Problem 9

    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)
    Resistance is Futile!
  • 相关阅读:
    PTA9
    PTA8
    第七周
    第六周
    第五周
    PTA4
    2019第三次作业
    第十周课程总结
    第九周课程总结&实验报告(七)
    第八周课程总结&实验报告(六)
  • 原文地址:https://www.cnblogs.com/noonjuan/p/10958645.html
Copyright © 2011-2022 走看看