📜  生成勾股三胞胎

📅  最后修改于: 2021-04-24 16:29:39             🧑  作者: Mango

毕达哥拉斯三联体是一组三个正整数a,b和c,使得a 2 + b 2 = c 2 。给定一个限制,则生成所有值小于给定限制的勾股三元组。

Input : limit = 20
Output : 3 4 5
         8 6 10
         5 12 13
         15 8 17
         12 16 20

一个简单的解决方案是使用三个嵌套循环生成小于给定限制的三元组。对于每个三元组,检查勾股条件是否为真,如果为真,则打印三元组。该解决方案的时间复杂度为O(limit 3 ),其中“ limit”为极限。

有效解决方案可以在O(k)时间内打印所有三元组,其中k是打印的三元组数。这个想法是使用毕达哥拉斯三重态的平方和关系,即a和b的平方相加等于c的平方,我们可以用m和n来表示这些数字,这样,

a = m2 - n2
       b = 2 * m * n
       c  = m2 + n2
because,
       a2 = m4 + n4 – 2 * m2 * n2
       b2 = 4 * m2 * n2
       c2 = m4 + n4 + 2* m2 * n2

我们可以看到a 2 + b 2 = c 2 ,因此除了迭代a,b和c外,我们还可以迭代m和n并生成这些三元组。

下面是上述想法的实现:

C++
// C++ program to generate pythagorean
// triplets smaller than a given limit
#include 
 
// Function to generate pythagorean
// triplets smaller than limit
void pythagoreanTriplets(int limit)
{
 
    // triplet: a^2 + b^2 = c^2
    int a, b, c = 0;
 
    // loop from 2 to max_limitit
    int m = 2;
 
    // Limiting c would limit
    // all a, b and c
    while (c < limit) {
 
        // now loop on j from 1 to i-1
        for (int n = 1; n < m; ++n) {
 
            // Evaluate and print triplets using
            // the relation between a, b and c
            a = m * m - n * n;
            b = 2 * m * n;
            c = m * m + n * n;
 
            if (c > limit)
                break;
 
            printf("%d %d %d\n", a, b, c);
        }
        m++;
    }
}
 
// Driver Code
int main()
{
    int limit = 20;
    pythagoreanTriplets(limit);
    return 0;
}


Java
// Java program to generate pythagorean
// triplets smaller than a given limit
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to generate pythagorean
    // triplets smaller than limit
    static void pythagoreanTriplets(int limit)
    {
 
        // triplet: a^2 + b^2 = c^2
        int a, b, c = 0;
 
        // loop from 2 to max_limitit
        int m = 2;
 
        // Limiting c would limit
        // all a, b and c
        while (c < limit) {
 
            // now loop on j from 1 to i-1
            for (int n = 1; n < m; ++n) {
                // Evaluate and print
                // triplets using
                // the relation between
                // a, b and c
                a = m * m - n * n;
                b = 2 * m * n;
                c = m * m + n * n;
 
                if (c > limit)
                    break;
 
                System.out.println(a + " " + b + " " + c);
            }
            m++;
        }
    }
 
    // Driver Code
    public static void main(String args[])
    {
        int limit = 20;
        pythagoreanTriplets(limit);
    }
}
 
// This code is contributed by Manish.


Python3
# Python3 program to generate pythagorean
# triplets smaller than a given limit
 
# Function to generate pythagorean
# triplets smaller than limit
def pythagoreanTriplets(limits) :
    c, m = 0, 2
 
    # Limiting c would limit
    # all a, b and c
    while c < limits :
         
        # Now loop on n from 1 to m-1
        for n in range(1, m) :
            a = m * m - n * n
            b = 2 * m * n
            c = m * m + n * n
 
            # if c is greater than
            # limit then break it
            if c > limits :
                break
 
            print(a, b, c)
 
        m = m + 1
 
 
# Driver Code
if __name__ == '__main__' :
     
    limit = 20
    pythagoreanTriplets(limit)
 
 
# This code is contributed by Shrikant13.


C#
// C# program to generate pythagorean
// triplets smaller than a given limit
using System;
 
class GFG {
 
    // Function to generate pythagorean
    // triplets smaller than limit
    static void pythagoreanTriplets(int limit)
    {
 
        // triplet: a^2 + b^2 = c^2
        int a, b, c = 0;
 
        // loop from 2 to max_limitit
        int m = 2;
 
        // Limiting c would limit
        // all a, b and c
        while (c < limit) {
 
            // now loop on j from 1 to i-1
            for (int n = 1; n < m; ++n)
            {
                 
                // Evaluate and print
                // triplets using
                // the relation between
                // a, b and c
                a = m * m - n * n;
                b = 2 * m * n;
                c = m * m + n * n;
 
                if (c > limit)
                    break;
 
                Console.WriteLine(a + " "
                            + b + " " + c);
            }
            m++;
        }
    }
 
    // Driver Code
    public static void Main()
    {
        int limit = 20;
        pythagoreanTriplets(limit);
    }
}
 
// This code is contributed by anuj_67.


PHP
 $limit)
                break;
 
            echo $a, " ", $b, " ", $c, "\n";
        }
        $m++;
    }
}
 
    // Driver Code
    $limit = 20;
    pythagoreanTriplets($limit);
 
// This code is contributed by ajit.
?>


Javascript


输出 :

3 4 5
8 6 10
5 12 13
15 8 17
12 16 20

此方法的时间复杂度为O(k),其中k是在给定限制下打印的三元组数(我们仅迭代m和n,每次迭代都打印三元组)

注意:以上方法不会生成所有小于给定限制的三元组。例如,上述方法未打印有效的三元组“ 9 12 15”。感谢Sid Agrawal指出了这一点。
参考:
https://zh.wikipedia.org/wiki/Formulas_for_generating_Pythagorean_triples