Generate Pythagorean Triplets
Last Updated :
12 Oct, 2023
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 :
C++
#include <bits/stdc++.h>
void pythagoreanTriplets( int limit)
{
int a, b, c = 0;
int m = 2;
while (c < limit) {
for ( int n = 1; n < m; ++n) {
a = m * m - n * n;
b = 2 * m * n;
c = m * m + n * n;
if (c > limit)
break ;
printf ( "%d %d %d\n" , a, b, c);
}
m++;
}
}
int main()
{
int limit = 20;
pythagoreanTriplets(limit);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static void pythagoreanTriplets( int limit)
{
int a, b, c = 0 ;
int m = 2 ;
while (c < limit) {
for ( int n = 1 ; n < m; ++n) {
a = m * m - n * n;
b = 2 * m * n;
c = m * m + n * n;
if (c > limit)
break ;
System.out.println(a + " " + b + " " + c);
}
m++;
}
}
public static void main(String args[])
{
int limit = 20 ;
pythagoreanTriplets(limit);
}
}
|
Python3
def pythagoreanTriplets(limits) :
c, m = 0 , 2
while c < limits :
for n in range ( 1 , m) :
a = m * m - n * n
b = 2 * m * n
c = m * m + n * n
if c > limits :
break
print (a, b, c)
m = m + 1
if __name__ = = '__main__' :
limit = 20
pythagoreanTriplets(limit)
|
C#
using System;
class GFG {
static void pythagoreanTriplets( int limit)
{
int a, b, c = 0;
int m = 2;
while (c < limit) {
for ( int n = 1; n < m; ++n)
{
a = m * m - n * n;
b = 2 * m * n;
c = m * m + n * n;
if (c > limit)
break ;
Console.WriteLine(a + " "
+ b + " " + c);
}
m++;
}
}
public static void Main()
{
int limit = 20;
pythagoreanTriplets(limit);
}
}
|
PHP
<?php
function pythagoreanTriplets( $limit )
{
$a ;
$b ;
$c =0;
$m = 2;
while ( $c < $limit )
{
for ( $n = 1; $n < $m ; ++ $n )
{
$a = $m * $m - $n * $n ;
$b = 2 * $m * $n ;
$c = $m * $m + $n * $n ;
if ( $c > $limit )
break ;
echo $a , " " , $b , " " , $c , "\n" ;
}
$m ++;
}
}
$limit = 20;
pythagoreanTriplets( $limit );
?>
|
Javascript
<script>
function pythagoreanTriplets(limit)
{
let a, b, c = 0;
let m = 2;
while (c < limit)
{
for (let n = 1; n < m; ++n)
{
a = m * m - n * n;
b = 2 * m * n;
c = m * m + n * n;
if (c > limit)
break ;
document.write(a + " " + b +
" " + c + "</br>" );
}
m++;
}
}
let limit = 20;
pythagoreanTriplets(limit);
</script>
|
Output
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)
Auxiliary space: O(1) as it is using constant space for variables
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.
References:
https://en.wikipedia.org/wiki/Formulas_for_generating_Pythagorean_triples
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...