Day.12 Sum of Square Numbers(633)

问题描述
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

思路:通过判断一个整数对1求模是否为0来判断一个数是否为整数。将双层循环变为一层

/**
 * @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;
};
    原文作者:前端伊始
    原文地址: https://www.jianshu.com/p/cc05d982963f
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞