In this example we will show how to find out all Pythagorean triplets in a given range with Python.
Source Code
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
upperLimit = int(input("Please input the upper range: "))
print("All the Pythagorean triplets between 1 and {} are: ".format(upperLimit))
z = 1
m = 2
while z < upperLimit: for n in range(1, m): if m > n:
x = 2 * m * n
y = m * m - n * n
z = m * m + n * n
if z > upperLimit:
break
print(x, y, z)
m += 1
Output:
Please input the upper range: 15
All the Pythagorean triplets between 1 and 15 are:
4 3 5
6 8 10
12 5 13
Please input the upper range: 25
All the Pythagorean triplets between 1 and 25 are:
4 3 5
6 8 10
12 5 13
8 15 17
16 12 20
24 7 25