Posted on by Kalkicode
Code Mathematics

# Generating Pythagorean Triples

Pythagorean triples are sets of three integers that satisfy the Pythagorean theorem, a² + b² = c², where c is the hypotenuse (the longest side) of a right-angled triangle and a and b are the lengths of the other two sides. Here's one way to generate Pythagorean triples:

1. Start with two integers, m and n, where m > n > 0. These will be the parameters of the algorithm.

2. Calculate three numbers: a = m² - n², b = 2mn, and c = m² + n².

3. Check if a, b, and c form a Pythagorean triple: if a² + b² = c², then they do.

4. If a, b, and c are a Pythagorean triple, output them. If not, go back to step 1 and try a different pair of values for m and n.

5. Repeat the process until you have generated all the Pythagorean triples you need.

Here's an example of this algorithm in action:

a = 2² - 1² = 3 b = 2 × 1 × 2 = 4 c = 2² + 1² = 5

Since a² + b² = 3² + 4² = 9 + 16 = 25 = c², (3, 4, 5) is a Pythagorean triple.

Let's try again with m = 3 and n = 2:

a = 3² - 2² = 5 b = 2 × 3 × 2 = 12 c = 3² + 2² = 13

Since a² + b² = 5² + 12² = 25 + 144 = 169 = c², (5, 12, 13) is a Pythagorean triple.

We can continue this process to generate more Pythagorean triples.

Here given code implementation process.

``````//C Program
//Generating Pythagorean Triples
#include <stdio.h>

//Dispaly pythagorean triples under the given limit
void pythagorean_triples(int limit)
{
//Loop controlling variables
int a = 0;
int b = 0;
int c = 0;
//Display Pythagorean Triples
for (a = 1; a <= limit; ++a)
{
for (b = a; b <= limit; ++b)
{
for (c = b; c <= limit; ++c)
{
if ((a * a) + (b * b) == (c * c))
{
//Display result
printf("%d² +  %d²  =  %d²\n", a, b, c);
}
}
}
}
}
int main()
{
//Test case
pythagorean_triples(30);
return 0;
}``````

#### Output

``````3² +  4²  =  5²
5² +  12²  =  13²
6² +  8²  =  10²
7² +  24²  =  25²
8² +  15²  =  17²
9² +  12²  =  15²
10² +  24²  =  26²
12² +  16²  =  20²
15² +  20²  =  25²
18² +  24²  =  30²
20² +  21²  =  29²``````
``````// Java program
// Generating Pythagorean Triples
class Triples
{
//Dispaly pythagorean triples under the given limit
public void pythagorean_triples(int limit)
{
//Loop controlling variables
int a = 0;
int b = 0;
int c = 0;
//Display Pythagorean Triples
for (a = 1; a <= limit; ++a)
{
for (b = a; b <= limit; ++b)
{
for (c = b; c <= limit; ++c)
{
if ((a * a) + (b * b) == (c * c))
{
//Display result
System.out.print("\n" + a + "² + " + b + "² = " + c + "²");
}
}
}
}
}
public static void main(String[] args)
{
Triples obj = new Triples();
//Test Case
obj.pythagorean_triples(30);
}
}``````

#### Output

``````3² + 4² = 5²
5² + 12² = 13²
6² + 8² = 10²
7² + 24² = 25²
8² + 15² = 17²
9² + 12² = 15²
10² + 24² = 26²
12² + 16² = 20²
15² + 20² = 25²
18² + 24² = 30²
20² + 21² = 29²``````
``````//Include header file
#include <iostream>

using namespace std;
// C++ program
// Generating Pythagorean Triples
class Triples
{
public:
//Dispaly pythagorean triples under the given limit
void pythagorean_triples(int limit)
{
//Loop controlling variables
int a = 0;
int b = 0;
int c = 0;
//Display Pythagorean Triples
for (a = 1; a <= limit; ++a)
{
for (b = a; b <= limit; ++b)
{
for (c = b; c <= limit; ++c)
{
if ((a * a) + (b * b) == (c * c))
{
//Display result
cout << "\n" << a << "² + " << b << "² = " << c << "²";
}
}
}
}
}
};
int main()
{
Triples obj = Triples();
//Test Case
obj.pythagorean_triples(30);
return 0;
}``````

#### Output

``````3² + 4² = 5²
5² + 12² = 13²
6² + 8² = 10²
7² + 24² = 25²
8² + 15² = 17²
9² + 12² = 15²
10² + 24² = 26²
12² + 16² = 20²
15² + 20² = 25²
18² + 24² = 30²
20² + 21² = 29²``````
``````//Include namespace system
using System;
// C# program
// Generating Pythagorean Triples
class Triples
{
//Dispaly pythagorean triples under the given limit
public void pythagorean_triples(int limit)
{
//Loop controlling variables
int a = 0;
int b = 0;
int c = 0;
//Display Pythagorean Triples
for (a = 1; a <= limit; ++a)
{
for (b = a; b <= limit; ++b)
{
for (c = b; c <= limit; ++c)
{
if ((a * a) + (b * b) == (c * c))
{
//Display result
Console.Write("\n" + a + "² + " + b + "² = " + c + "²");
}
}
}
}
}
public static void Main(String[] args)
{
Triples obj = new Triples();
//Test Case
obj.pythagorean_triples(30);
}
}``````

#### Output

``````3² + 4² = 5²
5² + 12² = 13²
6² + 8² = 10²
7² + 24² = 25²
8² + 15² = 17²
9² + 12² = 15²
10² + 24² = 26²
12² + 16² = 20²
15² + 20² = 25²
18² + 24² = 30²
20² + 21² = 29²``````
``````<?php

// Php program
// Generating Pythagorean Triples

class Triples
{
//Dispaly pythagorean triples under the given limit
public	function pythagorean_triples(\$limit)
{
//Loop controlling variables
\$a = 0;
\$b = 0;
\$c = 0;
//Display Pythagorean Triples
for (\$a = 1; \$a <= \$limit; ++\$a)
{
for (\$b = \$a; \$b <= \$limit; ++\$b)
{
for (\$c = \$b; \$c <= \$limit; ++\$c)
{
if ((\$a * \$a) + (\$b * \$b) == (\$c * \$c))
{
//Display result
echo "\n". \$a ."² + ". \$b ."² = ". \$c ."²";
}
}
}
}
}
}

function main()
{
\$obj = new Triples();
//Test Case
\$obj->pythagorean_triples(30);
}
main();``````

#### Output

``````3² + 4² = 5²
5² + 12² = 13²
6² + 8² = 10²
7² + 24² = 25²
8² + 15² = 17²
9² + 12² = 15²
10² + 24² = 26²
12² + 16² = 20²
15² + 20² = 25²
18² + 24² = 30²
20² + 21² = 29²``````
``````// Node Js program
// Generating Pythagorean Triples
class Triples
{
//Dispaly pythagorean triples under the given limit
pythagorean_triples(limit)
{
//Loop controlling variables
var a = 0;
var b = 0;
var c = 0;
//Display Pythagorean Triples
for (a = 1; a <= limit; ++a)
{
for (b = a; b <= limit; ++b)
{
for (c = b; c <= limit; ++c)
{
if ((a * a) + (b * b) == (c * c))
{
//Display result
process.stdout.write("\n" + a + "² + " + b + "² = " + c + "²");
}
}
}
}
}
}

function main()
{
var obj = new Triples();
//Test Case
obj.pythagorean_triples(30);
}
main();``````

#### Output

``````3² + 4² = 5²
5² + 12² = 13²
6² + 8² = 10²
7² + 24² = 25²
8² + 15² = 17²
9² + 12² = 15²
10² + 24² = 26²
12² + 16² = 20²
15² + 20² = 25²
18² + 24² = 30²
20² + 21² = 29²``````
``````#  Python 3 program
#  Generating Pythagorean Triples
class Triples :
# Dispaly pythagorean triples under the given limit
def pythagorean_triples(self, limit) :
# Loop controlling variables
a = 1
b = 0
c = 0
# Display Pythagorean Triples
while (a <= limit) :
b = a
while (b <= limit) :
c = b
while (c <= limit) :
if ((a * a) + (b * b) == (c * c)) :
# Display result
print("\n{0}² + {1}² = {2}".format(a,b,c), end = "")
c += 1
b += 1
a += 1

def main() :
obj = Triples()
# Test Case
obj.pythagorean_triples(30)

if __name__ == "__main__": main()``````

#### Output

``````3² + 4² = 5
5² + 12² = 13
6² + 8² = 10
7² + 24² = 25
8² + 15² = 17
9² + 12² = 15
10² + 24² = 26
12² + 16² = 20
15² + 20² = 25
18² + 24² = 30
20² + 21² = 29``````
``````#  Ruby program
#  Generating Pythagorean Triples
class Triples

# Dispaly pythagorean triples under the given limit
def pythagorean_triples(limit)

# Loop controlling variables
a = 1
b = 0
c = 0
# Display Pythagorean Triples
while (a <= limit)

b = a
while (b <= limit)

c = b
while (c <= limit)

if ((a * a) + (b * b) == (c * c))

# Display result
print("\n", a ,"² + ", b ,"² = ", c ,"²")
end
c += 1
end
b += 1
end
a += 1
end
end
end
def main()

obj = Triples.new()
# Test Case
obj.pythagorean_triples(30)
end
main()``````

#### Output

``````3² + 4² = 5²
5² + 12² = 13²
6² + 8² = 10²
7² + 24² = 25²
8² + 15² = 17²
9² + 12² = 15²
10² + 24² = 26²
12² + 16² = 20²
15² + 20² = 25²
18² + 24² = 30²
20² + 21² = 29²``````
``````// Scala program
// Generating Pythagorean Triples
class Triples
{
//Dispaly pythagorean triples under the given limit
def pythagorean_triples(limit: Int): Unit = {
//Loop controlling variables
var a: Int = 1;
var b: Int = 0;
var c: Int = 0;
//Display Pythagorean Triples
while (a <= limit)
{
b = a;
while (b <= limit)
{
c = b;
while (c <= limit)
{
if ((a * a) + (b * b) == (c * c))
{
//Display result
print("\n" + a + "² + " + b + "² = " + c + "²");
}
c += 1;
}
b += 1;
}
a += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: Triples = new Triples();
//Test Case
obj.pythagorean_triples(30);
}
}``````

#### Output

``````3² + 4² = 5²
5² + 12² = 13²
6² + 8² = 10²
7² + 24² = 25²
8² + 15² = 17²
9² + 12² = 15²
10² + 24² = 26²
12² + 16² = 20²
15² + 20² = 25²
18² + 24² = 30²
20² + 21² = 29²``````
``````// Swift program
// Generating Pythagorean Triples
class Triples
{
//Dispaly pythagorean triples under the given limit
func pythagorean_triples(_ limit: Int)
{
//Loop controlling variables
var a: Int = 1;
var b: Int = 0;
var c: Int = 0;
//Display Pythagorean Triples
while (a <= limit)
{
b = a;
while (b <= limit)
{
c = b;
while (c <= limit)
{
if ((a * a) + (b * b) == (c * c))
{
//Display result
print("\(a)² + \(b)² = \(c)");
}
c += 1;
}
b += 1;
}
a += 1;
}
}
}
func main()
{
let obj: Triples = Triples();
//Test Case
obj.pythagorean_triples(30);
}
main();``````

#### Output

``````3² + 4² = 5
5² + 12² = 13
6² + 8² = 10
7² + 24² = 25
8² + 15² = 17
9² + 12² = 15
10² + 24² = 26
12² + 16² = 20
15² + 20² = 25
18² + 24² = 30
20² + 21² = 29``````

Above approach is very simple. This are detect each pair under given range that are tack O(n^3) time. When each pair are not important then we can solve this problem in this ways.

``````//C Program
//Generating Pythagorean Triples
#include <stdio.h>

//Dispaly pythagorean triples under the given limit
void pythagorean_triples(int limit)
{
//Loop controlling variables
int a = 0;
int b = 0;
int c = 0;
int m = 2;
int n = 1;
//Execute loop, until the value of c are not exceed given limit
while (c <= limit)
{
//Check that valid condition
if (n < m)
{
// calcuate a = m² + n²
a = (m * m) - (n * n);
// calcuate b = 2mn
b = 2 * m * n;
//calcuate a = n² + m²
c = (n * n) + (m * m);
if (c <= limit)
{
//Display result
printf("%d² +  %d²  =  %d²\n", a, b, c);
}
//increasing the n value by one
n++;
}
else
{
//Reset the state of variable
n = 1;
m = m + 1;
}
}
}
int main()
{
//Test case
pythagorean_triples(30);
return 0;
}``````

#### Output

``````3² +  4²  =  5²
8² +  6²  =  10²
5² +  12²  =  13²
15² +  8²  =  17²
12² +  16²  =  20²
7² +  24²  =  25²
24² +  10²  =  26²
21² +  20²  =  29²``````
``````// Java program
// Generating Pythagorean Triples
class Triples
{
//Dispaly pythagorean triples under the given limit
public void pythagorean_triples(int limit)
{
//Loop controlling variables
int a = 0;
int b = 0;
int c = 0;
// set initial m and n
int m = 2;
int n = 1;
//Execute loop, until the value of c are not exceed given limit
while (c <= limit)
{
//Check that valid condition
if (n < m)
{
// calcuate a = m² + n²
a = (m * m) - (n * n);
// calcuate b = 2mn
b = 2 * m * n;
//calcuate a = n² + m²
c = (n * n) + (m * m);
if (c <= limit)
{
//Display result
System.out.print("" + a + "² + " + b + "² = " + c + "²\n");
}
//increasing the n value by one
n++;
}
else
{
//Reset the state of variable
n = 1;
m = m + 1;
}
}
}
public static void main(String[] args)
{
Triples obj = new Triples();
//Test Case
obj.pythagorean_triples(30);
}
}``````

#### Output

``````3² + 4² = 5²
8² + 6² = 10²
5² + 12² = 13²
15² + 8² = 17²
12² + 16² = 20²
7² + 24² = 25²
24² + 10² = 26²
21² + 20² = 29²``````
``````//Include header file
#include <iostream>

using namespace std;
// C++ program
// Generating Pythagorean Triples
class Triples
{
public:
//Dispaly pythagorean triples under the given limit
void pythagorean_triples(int limit)
{
//Loop controlling variables
int a = 0;
int b = 0;
int c = 0;
// set initial m and n
int m = 2;
int n = 1;
//Execute loop, until the value of c are not exceed given limit
while (c <= limit)
{
//Check that valid condition
if (n < m)
{
// calcuate a = m² + n²
a = (m * m) - (n * n);
// calcuate b = 2mn
b = 2 * m * n;
//calcuate a = n² + m²
c = (n * n) + (m * m);
if (c <= limit)
{
//Display result
cout << "" << a << "² + " << b << "² = " << c << "²\n";
}
//increasing the n value by one
n++;
}
else
{
//Reset the state of variable
n = 1;
m = m + 1;
}
}
}
};
int main()
{
Triples obj = Triples();
//Test Case
obj.pythagorean_triples(30);
return 0;
}``````

#### Output

``````3² + 4² = 5²
8² + 6² = 10²
5² + 12² = 13²
15² + 8² = 17²
12² + 16² = 20²
7² + 24² = 25²
24² + 10² = 26²
21² + 20² = 29²``````
``````//Include namespace system
using System;
// C# program
// Generating Pythagorean Triples
class Triples
{
//Dispaly pythagorean triples under the given limit
public void pythagorean_triples(int limit)
{
//Loop controlling variables
int a = 0;
int b = 0;
int c = 0;
// set initial m and n
int m = 2;
int n = 1;
//Execute loop, until the value of c are not exceed given limit
while (c <= limit)
{
//Check that valid condition
if (n < m)
{
// calcuate a = m² + n²
a = (m * m) - (n * n);
// calcuate b = 2mn
b = 2 * m * n;
//calcuate a = n² + m²
c = (n * n) + (m * m);
if (c <= limit)
{
//Display result
Console.Write("" + a + "² + " + b + "² = " + c + "²\n");
}
//increasing the n value by one
n++;
}
else
{
//Reset the state of variable
n = 1;
m = m + 1;
}
}
}
public static void Main(String[] args)
{
Triples obj = new Triples();
//Test Case
obj.pythagorean_triples(30);
}
}``````

#### Output

``````3² + 4² = 5²
8² + 6² = 10²
5² + 12² = 13²
15² + 8² = 17²
12² + 16² = 20²
7² + 24² = 25²
24² + 10² = 26²
21² + 20² = 29²``````
``````<?php
// Php program
// Generating Pythagorean Triples
class Triples
{
//Dispaly pythagorean triples under the given limit
public	function pythagorean_triples(\$limit)
{
//Loop controlling variables
\$a = 0;
\$b = 0;
\$c = 0;
// set initial m and n
\$m = 2;
\$n = 1;
//Execute loop, until the value of c are not exceed given limit
while (\$c <= \$limit)
{
//Check that valid condition
if (\$n < \$m)
{
// calcuate a = m² + n²
\$a = (\$m * \$m) - (\$n * \$n);
// calcuate b = 2mn
\$b = 2 * \$m * \$n;
//calcuate a = n² + m²
\$c = (\$n * \$n) + (\$m * \$m);
if (\$c <= \$limit)
{
//Display result
echo "". \$a ."² + ". \$b ."² = ". \$c ."²\n";
}
//increasing the n value by one
\$n++;
}
else
{
//Reset the state of variable
\$n = 1;
\$m = \$m + 1;
}
}
}
}

function main()
{
\$obj = new Triples();
//Test Case
\$obj->pythagorean_triples(30);
}
main();``````

#### Output

``````3² + 4² = 5²
8² + 6² = 10²
5² + 12² = 13²
15² + 8² = 17²
12² + 16² = 20²
7² + 24² = 25²
24² + 10² = 26²
21² + 20² = 29²``````
``````// Node Js program
// Generating Pythagorean Triples
class Triples
{
//Dispaly pythagorean triples under the given limit
pythagorean_triples(limit)
{
//Loop controlling variables
var a = 0;
var b = 0;
var c = 0;
// set initial m and n
var m = 2;
var n = 1;
//Execute loop, until the value of c are not exceed given limit
while (c <= limit)
{
//Check that valid condition
if (n < m)
{
// calcuate a = m² + n²
a = (m * m) - (n * n);
// calcuate b = 2mn
b = 2 * m * n;
//calcuate a = n² + m²
c = (n * n) + (m * m);
if (c <= limit)
{
//Display result
process.stdout.write("" + a + "² + " + b + "² = " + c + "²\n");
}
//increasing the n value by one
n++;
}
else
{
//Reset the state of variable
n = 1;
m = m + 1;
}
}
}
}

function main()
{
var obj = new Triples();
//Test Case
obj.pythagorean_triples(30);
}
main();``````

#### Output

``````3² + 4² = 5²
8² + 6² = 10²
5² + 12² = 13²
15² + 8² = 17²
12² + 16² = 20²
7² + 24² = 25²
24² + 10² = 26²
21² + 20² = 29²``````
``````#  Python 3 program
#  Generating Pythagorean Triples
class Triples :
# Dispaly pythagorean triples under the given limit
def pythagorean_triples(self, limit) :
# Loop controlling variables
a = 0
b = 0
c = 0
#  set initial m and n
m = 2
n = 1
# Execute loop, until the value of c are not exceed given limit
while (c <= limit) :
# Check that valid condition
if (n < m) :
#  calcuate a = m² + n²
a = (m * m) - (n * n)
#  calcuate b = 2mn
b = 2 * m * n
# calcuate a = n² + m²
c = (n * n) + (m * m)
if (c <= limit) :
# Display result
print("{0}² + {1}² = {2}".format(a,b,c))

# increasing the n value by one
n += 1
else :
# Reset the state of variable
n = 1
m = m + 1

def main() :
obj = Triples()
# Test Case
obj.pythagorean_triples(30)

if __name__ == "__main__": main()``````

#### Output

``````3² + 4² = 5
8² + 6² = 10
5² + 12² = 13
15² + 8² = 17
12² + 16² = 20
7² + 24² = 25
24² + 10² = 26
21² + 20² = 29``````
``````#  Ruby program
#  Generating Pythagorean Triples
class Triples

# Dispaly pythagorean triples under the given limit
def pythagorean_triples(limit)

# Loop controlling variables
a = 0
b = 0
c = 0
#  set initial m and n
m = 2
n = 1
# Execute loop, until the value of c are not exceed given limit
while (c <= limit)

# Check that valid condition
if (n < m)

#  calcuate a = m² + n²
a = (m * m) - (n * n)
#  calcuate b = 2mn
b = 2 * m * n
# calcuate a = n² + m²
c = (n * n) + (m * m)
if (c <= limit)

# Display result
print("", a ,"² + ", b ,"² = ", c ,"²\n")
end
# increasing the n value by one
n += 1
else

# Reset the state of variable
n = 1
m = m + 1
end
end
end
end
def main()

obj = Triples.new()
# Test Case
obj.pythagorean_triples(30)
end
main()``````

#### Output

``````3² + 4² = 5²
8² + 6² = 10²
5² + 12² = 13²
15² + 8² = 17²
12² + 16² = 20²
7² + 24² = 25²
24² + 10² = 26²
21² + 20² = 29²
``````
``````// Scala program
// Generating Pythagorean Triples
class Triples
{
//Dispaly pythagorean triples under the given limit
def pythagorean_triples(limit: Int): Unit = {
//Loop controlling variables
var a: Int = 0;
var b: Int = 0;
var c: Int = 0;
// set initial m and n
var m: Int = 2;
var n: Int = 1;
//Execute loop, until the value of c are not exceed given limit
while (c <= limit)
{
//Check that valid condition
if (n < m)
{
// calcuate a = m² + n²
a = (m * m) - (n * n);
// calcuate b = 2mn
b = 2 * m * n;
//calcuate a = n² + m²
c = (n * n) + (m * m);
if (c <= limit)
{
//Display result
print("" + a + "² + " + b + "² = " + c + "²\n");
}
//increasing the n value by one
n += 1;
}
else
{
//Reset the state of variable
n = 1;
m = m + 1;
}
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: Triples = new Triples();
//Test Case
obj.pythagorean_triples(30);
}
}``````

#### Output

``````3² + 4² = 5²
8² + 6² = 10²
5² + 12² = 13²
15² + 8² = 17²
12² + 16² = 20²
7² + 24² = 25²
24² + 10² = 26²
21² + 20² = 29²``````
``````// Swift program
// Generating Pythagorean Triples
class Triples
{
//Dispaly pythagorean triples under the given limit
func pythagorean_triples(_ limit: Int)
{
//Loop controlling variables
var a: Int = 0;
var b: Int = 0;
var c: Int = 0;
// set initial m and n
var m: Int = 2;
var n: Int = 1;
//Execute loop, until the value of c are not exceed given limit
while (c <= limit)
{
//Check that valid condition
if (n < m)
{
// calcuate a = m² + n²
a = (m * m) - (n * n);
// calcuate b = 2mn
b = 2 * m * n;
//calcuate a = n² + m²
c = (n * n) + (m * m);
if (c <= limit)
{
//Display result
print("\(a)² + \(b)² = \(c)");
}
//increasing the n value by one
n += 1;
}
else
{
//Reset the state of variable
n = 1;
m = m + 1;
}
}
}
}
func main()
{
let obj: Triples = Triples();
//Test Case
obj.pythagorean_triples(30);
}
main();``````

#### Output

``````3² + 4² = 5
8² + 6² = 10
5² + 12² = 13
15² + 8² = 17
12² + 16² = 20
7² + 24² = 25
24² + 10² = 26
21² + 20² = 29``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post