Python Program to Determine all Pythagorean Triplets in the Range

PythonServer Side ProgrammingProgramming

When it is required to determine the Pythagorean triplets within a given range, a method is defined, that helps calculate the triplet values.

Below is the demonstration of the same −

Example

 Live Demo

def pythagorean_triplets(limits) :
   c, m = 0, 2
   while c < limits :
   for n in range(1, m) :
      a = m * m - n * n
      b = 2 * m * n
      c = m * m + n * n
      if c > limits :
         break
      print(a, b, c)
   m = m + 1
upper_limit = 15
print("The upper limit is :")
print(upper_limit)
print("The Pythagorean triplets are :")
pythagorean_triplets(upper_limit)

Output

The upper limit is :
15
The Pythagorean triplets are :
3 4 5
8 6 10
5 12 13

Explanation

  • A method is defined that defines variables to define values for each of the Pythagorean triplet.

  • Outside the method, the integer is defined.

  • This method is called by passing the integer.

  • The output is displayed on the console.

raja
Updated on 19-Apr-2021 10:40:17

Advertisements