Pythagorean Triplet with given sum

A Pythagorean Triplet is a set of natural numbers such that a < b < c, for which a^2 + b^2 = c^2. For example, 3^2 + 4^2 = 5^2.

Given a number n, find a Pythagorean Triplet with sum as given n.

Examples :

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.



A simple solution is to run three nested loops to generate all possible triplets and for every triplet, check if it is a Pythagorean Triplet and has given sum. Time complexity of this solution is O(n3).

An efficient solution is to run two loops, where first loop runs from i = 1 to n/3, second loop runs from j = i+1 to n/2. In second loop, we check if (n – i – j) is equal to i * i + j * j.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find Pythagorean 
// Triplet of given sum.
#include <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find  
// Pythagorean Triplet 
// of given sum.
  
function pythagoreanTriplet($n)
{
    // Considering triplets in 
    // sorted order. The value 
    // of first element in sorted 
    // triplet can be at-most n/3.
    for ( $i = 1; $i <= $n / 3; $i++) 
    {
          
        // The value of second 
        // element must be less 
        // than equal to n/2
        for ( $j = $i + 1; $j <= $n / 2; $j++) 
        {
            $k = $n - $i - $j;
            if ($i * $i + $j * $j == $k * $k
            {
                echo $i , ", ", $j ,", ", $k;
                return;
            }
        }
    
  
    echo "No Triplet";
}
  
// Driver Code
$n = 12;
pythagoreanTriplet($n);
  
// This code is contributed by anuj_67.
?>

chevron_right


Output :

3, 4, 5


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : vt_m



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.