Python Programming Challenge 1: Pythagorean Triples


The challenge today is to write a program to generate Pythagorean triples. 

Find that name familiar? Yes, it has to do with the Pythagorean Theorem that you learned in high school Math.

Remember the theorem that says that for any right-angled triangle, the sum of the squares of the two shorter sides is equal to the square of the longest side? In other words, for the triangle shown in the diagram below, a2 + b2 = c2.

In most cases, at least one of the lengths of the triangle will not be an integer.

However, in some special cases, all three sides will have lengths that are integers. A common example is a right angle triangle with sides 3cm, 4cm and 5cm.

The numbers 3, 4, 5 is known as a Pythagorean triple. Specifically, a Pythagorean triple is a set of three positive integers a, b, and c such that the three integers form the sides of a right-angle triangle.

Clear?

The task today is to write a program in Python that does the following:

  1. Prompt users to enter the number of triples (n) that they want to generate. 
  2. Generate the first n triples, ensuring that there are no duplicates.

To do that, we’ll use the two formulas below:

For any even number p,
the numbers p, (p/2)2 -1 and (p/2)2 + 1 form a Pythagorean triple.

For any odd number q,
the numbers q, (q2 -1)/2 and (q2 +1)/2 form a Pythagorean triple.

Here’s a video showing how the program works.

The suggested solution and a run-through of it can be found below.


Suggested Solution:

i = 0
a = [0, 0, 0]

n = int(input('Number of triples required: '))

while i<n:

    if i%2 == 0:    #even
        a[0] = i
        a[1] = int((i/2)**2 - 1)
        a[2] = int((i/2)**2 + 1)
    else:           #odd
        a[0] = i
        a[1] = int((i**2-1)/2)
        a[2] = int((i**2+1)/2)

    if (a[0] < a[1] and a[0] < a[2]):
        #This is a new triple, print it
        print(a)
    else:
        #This is a duplicate triple, do not print it.
        #Increment n by 1 so that the loop will calculate one more triple
        #to replace the current duplicate
        n = n + 1

    i = i + 1


Main Concepts Used: 

  • Python Math operators for finding remainders (%) and exponents (**)
  • Control flow statements like if, while, for loops etc

Run Through:

First, we declare and initialize three variables i, a and number

Next, we use a while loop to repeatedly calculate and print the triples. i serves as a loop counter that is incremented by one each time the loop runs.

Within the while loop, we check if i is even. We do that using i%2. The % operator gives us the remainder when i is divided by 2. If the remainder is zero, we know that i is even.

Depending on whether i is even or odd, we use the corresponding formula to calculate the three integers that form the triple.

After calculating each triple, we check if the first number is the smallest among the three. If it is, we know that this triple is new. 

If it is not, we know that the triple is a duplicate.

For instance, when i is 3, we get the triple 3, 4, 5.

However, when i becomes 4, we get the triple 4, 3, 5 which is actually the same as the previous triple. 

We can detect this duplicate simply by looking at the smallest number in 4, 3, 5. The smallest number is 3, hence we know this triple has already been displayed when i = 3.

In other words, if there is a number that is smaller than the first number in the triple, we know that this triple has already been calculated earlier.

If the triple is new, we use the print() function to print its value (that is stored inside the list a).

Else, we increase the value of n by 1 as we need to calculate one more triple to replace the current one.

Finally, we increment i by 1  and let the while loop run again until the condition i < n is no longer valid.

Written by Jamie | Last Updated August 28, 2019

Recent Posts