A Pythagorean triplet is a set of three positive integers a, b and c such that a2 + b2 = c2. Given a limit, generate all Pythagorean Triples with values smaller than given limit.
Input : limit = 20 Output : 3 4 5 8 6 10 5 12 13 15 8 17 12 16 20
A Simple Solution is to generate these triplets smaller than given limit using three nested loop. For every triplet, check if Pythagorean condition is true, if true, then print the triplet. Time complexity of this solution is O(limit3) where ‘limit’ is given limit.
An Efficient Solution can print all triplets in O(k) time where k is number of triplets printed. The idea is to use square sum relation of Pythagorean triplet, i.e., addition of squares of a and b is equal to square of c, we can write these number in terms of m and n such that,
a = m2 - n2 b = 2 * m * n c = m2 + n2 because, a2 = m4 + n4 – 2 * m2 * n2 b2 = 4 * m2 * n2 c2 = m4 + n4 + 2* m2 * n2
We can see that a2 + b2 = c2, so instead of iterating for a, b and c we can iterate for m and n and can generate these triplets.
Below is the implementation of above idea :
3 4 5 8 6 10 5 12 13 15 8 17 12 16 20
Time complexity of this approach is O(k) where k is number of triplets printed for a given limit (We iterate for m and n only and every iteration prints a triplet)
Note: The above method doesn’t generate all triplets smaller than a given limit. For example “9 12 15” which is a valid triplet is not printed by above method. Thanks to Sid Agrawal for pointing this out.
This article is contributed by Utkarsh Trivedi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
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.
- Twin Pythagorean triplets in an array
- Pythagorean Triplet with given sum
- Check if a number is a Pythagorean Prime or not
- Pythagorean Triplet with given sum using single loop
- Possible pairs forming a Pythagorean Triple with a given value
- Count number of triplets with product equal to given number with duplicates allowed
- Number of triplets such that each value is less than N and each pair sum is a multiple of K
- Count non-negative triplets with sum equal to N
- Find sum of xor of all unordered triplets of the array
- Total number of triplets (A, B, C) in which the points B and C are Equidistant to A
- Count of triplets (a, b, c) in the Array such that a divides b and b divides c
- Count of ordered triplets (R, G, B) in a given original string
- Count of triplets of numbers 1 to N such that middle element is always largest
- Count of triplets in an Array such that A[i] * A[j] = A[k] and i < j < k
- Reduce a given Binary Array to a single element by removal of Triplets
- Count of ways to split N into Triplets forming a Triangle
- Count number of triplets (a, b, c) such that a^2 + b^2 = c^2 and 1 <= a <= b <= c <= n
- Generate integer from 1 to 7 with equal probability
- Generate all unique partitions of an integer
- Generate all permutation of a set in Python