Project Euler: Problem 9

https://projecteuler.net/problem=9

A Pythagorean triplet is a set of three natural numbers, $a < b < c$, for which,

$$a^2 + b^2 = c^2$$

For example, $3^2 + 4^2 = 9 + 16 = 25 = 5^2$.

There exists exactly one Pythagorean triplet for which $a + b + c = 1000$. Find the product abc.


In [9]:
#This function displays the greatest product of the Pythagorean triple that adds up to n (The parameter).
def Pythag(n):
    #creates a for loop with i ranging from 1 to n
    for i in range(1,n):
        #creates a for loop with j ranging from i+1 to n
        for j in range(i+1,n):
            #k = n - i - j: because i + j + k = n
            k = n - i - j
            #is i,j,k a Pythagorean triple?
            if i**2 + j**2 == k**2:
                
                return i * j * k
            
Pythag(1000)


Out[9]:
31875000

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:
# This cell will be used for grading, leave it at the end of the notebook.