 Open in App
Not now

# Pythagorean Triplet with given sum using single loop

• Last Updated : 18 Mar, 2023

A Pythagorean Triplet is a set of natural numbers such that a < b < c, for which Given a number N, find a Pythagorean Triplet with sum as given N or return -1.

Examples:

```Input: 12
Output: 3 4 5
Explanation:
As 32 + 42 = 52

Input: 82
Output: -1```

Approach: The idea is to find the value of b and c in terms of a and iterate a from 1 to N. To find the value of b and c in terms of a we have to do following:

We have two equations,  We will find the value of c in term of a and b Then put this value in equation 1 to solve for b.

From equation 2, Now, put this value in equation 1. After solving the above equation we will get,  Now, iterate a from 1 to N and calculate respectively the value of b and c Then, check if ## C++

 `// C++ program to find the Pythagorean``// Triplet with given sum``#include ``using` `namespace` `std;` `// Function to calculate the``// Pythagorean triplet in O(n)``void` `PythagoreanTriplet(``int` `n)``{``    ``int` `flag = 0;` `    ``// Iterate a from 1 to N-1.``    ``for` `(``int` `a = 1; a < n; a++)``    ``{``        ``// Calculate value of b``        ``int` `b = (n * n - 2 * n * a)``                ``/ (2 * n - 2 * a);` `        ``// The value of c = n - a - b``        ``int` `c = n - a - b;` `        ``if` `(a * a + b * b == c * c``            ``&& b > 0 && c > 0)``        ``{``            ``cout << a << ``" "` `<< b << ``" "` `<< c;``            ``flag = 1;``            ``break``;``        ``}``    ``}` `    ``if` `(flag == 0) {``        ``cout << ``"-1"``;``    ``}` `    ``return``;``}` `// Driver Code``int` `main()``{``    ``int` `N = 12;` `    ``// Function call``    ``PythagoreanTriplet(N);` `    ``return` `0;``}`

## Java

 `// Java program to find the Pythagorean``// Triplet with given sum` `class` `GFG {` `    ``// Function to calculate the``    ``// Pythagorean triplet in O(n)``    ``static` `void` `PythagoreanTriplet(``int` `n)``    ``{``        ``int` `flag = ``0``;` `        ``// Iterate a from 1 to N-1.``        ``for` `(``int` `a = ``1``; a < n; a++)``        ``{``            ``// Calculate value of b``            ``int` `b = (n * n - ``2` `* n * a)``              ``/ (``2` `* n - ``2` `* a);` `            ``// The value of c = n - a - b``            ``int` `c = n - a - b;` `            ``if` `(a * a + b * b == c * c``                ``&& b > ``0` `&& c > ``0``)``            ``{``                ``System.out``                  ``.print(a + ``" "` `+ b + ``" "` `+ c);``                ``flag = ``1``;``                ``break``;``            ``}``        ``}` `        ``if` `(flag == ``0``)``        ``{``            ``System.out.print(``"-1"``);``        ``}` `        ``return``;``    ``}``  ` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `N = ``12``;` `        ``// Function call``        ``PythagoreanTriplet(N);``    ``}``}` `// This code contributed by sapnasingh4991`

## Python3

 `# Python3 program to find the Pythagorean``# Triplet with a given sum` `# Function to calculate the``# Pythagorean triplet in O(n)`  `def` `PythagoreanTriplet(n):``    ``flag ``=` `0` `    ``# Iterate a from 1 to N-1.``    ``for` `a ``in` `range``(``1``, n, ``1``):` `        ``# Calculate value of b``        ``b ``=` `(n ``*` `n ``-` `2` `*` `n ``*` `a) ``/``/` `(``2` `*` `n ``-` `2` `*` `a)` `        ``# The value of c = n - a - b``        ``c ``=` `n ``-` `a ``-` `b` `        ``if` `(a ``*` `a ``+` `b ``*` `b ``=``=` `c ``*` `c``            ``and` `b > ``0` `and` `c > ``0``):``            ``print``(a, b, c)``            ``flag ``=` `1``            ``break` `    ``if``(flag ``=``=` `0``):``        ``print``(``"-1"``)` `    ``return`  `# Driver code``if` `__name__ ``=``=` `'__main__'``:``    ``N ``=` `12` `    ``# Function call``    ``PythagoreanTriplet(N)` `# This code is contributed by Bhupendra_Singh`

## C#

 `// C# program to find the Pythagorean``// Triplet with given sum``using` `System;` `class` `GFG {` `    ``// Function to calculate the``    ``// Pythagorean triplet in O(n)``    ``static` `void` `PythagoreanTriplet(``int` `n)``    ``{``        ``int` `flag = 0;` `        ``// Iterate a from 1 to N-1.``        ``for` `(``int` `a = 1; a < n; a++)``        ``{``            ``// Calculate value of b``            ``int` `b = (n * n - 2 * n * a)``              ``/ (2 * n - 2 * a);` `            ``// The value of c = n - a - b``            ``int` `c = n - a - b;``            ``if` `(a * a + b * b == c * c``                ``&& b > 0 && c > 0)``            ``{``                ``Console.Write(a + ``" "` `+ b + ``" "` `+ c);``                ``flag = 1;``                ``break``;``            ``}``        ``}` `        ``if` `(flag == 0) {``            ``Console.Write(``"-1"``);``        ``}``        ``return``;``    ``}` `    ``// Driver code``    ``public` `static` `void` `Main(String[] args)``    ``{``        ``int` `N = 12;` `        ``// Function call``        ``PythagoreanTriplet(N);``    ``}``}` `// This code is contributed by shivanisinghss2110`

## Javascript

 ``

Output

`3 4 5`

Time Complexity: O(N)

Auxiliary Space: O(1)

Approach:

Use a single loop to iterate over all possible values of the first number in the triplet, from 1 to the sum divided by 3 (since the sum of the three numbers must be at least three times the minimum value).
For each value of the first number, calculate the maximum value of the second number using the formula (sum – a) / 2, since the sum of the first two numbers must be less than the sum.
Use another loop to iterate over all possible values of the second number in the triplet, starting from the first number + 1 and up to the maximum value calculated in step 3.
Calculate the third number using the formula sum – a – b.
Check if the three numbers form a Pythagorean triplet using the formula a**2 + b**2 == c**2.
If the condition is satisfied, print the values of the triplet and exit the loop.

## Python3

 `def` `find_pythagorean_triplet(``sum``):``    ``for` `a ``in` `range``(``1``, ``sum` `/``/` `3` `+` `1``):``        ``b_max ``=` `(``sum` `-` `a) ``/``/` `2``        ``for` `b ``in` `range``(a ``+` `1``, b_max ``+` `1``):``            ``c ``=` `sum` `-` `a ``-` `b``            ``if` `a``*``*``2` `+` `b``*``*``2` `=``=` `c``*``*``2``:``                ``return` `(a, b, c)``    ``return` `None` `# Example usage:``triplet ``=` `find_pythagorean_triplet(``1000``)``if` `triplet:``    ``print``(triplet)  ``# Output: (200, 375, 425)``else``:``    ``print``(``"No Pythagorean triplet found."``)`

Output

`(200, 375, 425)`

Time complexity:

Time complexity of the function is O(sum^2).

Auxiliary Space: O(1).

My Personal Notes arrow_drop_up