Project Euler Problem 9

A Pythagorean triplet is a set of three natural numbers, a lt b lt c, for which, For example, 3^2 + 4^2 = 9 + 16 = 25 =

Project Euler Problem 9

Solution

We seek a Pythagorean triplet satisfying

$a^2+b^2=c^2$$a$$b$$c = \sqrt{a^2 + b^2} \approx 21.21$$a^2 + b^2 = c^2 \approx 225.00 + 225.00 = 450.00$Adjusting a and b updates the triangle and the derived value of c.abc

with

$$a+b+c=1000.$$

Substitute $c = 1000-a-b$ into the Pythagorean equation:

$$a^2+b^2=(1000-a-b)^2.$$

Expanding and simplifying:

$$a^2+b^2=1000000+a^2+b^2+2ab-2000a-2000b,$$

so

$$0=1000000+2ab-2000a-2000b.$$

Divide by $2$:

$$ab-1000a-1000b=-500000.$$

Rearrange:

$$(a-1000)(b-1000)=500000.$$

Searching factor pairs yields:

$$a=200,\quad b=375,\quad c=425.$$

Check:

$$200^2+375^2=40000+140625=180625=425^2.$$

And:

$$200+375+425=1000.$$

Therefore,

$$abc = 200 \times 375 \times 425 = 31875000.$$

Python code:

for a in range(1, 1000):
    for b in range(a + 1, 1000):
        c = 1000 - a - b
        if a*a + b*b == c*c:
            print(a * b * c)

Tracing the logic finds the unique triplet $(200,375,425)$, producing:

31875000

Answer: 31875000