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]:
# YOUR CODE HERE

def triplet(s):
    #loop through possible values for a
    for a in range(1, s, 1):
        #loop through possible values of b
        for b in range(1, s-a, 1):
            #for values of a and b find c
            c = s - a - b
            #loop the squares of a and b and see if they equal the square of c 
            while a**2 + b**2 == c**2:
                #if the above is true return the product of a, b, and, c
                return a * b * c

#print the value calculated
print (triplet(1000))


31875000

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