In this simple python program, we need to compute the Pythagorean triples. It is a number-based python program.
For a better understanding of this example, we always recommend you to learn the basic topics of Python programming listed below:
This simple python program is to find all the Pythagorean triples in the given range. To understand the problem, the basic knowledge of Pythagorean triples is needed. A set of three positive numbers a, b and c is said to be Pythagorean triples if a2 + b2 = c2. Such a number set is normally denoted as (a, b, c).
Let us take an example (3, 4, 5) the three numbers are well known Pythagorean triples where 32 + 42 = 52 == 9 + 16 = 25.
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+1, 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. Else check
if a==0 or b ==0 or c ==0 if so,
break the python program, then
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+1 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: Using and
if condition checks any of the variables a, b, or c is zero if so,
break the program.
STEP 7: Print the variable a b and c as Pythagorean triples. And increment the, m by 1.
limit=int(input("Enter upper limit:")) c=0 m=2 while(c<limit): for n in range(1,m+1): a=m*m-n*n b=2*m*n c=m*m+n*n if(c>limit): break if(a==0 or b==0 or c==0): break print(a,b,c) m=m+1
Enter upper limit:17 3 4 5 8 6 10 5 12 13