633. Sum of Square Numbers


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


Solution


Approach #1 Brute Force [Time Limit Exceeded]

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.

Complexity Analysis

  • Time complexity : . Two loops upto . Here, refers to the given integer(sum of squares).

  • Space complexity : . Constant extra space is used.


Approach #2 Better Brute Force [Time Limit Exceeded]

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

Now, 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:

.

To look at the proof of this statement, look at the L.H.S. of the above statement.

This completes the proof of the above statement.

Complexity Analysis

  • Time complexity : . The total number of times the is updated is: .

  • Space complexity : . Constant extra space is used.


Approach #3 Using sqrt function [Accepted]

Algorithm

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

Complexity Analysis

  • Time complexity : . We iterate over values for choosing . For every chosen, finding square root of takes time in the worst case.

  • Space complexity : . Constant extra space is used.


Approach #4 Using Binary Search [Accepted]

Algorithm

Another 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. The only difference lies in that we need to find an integer, in the range , such that this number is the square root of . Or in other words, we need to find an integer, , in the range , such that x.

The following animation illustrates the search process for a particular value of .

!?!../Documents/633_Sum_of_Squares.json:1000,563!?!

Complexity Analysis

  • Time complexity : . Binary search taking in the worst case is done for values of .

  • Space complexity : . Binary Search will take space.


Approach #5 Fermat Theorem [Accepted]:

Algorithm

This approach is based on the following statement, which is based on Fermat's Theorem:

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

By making use of the above theorem, we can directly find out if the given number can be expressed as a sum of two squares.

To 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. If 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.

In 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 . If so, we need to return a False value, indicating that this prime occurs an odd number(1) of times.

Otherwise, we can return a True value.

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

Complexity Analysis

  • Time complexity : . We find the factors of and their count using repeated division. We check for the factors in the range . The maximum number of times a factor can occur(repeated division can be done) is (considering 2 as the only factor, . Thus, ).

  • Space complexity : . Constant space is used.


Analysis written by: @vinod23