Given a single integer n [1, 1000000000], generate a Pythagoras triplet which includes n as one of its sides if possible.
Input : 22 Output : Pythagoras Triplets exist i.e. 22 120 122 Input : 4 Output : Pythagoras Triplets exist i.e. 4 3 5 Input : 2 Output : No Pythagoras Triplet exists
Definition: “Pythagorean triplets” are integer solutions to the Pythagorean Theorem, i.e. they satisfy the equation
Our task is to generate a triplet from an integral value. This can be a confusing task because, the side given to us can be a hypotenuse or a non-hypotenuse side.
Starting to calculate triplets by putting them in a formula, it can be deduced that only for 1 and 2, no triplets are possible.
if n is even, our triplets are calculated by formula
if n is odd, our triplets are calculated by formula
Pythagoras Theorem can also be written as
i.e a*a = (c-b)(c+b)
a*a x 1 = a*a, thus and , this solution works if n is odd.
For even solution, , thus, we get the above formula when n is even.
Pythagoras Triplets exist i.e. 22 120 122
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Pythagorean Triplet with given sum using single loop
- Generate all unique partitions of an integer
- Generate integer from 1 to 7 with equal probability
- Minimum decrements to make integer A divisible by integer B
- Pythagorean Triplet with given sum
- Prime Triplet
- Triplet with no element divisible by 3 and sum N
- Sum of multiplication of triplet of divisors of a number
- Finding a Non Transitive Coprime Triplet in a Range
- Find a triplet in an array whose sum is closest to a given number
- Find distinct integers for a triplet with given product
- Count triplet of indices (i, j, k) such that XOR of elements between [i, j) equals [j, k]
- Count triplet pairs (A, B, C) of points in 2-D space that satisfy the given condition
- Find single Movement in a Matrix
- Maximum no. of apples that can be kept in a single basket
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.