The
Brahmagupta–Fibonacci identity states that the product of two sums of two squares is a sum of two squares. Euler's method relies on this theorem but it can be viewed as the converse, given n = a^2 + b^2 = c^2 + d^2 we find n as a product of sums of two squares. First deduce that :a^2 - c^2 = d^2 - b^2 and factor both sides to get :(a-c)(a+c) = (d-b)(d+b) (1) Now let k = \operatorname{gcd}(a-c,d-b) and h = \operatorname{gcd}(a+c,d+b) so that there exists some constants l,m,l',m' satisfying • (a-c) = kl, • (d-b) = km, \operatorname{gcd}(l,m) = 1 • (a+c) = hm', • (d+b) = hl', \operatorname{gcd}(l',m') = 1 Substituting these into equation (1) gives :klhm' = kmhl' Canceling common factors yields :lm' = l'm Now using the fact that (l,m) and \left(l',m'\right) are pairs of relatively prime numbers, we find that • l = l' • m = m' So • (a-c) = kl • (d-b) = km • (a+c) = hm • (d+b) = hl We now see that m = \operatorname{gcd}(a+c,d-b) and l = \operatorname{gcd}(a-c,d+b) Applying the
Brahmagupta–Fibonacci identity we get :\left(k^2 + h^2\right)\left(l^2 + m^2\right) = (kl + hm)^2 + (km - hl)^2 = \bigl((a-c) + (a+c)\bigr)^2 + \bigl((d-b) - (d+b)\bigr)^2 = (2a)^2 + (2b)^2 = 4n. As each factor is a sum of two squares, one of these must contain both even numbers: either (k, h) or (l ,m). Without loss of generality, assume that pair (k,h) is even. The factorization then becomes :4n = \left(\left(\tfrac{k}{2}\right)^2 + \left(\tfrac{h}{2}\right)^2\right)\left(l^2 + m^2\right). \, ==Worked example==