Given a non-negative integer c, decide whether there're two integers a and b such that a2 + b2 = c.
Example
Input: c = 5
Output: true
Explanation: 1 * 1 + 2 * 2 = 5
Solution
Use Fermat's theorem for O((ān)) Time Complexity.
Basic
defjudgeSquareSum(self,c:int) ->bool:# iterate from a for a inrange(int(math.sqrt(c))+1): b = math.sqer(c -pow(a,2))if b %1==0:returnTruereturnFalse
Faster
defjudgeSquareSum(self,c:int) ->bool: div =2whilepow(div, 2)<= c:if c % div ==0: exp =0while c % div ==0: exp +=1 c //= divif div %4==3and exp %2!=0:returnFalse div +=1return c %4!=3