Find Pythagorean triplet for which a + b + c = 1000

后端 未结 16 2621
离开以前
离开以前 2020-12-24 13:13

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a2 + b2 = c2

For example, 32 + 4

16条回答
  •  自闭症患者
    2020-12-24 14:08

    As mentioned above, ^ is bitwise xor, not power.

    You can also remove the third loop, and instead use c = 1000-a-b; and optimize this a little.

    Pseudocode

    for a in 1..1000
        for b in a+1..1000
            c=1000-a-b
            print a, b, c if a*a+b*b=c*c
    

提交回复
热议问题