zoukankan      html  css  js  c++  java
  • Project Euler Problem9

    Special Pythagorean triplet

    Problem 9

    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.

     
    The code is simple:
    a = 0
    b = 0
    c = 0
    totalSum = 1000
    cMax = int(totalSum/2)
    cMin = int(totalSum/3)
    aMax = cMin
    c = cMax
    while c > cMin:
        b = c - 1
        while b > 1:
            a = totalSum - c - b
            if a > b or a < 1 or a > aMax:
                break
            if a*a + b*b == c*c:
                print(a, b, c)
                print(a*b*c)
                break
            b -= 1
        c -= 1
    

      

  • 相关阅读:
    第三天-基本数据类型 int bool str
    第二天-while循环 格式化输出 运算符 编码
    第一天-python基础
    Mysql
    Mysql
    Mysql
    Mysql
    Mysql
    Mysql
    Php
  • 原文地址:https://www.cnblogs.com/tianxiaozz/p/3471893.html
Copyright © 2011-2022 走看看