Study with me/프로그래머스 L0 마스터하기

프로그래머스 - L0 제곱수판별하기

외계나무 2023. 11. 6. 17:59

프로그래머스 - level 0 제곱수 판별하기

class Solution {
    public int solution(int n) {
        int answer = 2;
        for(int i=0; i<(int)(Math.sqrt(1000000)+1); i++) {
            if(Math.pow(i, 2) == n)
                answer = 1;
        }
        return answer;
    }
}

그리고...

// 탐나는 타인의 코드

class Solution {
    public int solution(int n) {
        if (n % Math.sqrt(n) == 0) {
            return 1;
        } else {
            return 2;
        }
    }
}