Skip to main content

Euler’s Four Square Identity

According to Euler’s four square identity, the product of any two numbers a and b can be expressed as a sum of four squares if a and b both can individually be expressed as sum of four squares.


function euler_four_square(a, b, ab)

{
var res = [];
var s = 0;
for (var i = 0;i * i <= ab;i ++)
{
s = i * i;
for (var j = i;j * j <= ab;j ++)
{
s = j * j + i * i;
for (var k = j;k * k <= ab;k ++)
{
s = k * k + j * j + i * i;
for (var l = k;l * l <= ab;l ++)
{
s = l * l + k * k + j * j + i * i;
if (s == ab)
{
res.push([ab,i,j,k,l]);
}
}
}
}
}
return res;
}