Generating 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


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.

New Comment







© 2021, kalkicode.com, All rights reserved