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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Pythagorean Triplet with given sum using single loop
- Generate integer from 1 to 7 with equal probability
- Generate all unique partitions of an integer
- Minimum decrements to make integer A divisible by integer B
- Prime Triplet
- Pythagorean Triplet with given sum
- Finding a Non Transitive Coprime Triplet in a Range
- Sum of multiplication of triplet of divisors of a number
- Triplet with no element divisible by 3 and sum N
- Count triplet pairs (A, B, C) of points in 2-D space that satisfy the given condition
- 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]
- Puzzle | Place numbers 1 to 9 in a Circle such that sum of every triplet in straight line is 15
- Largest lexicographic triplet from a given Array that forms a triangle
- Maximum value of expression (arr[i] + arr[j] * arr[k]) formed from a valid Triplet
- Check if every vertex triplet in graph contains two vertices connected to third vertex
- Minimum possible sum of prices of a Triplet from the given Array
- Finding sum of digits of a number until sum becomes single digit
- Check whether a number can be expressed as a product of single digit numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.