📜  毕达哥拉斯三胞胎与给定的总和

📅  最后修改于: 2021-04-29 14:29:50             🧑  作者: Mango

毕达哥拉斯三胞胎是一组自然数,使得a a ^ 2 + b ^ 2 = c ^ 2 。例如3 ^ 2 + 4 ^ 2 = 5 ^ 2。
给定数字n,找到总和为n的毕达哥拉斯三重态。
例子 :

Input : n = 12
Output : 3, 4, 5
Note that 3, 4 and 5 is a Pythagorean Triplet
with sum equal to 12.

Input : n = 4.
Output : No Triplet
There does not exist a Pythagorean Triplet
with sum equal to 4.

一个简单的解决方案是运行三个嵌套循环以生成所有可能的三元组,对于每个三元组,检查它是否为毕达哥拉斯三元组并且已给出总和。该解决方案的时间复杂度为O(n 3 )。
一种有效的解决方案是运行两个循环,其中第一个循环从i = 1到n / 3,第二个循环从j = i + 1到n / 2。在第二个循环中,我们检查(n – i – j)是否等于i * i + j * j。

C++
// C++ program to find Pythagorean
// Triplet of given sum.
#include 
using namespace std;
 
void pythagoreanTriplet(int n)
{
    // Considering triplets in
    // sorted order. The value
    // of first element in sorted
    // triplet can be at-most n/3.
    for (int i = 1; i <= n / 3; i++)
    {
         
        // The value of second
        // element must be less
        // than equal to n/2
        for (int j = i + 1; j <= n / 2; j++)
        {
            int k = n - i - j;
            if (i * i + j * j == k * k)
            {
                cout << i << ", "
                     << j << ", "
                     << k;
                return;
            }
        }
    }
 
    cout << "No Triplet";
}
 
// Driver Code
int main()
{
    int n = 12;
    pythagoreanTriplet(n);
    return 0;
}


Java
// Java program to find Pythagorean 
// Triplet of given sum.
class GFG
{
    static void pythagoreanTriplet(int n)
    {
         
        // Considering triplets in
        // sorted order. The value
        // of first element in sorted
        // triplet can be at-most n/3.
        for (int i = 1; i <= n / 3; i++)
        {
             
            // The value of second element
            // must be less than equal to n/2
            for (int j = i + 1; j <= n / 2; j++)
            {
                int k = n - i - j;
                if (i * i + j * j == k * k)
                {
                    System.out.print(i + ", "+
                                j + ", " + k);
                    return;
                }
            }
        }
     
        System.out.print("No Triplet");
    }
     
    // Driver Code
    public static void main(String arg[])
    {
        int n = 12;
         
        pythagoreanTriplet(n);
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python3 program to find
# Pythagorean Triplet of
# given sum.
 
def pythagoreanTriplet(n):
 
    # Considering triplets in
    # sorted order. The value
    # of first element in sorted
    # triplet can be at-most n/3.
    for i in range(1, int(n / 3) + 1):
         
        # The value of second element
        # must be less than equal to n/2
        for j in range(i + 1,
                       int(n / 2) + 1):
 
            k = n - i - j
            if (i * i + j * j == k * k):
                print(i, ", ", j, ", ",
                               k, sep = "")
                return
     
    print("No Triplet")
     
# Driver Code
n = 12
pythagoreanTriplet(n)
 
# This code is contributed
# by Smitha Dinesh Semwal


C#
// C# program to find 
// Pythagorean Triplet
// of given sum.
using System;
 
class GFG
{
    static void pythagoreanTriplet(int n)
    {
         
        // Considering triplets in
        // sorted order. The value
        // of first element in sorted
        // triplet can be at-most n/3.
        for (int i = 1; i <= n / 3; i++)
        {
             
            // The value of second element
            // must be less than equal to n/2
            for (int j = i + 1;
                     j <= n / 2; j++)
            {
                int k = n - i - j;
                if (i * i + j * j == k * k)
                {
                    Console.Write(i + ", "+
                                  j + ", " + k);
                    return;
                }
            }
        }
     
        Console.Write("No Triplet");
    }
     
    // Driver Code
    public static void Main()
    {
        int n = 12;
         
        pythagoreanTriplet(n);
    }
}
 
// This code is contributed by Vt_m.


PHP


Javascript


输出 :
3, 4, 5