Beakjoon&프로그래머스/Java

[프로그래머스/Java] 유한소수 판별하기

현장 2025. 2. 21. 02:10

-Code

class Solution {
    public int solution(int a, int b) {
        int n = b / getGcd(a, b);
        while (n != 1) {
            if (n % 5 == 0) n /= 5;
            else if (n % 2 == 0) n /= 2;
            else return 2;
        }
        return 1;
    }

    public static int getGcd(int n1, int n2) {
        return n1 % n2 == 0 ? n2 : getGcd(n2, n1 % n2);
    }
}

while문 종료 조건을 잘 못찾아서 힌트등을 보고 해결했습니다.