손코딩

[프로그래머스] 콜라츠 추측 본문

Java

[프로그래머스] 콜라츠 추측

활시현 2021. 1. 27. 16:59
728x90
728x90

class Solution {
    public int solution(long num) {
        int answer = 0;
            while(num != 1){
                if(num % 2 == 0){
                    num /= 2;
                }else{
                    num = (num * 3) + 1;
                }
                answer++;
                if(answer > 500){
                    answer = -1;
                    break;
                }
            }
        return answer;
    }
}