# Python Program to find all pythagorean triples in a range

December 20, 2022, Learn eTutorial
1940

This is a simple  number based python program. Here, we are going to generate the pythagorean triples.

For a better understanding of this example, we always recommend you to learn the basic topics of Python programming listed below:

## What are pythagorean triples?

This simple python program will generate all the Pythagorean triples in the given limit. A set of three positive numbers a, b and c is said to be Pythagorean triples if a2 + b2 = c2, normally denoted as (a, b, c). While considering a right angled triangle, a represents the perpendicular side, b represents the base and c represents the hypotenuse.

Let us take an example (3, 4, 5) the three numbers are well known Pythagorean triples where 32 + 42 = 52 ==> 9 + 16 = 25.

To understand the program we recommend you to learn the basics of pythagorous theorm.

## How to find Pythagorean triples in python?

To apply this logic in the python programming language, we accept the user's input as the limit to check all the Pythagorean triple numbers in the limit. Then open a `while loop` until the number is less than the limit. Then open a `for loop` from 1 to m, we have to calculate m2-n2 and store that value in a variable a. Now calculate 2*m*n and store that in a variable b, and calculate m2+n2 and store that in a variable c. Now open an `if` condition to check c is greater than the limit, `break` the program. then `print` the variable a b and c. Finally, increment m by one.

### ALGORITHM

STEP 1: Accept the limit from the user using the input method in python programming.

STEP 2: Initialize the variables c and m.

STEP 3: Open a `while loop` until the c is less than the limit.

STEP 4: Open a `for loop` from 1 to m to calculate the three variables a, b, c.

STEP 5: Open an `if` condition to check if c is greater than the limit if so `break` the program.

STEP 6: Print the variable a b and c as Pythagorean triples. And increment the, m by 1.

## Python Source Code

```                                          ```limit=int(input("Enter upper limit:"))
c=0
m=2
while(c<limit):
for n in range(1,m):
a=m*m-n*n
b=2*m*n
c=m*m+n*n
if(c>limit):
break
print(a,b,c)
m=m+1```
```

## OUTPUT

```Enter upper limit:17
3 4 5
8 6 10
5 12 13
15 8 17
```

VIEW ALL
VIEW ALL