# Euler 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 [2]:

from math import gcd
for m in range(2, 23, 2):
for n in range(1 + (m % 2), m, 2):
if gcd(m,n) == 1:
a = (m + n) * (m - n)
b = 2 * m * n
c = m * m + n * n
p = a + b + c
if (1000 % p) == 0:
k = 1000 // p
print((k*a, k*b, k*c))
print(k**3 * a * b * c)

``````
``````

(375, 200, 425)
31875000

``````

Explanation: A Pythagorean triplet is said to be primitive if the three numbers have no common factor. Every Pythagorean triplet is a multiple of a primitive triplet.

If \$m > n\$ then \$(m^2 - n^2, 2mn, m^2 + n^2)\$ is a Pythagorean triple. The triple is primitive if and only if \$m-n\$ is odd and \$\gcd(m, n) = 1\$. Furthermore, every primitive Pythagorean triple can be generated in this manner.

Our strategy is to find all primitive Pythagorean triplets whose sums are factors of 1000, and scale them so their sums are equal to 1000.