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 [4]:
import math

A = [i for i in range(1000)] # a, b could be any value 0-999
B = [i for i in range(1000)] 

for a in A:
    for b in B: # I solved both equations for "c" and set them equal to each other. Since a and b cannot equal 0 (since this is a triangle) and a<b<c there can only be one answer
        if math.sqrt(a**2 + b**2) == 1000 - a - b and a != 0 and b != 0 and b>a: 
            print(a*b*(1000-a-b))


31875000

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