#Skip to menu

Special Pythagorean triplet

First read the problem description.

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).


Source code of the solution(s):