Special Pythagorean triplet
Let’s consider the general case \(a + b + c = s\), where \(s\) can only be even. All the pythagorean triplets can be generated with the Euclid’s formula \(a = k*(m^2 - n^2)\), \(b = k*(2*m*n)\), \(c = k*(m^2 + n^2)\). When substituting the above in \(a + b + c = s\) we get \(2*k*m*(n + m) = s\) and \(k = \frac{s'}{m*(n+m)}\) with \(s' = s/2\). The conditions for the Euclid formula are: \(m > n\), \(m - n\) odd and \(m\) and \(n\) coprime. So we just need to generate all the pairs \((m,n)\) which satisfy these conditions until we find one that evenly divides \(s'\) at which point we calculate \(k\). This can be done, starting with \(m = 2\) and \(n = 1\), by applying the following formulas at each iteration: \((2m - n, m)\), \((2m + n, m)\) and \((m + 2n, n)\) (see Generating all coprime pairs).
def euclid_formula(k, m, n):
return k*(m**2 - n**2), k*(2*m*n), k*(m**2+n**2)
def coprimes():
from collections import deque
= deque([(2, 1)])
pairs while True:
= pairs.popleft()
(m, n) yield m, n
2*m - n, m))
pairs.append((2*m + n, m))
pairs.append((+ 2*n, n))
pairs.append((m
def k(s):
= s/2
s1 for (m, n) in coprimes():
= divmod(s1, m*(n+m))
k, rem if rem == 0:
return euclid_formula(k, m, n)
= k(1_000)
a, b, c int(a*b*c)
31875000
Source code of the solution(s):