To get all Pythagorean triples up to a given number in Python, we can use a for loop and apply the Pythagorean triplet’s square sum connection.
def pythagorean_triples(num):
triples = []
c, m = 0, 2
while c < num:
for n in range(1, m):
a = m * m - n * n
b = 2 * m * n
c = m * m + n * n
if c > num:
break
triples.append([a, b, c])
m = m + 1
return triples
print(pythagorean_triples(25))
#Output:
[[3, 4, 5], [8, 6, 10], [5, 12, 13], [15, 8, 17], [12, 16, 20], [7, 24, 25]]
A less efficient solution for getting all Pythagorean triples in a range is checking all numbers in a range to see if a squared plus b squared equals c squared in a loop.
def pythagorean_triples(num):
triples = []
for b in range(1, num):
for a in range(1, b):
c = (a ** 2 + b ** 2) ** (1/2)
if c % 1 == 0 and c <= num:
triples.append([a, b, int(c)])
elif c > num:
break
return triples
print(pythagorean_triples(25))
#Output:
[[3, 4, 5], [6, 8, 10], [5, 12, 13], [9, 12, 15], [8, 15, 17], [12, 16, 20], [15, 20, 25], [7, 24, 25]]
Python allows us to implement complex algorithms to do various calculations. One such calculation is finding all of the Pythagorean triples in a range of numbers.
Pythagorean triples are a set of three whole numbers where a squared plus b squared equals c squared.
We can find Pythagorean triples in a range easily by with a loop and apply the Pythagorean triplet’s square sum connection. The square sum connection gives us equations which can help us solve for a, b and c.
Below is an example in Python of how to get Pythagorean triples up to a given number.
def pythagorean_triples(num):
triples = []
c, m = 0, 2
while c < num:
for n in range(1, m):
a = m * m - n * n
b = 2 * m * n
c = m * m + n * n
if c > num:
break
triples.append([a, b, c])
m = m + 1
return triples
print(pythagorean_triples(25))
#Output:
[[3, 4, 5], [8, 6, 10], [5, 12, 13], [15, 8, 17], [12, 16, 20], [7, 24, 25]]
Getting the First n Pythagorean Triples in Python
If you want to get the first n Pythagorean triples, instead of finding the Pythagorean triples up to a certain number, we can easily change the function above.
Instead of checking if c is less than num, we will check the length of the triples list.
Below is how to get the first 5 Pythagorean triples in Python.
def pythagorean_triples(num):
triples = []
c, m = 0, 2
while len(triples) < num:
for n in range(1, m):
a = m * m - n * n
b = 2 * m * n
c = m * m + n * n
if len(triples) == num:
break
triples.append([a, b, c])
m = m + 1
return triples
print(pythagorean_triples(5))
#Output:
[[3, 4, 5], [8, 6, 10], [5, 12, 13], [15, 8, 17], [12, 16, 20]]
Brute Force Method for Computing Pythagorean Triples in Python
A less efficient solution for getting all Pythagorean triples in a range is checking all numbers in a range to see if a squared plus b squared equals c squared in a loop.
We can brute force all of the Pythagorean triples in a range by looping over all valid combinations of a and b and check if a squared plus b squared equals c squared.
Below is a simple example of a Python function which calculates Pythagorean triples up to a given number.
def pythagorean_triples(num):
triples = []
for b in range(1, num):
for a in range(1, b):
c = (a ** 2 + b ** 2) ** (1/2)
if c % 1 == 0 and c <= num:
triples.append([a, b, int(c)])
elif c > num:
break
return triples
print(pythagorean_triples(25))
#Output:
[[3, 4, 5], [6, 8, 10], [5, 12, 13], [9, 12, 15], [8, 15, 17], [12, 16, 20], [15, 20, 25], [7, 24, 25]]
Hopefully this article has been useful for you to learn how to get Pythagorean triples in Python.