問題描述
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:
Input: 5
Output: True
Explanation: 1 * 1 + 2 * 2 = 5
Input: 3
Output: False
思路:通過判斷一個整數(shù)對1求模是否為0來判斷一個數(shù)是否為整數(shù)。將雙層循環(huán)變?yōu)橐粚?/em>
/**
* @param {number} c
* @return {boolean}
*/
var judgeSquareSum = function(c) {
for(var i = 0; i*i <= c; i++){
var val = Math.sqrt(c-i*i);
if(val%1 === 0){
return true;
}
}
return false;
};