[프로그래머스/Lv.0] 제곱수 판별하기

Lainlnya·2023년 2월 21일
0

프로그래머스

목록 보기
39/49
post-thumbnail

문제 설명

어떤 자연수를 제곱했을 때 나오는 정수를 제곱수라고 합니다. 정수 n이 매개변수로 주어질 때, n이 제곱수라면 1을 아니라면 2를 return하도록 solution 함수를 완성해주세요.

제한 사항

  • 1 ≤ n ≤ 1,000,000

입출력 예

문제 풀이

function solution(n) {
  let temp = 0;
  switch (n & 0x0f) {
    case 0:
    case 1:
    case 4:
    case 9:
      temp = Math.sqrt(n);
      return Number.isInteger(temp) && temp * temp === n ? 1 : 2;

    default:
      return 2;
  }
}
profile
Growing up

0개의 댓글