![]() |
VOOZH | about |
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 the sum of four squares.
Mathematically, if a = and b =
Then, a * b =
where c1, c2, c3, c4, d1, d2, d3, d4, e1, e2, e3, e4 are any integer.
Some examples are, a = = 30 b = = 4 ab = a * b = 120 = a = = 15 b = = 24 ab = a * b = 810 = a = = 15 b = = 26 ab = a * b = 390 =
Example:
Input: a = 1 * 1 + 2 * 2 + 3 * 3 + 4 * 4 b = 1 * 1 + 1 * 1 + 1 * 1 + 1 * 1 Output: i = 0 j = 2 k = 4 l = 10 Product of 30 and 4 can be written as sum of squares of i, j, k, l 120 = 0 * 0 + 2 * 2 + 4 * 4 + 10 * 10 i = 2 j = 4 k = 6 l = 8 Product of 30 and 4 can be written as sum of squares of i, j, k, l 120 = 2 * 2 + 4 * 4 + 6 * 6 + 8 * 8
Explanation :
The product of the 2 numbers a(30) and b(4) can be represented as the sum of 4 squares as stated by Euler's four square identity. The above are the 2 representations of the product a * b in the sum of 4 squares form. All possible representations of the product a*b in the sum of four squares form are shown.
Input: a = 1*1 + 2*2 + 3*3 + 1*1 b = 1*1 + 2*2 + 1*1 + 1*1 Output: i = 0 j = 1 k = 2 l = 10 Product of 15 and 7 can be written as sum of squares of i, j, k, l 105 = 0*0 + 1*1 + 2*2 + 10*10 i = 0 j = 4 k = 5 l = 8 Product of 15 and 7 can be written as sum of squares of i, j, k, l 105 = 0*0 + 4*4 + 5*5 + 8*8 i = 1 j = 2 k = 6 l = 8 Product of 15 and 7 can be written as sum of squares of i, j, k, l 105 = 1*1 + 2*2 + 6*6 + 8*8 i = 2 j = 2 k = 4 l = 9 Product of 15 and 7 can be written as sum of squares of i, j, k, l 105 = 2*2 + 2*2 + 4*4 + 9*9 i = 2 j = 4 k = 6 l = 7 Product of 15 and 7 can be written as sum of squares of i, j, k, l 105 = 2*2 + 4*4 + 6*6 + 7*7 i = 3 j = 4 k = 4 l = 8 Product of 15 and 7 can be written as sum of squares of i, j, k, l 105 = 3*3 + 4*4 + 4*4 + 8*8
Approach :
Brute Force :
A given number(a*b) can be represented in a sum of 4 squares form by using 4 loops i, j, k, l to find each of the four squares. This gives all possible combinations to form a*b as a sum of four squares. At each iteration of the innermost loop(l loop), check the sum with the product a*b. If there is a match, then print the 4 numbers(i, j, k, and l) whose sum of squares equals a*b.
i = 0 j = 2 k = 4 l = 10 Product of 30 and 4 can be written as sum of squares of i, j, k, l 120 = 0*0 + 2*2 + 4*4 + 10*10 i = 2 j = 4 k = 6 l = 8 Product of 30 and 4 can be written as sum of squares of i, j, k, l 120 = 2*2 + 4*4 + 6*6 + 8*8
Improved Algorithm:
The time complexity of the above algorithm is in the worst case. This can be reduced to by subtracting the squares of i, j, and k from the product a*b for all (i, j, k) and checking if that value is a perfect square or not. If it is a perfect square, then we have found the solution.
Output:
i = 0 j = 2 k = 4 l = 10 Product of 30 and 4 can be written as sum of squares of i, j, k, l 120 = 0*0 + 2*2 + 4*4 + 10*10 i = 2 j = 4 k = 6 l = 8 Product of 30 and 4 can be written as sum of squares of i, j, k, l 120 = 2*2 + 4*4 + 6*6 + 8*8