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

后端 未结 16 2633
离开以前
离开以前 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:07

    I think the best approach here is this:

    int n = 1000;
    unsigned long long b =0;
    unsigned long long c =0;
    for(int a =1;a

    explanation: We shall refer to the N and A constant so we will not have to use two loops. We can do it because c=n-a-b and b=(a^2-(a-n)^2)/(2(a-n)) I got these formulas by solving a system of equations:

    a+b+c=n, a^2+b^2=c^2

提交回复
热议问题