Given a non-negative integer c
, your task is to decide whether there're two integers a
and b
such that a2 + b2 = c.
Example 1:
Input: 5 Output: True Explanation: 1 * 1 + 2 * 2 = 5
Example 2:
Input: 3 Output: False
The simplest solution would be to consider every possible combination of integers and and check if the sum of their squares equals . Now, both and can lie within the range . Thus, we need to check for the values of and in this range only.
\n\nComplexity Analysis
\nTime complexity : . Two loops upto . Here, refers to the given integer(sum of squares).
\nSpace complexity : . Constant extra space is used.
\nWe can improve the last solution, if we make the following observation. For any particular chosen, the value of required to satisfy the equation will be such that . Thus, we need to traverse over the range only for considering the various values of . For every current value of chosen, we can determine the corresponding value and check if it is a perfect square or not. If it happens to be a perfect square, is a sum of squares of two integers, otherwise not.
\nNow, to determine, if the number is a perfect square or not, we can make use of the following theorem: "The square of positive integer can be represented as a sum of first odd positive integers." Or in mathematical terms:
\n\n.
\nTo look at the proof of this statement, look at the L.H.S. of the above statement.
\n\n\n
\n\n\n
\n\n\n
\n\n\n
\n\n\n
\n\n\n
\nThis completes the proof of the above statement.
\n\nComplexity Analysis
\nTime complexity : . The total number of times the is updated is: .
\nSpace complexity : . Constant extra space is used.
\nAlgorithm
\nInstead of finding if is a perfect square using sum of odd numbers, as done in the last approach, we can make use of the inbuilt function and check if turns out to be an integer. If it happens for any value of in the range , we can return a True value immediately.
\n\nComplexity Analysis
\nTime complexity : . We iterate over values for choosing . For every chosen, finding square root of takes time in the worst case.
\nSpace complexity : . Constant extra space is used.
\nAlgorithm
\nAnother method to check if is a perfect square, is by making use of Binary Search. The method remains same as that of a typical Binary Search to find a number.\nThe only difference lies in that we need to find an integer, in the range , such that this number is the square root of .\nOr in other words, we need to find an integer, , in the range , such that x.
\nThe following animation illustrates the search process for a particular value of .
\n!?!../Documents/633_Sum_of_Squares.json:1000,563!?!
\n\nComplexity Analysis
\nTime complexity : . Binary search taking in the worst case is done for values of .
\nSpace complexity : . Binary Search will take space.
\nAlgorithm
\nThis approach is based on the following statement, which is based on Fermat\'s Theorem:
\n"Any positive number is expressible as a sum of two squares if and only if the prime factorization of , every prime of the form occurs an even number of times."
\nBy making use of the above theorem, we can directly find out if the given number can be expressed as a sum of two squares.
\nTo do so we simply find all the prime factors of the given number , which could range from along with the count of those factors, by repeated division. \nIf at any step, we find out that the number of occurences of any prime factor of the form occurs an odd number of times, we can return a False value.
\nIn case, itself is a prime number, it won\'t be divisible by any of the primes in the . Thus, we need to check if can be expressed in the form of\n. If so, we need to return a False value, indicating that this prime occurs an odd number(1) of times.
\nOtherwise, we can return a True value.
\nThe proof of this theorem includes the knowledge of advanced mathematics and is beyond the scope of this article. However, interested reader can refer to this documentation.
\n\nComplexity Analysis
\nTime complexity : . We find the factors of and their count using repeated division. We check for the factors in the range .\nThe maximum number of times a factor can occur(repeated division can be done) is (considering 2 as the only factor, . Thus, ).
\nSpace complexity : . Constant space is used.
\nAnalysis written by: @vinod23
\n