보통사람

[7kyu] 해당 정수가 제곱근인지 확인 (You're a square!) 본문

Codewars/Javascript

[7kyu] 해당 정수가 제곱근인지 확인 (You're a square!)

pej4303 2019. 8. 2. 23:23
/**
 * <pre>
 * 해당 정수가 제곱근인지 확인 (You're a square!)
 * 
 * 정수가 주어 졌을 때 그 수가 제곱인지 확인하십시오.
 * 수학에서 정사각형 수 또는 완전한 정사각형은 정수의 제곱 인 정수입니다. 
 * 다시 말해서, 그것은 정수 자체의 결과입니다.
 * 테스트에서는 항상 정수를 사용하므로 동적 유형 지정 언어에서는 걱정하지 마십시오.
 * 
 * Given an integral number, determine if it's a square number:
 * In mathematics, a square number or perfect square is an integer that is the square of an integer; 
 * in other words, it is the product of some integer with itself.
 * The tests will always use some integral number, so don't worry about that in dynamic typed languages.
 * 
 * </pre>
 *
 * @auther : pej
 * @date : 2019. 03. 16.
 * @param : {int} 확일할 정수
 * @return : {Boolean} true / false
 * @example : isNumber(val);
 *
 */
function isSquare (num) {
    var root = Math.sqrt(num);
    
    if (root % 1 == 0) {
        return true;
    } else {
        return false;
    }
}

 

  • 방법2) - 다른 사람이 한 것

function isSquare (num) {
    return Math.sqrt(num) % 1 === 0;
}

 

  • 방법3) - 다른 사람이 한 것

function isSquare (num) {
    return Number.isInteger(Math.sqrt(num));
}