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.

First, we can reform to say that: $$ c = 10^3 - (a+b)$$ so,

$$a^2+b^2=c^2=(10^3 - (a+b))^2.$$

This turns into:

$$a^2+b^2=10^6+a^2+b^2 + 2 ab - 2\cdot10^3(a+b)$$

which leads to:

$$ 2\cdot10^3(a+b)- 2 ab = 10^6$$

with $$a<b \mbox{ with } a,b \in \mathbb{N}.$$


In [12]:
def is_true_for_searched(a, b):
    return 2 * (10 ** 3) * (a+b) - 2*a*b == 10**6
is_true_for_searched(3, 4)


Out[12]:
False

In [30]:
def get_a_b_c():
    from math import sqrt
    for i in range(2, 1000):
        for j in range(i, 1000):
            if is_true_for_searched(i, j):
                return i, j, sqrt((i**2+j**2))

In [32]:
a, b, c = get_a_b_c()
print(a, b, c, a*b*c)


200 375 425.0 31875000.0

In [34]:
%timeit get_a_b_c()


10 loops, best of 3: 51.4 ms per loop