Spoilers for Project Euler

Problem 9

Project Euler Problem 9 is a problem about Pythagorean Triples. A Pythagorean Triple is a set of 3 integers that satisfy the equation a2+b2=c2 where a<b<c. The goal is to find the product abc of the only Pythagorean Triplet that satisfies a+b+c=1000.

Finding an Equation

The problem gives us two equations and three variables, so we’re not going to be able to solve it with just algebraic manipulation. My approach was to try to find the value of c that would match a pair of values for a, b. The first equation becomes c=1000-a-b. Once we have some values for c, we can plug it into the Pythagorean formula to see if we get appropriate values.

Python Code

This approach just goes through a pair of for loops. We don’t need to go through every possible number 1 to 1000 because we know the max value for a is 332 and b is a 499.

for a in range(333):
    for b in range(500):
        ans_1 = equation_1(a, b)
        ans_2 = equation_2(a, b)
        if ans_1 ** 2 == ans_2:
            print(a)
            print(b)
            print(ans_1)

def equation_1(a, b):
    return 1000 - a - b

def equation_2(a, b):
    return a**2 + b**2

200
375
425
31875000