Special Pythagorean triplet

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,

$$a^2 + b^2 = c^2$$ For example, 32 + 42 = 9 + 16 = 25 = 52.

There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product $$abc$$.


Idea

A problem of 'Linear indeterminate equation', a.k.a. 'Diophantine equation'.

Refer to Diophantine equation

We have equation:

  • a, b, c are natural numbers
  • $$ a < b < c$$
  • $$ a^2 + b^2 = c^2$$
  • $$ a + b + c = 1000$$

In [1]:
from math import ceil
In [2]:
def solve(sum_of_three):
    
    # solve determinate equation for `b` and `c`
    def solve_b_c(a):
        _1, _2 = sum_of_three - a, pow(a, 2) // (sum_of_three - a)
        return (_1 - _2) // 2, (_1 + _2) // 2
    
    a_upper_bound = ceil(sum_of_three / 3)
    for a in range(1, a_upper_bound):
        b, c = solve_b_c(a)
        if pow(a, 2) + pow(b, 2) == pow(c, 2):
            return (a, b, c), a * b * c
In [3]:
solve(12)
Out[3]:
((3, 4, 5), 60)
In [4]:
solve(1000)
Out[4]:
((200, 375, 425), 31875000)