손코딩
[프로그래머스] 콜라츠 추측 본문
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;
}
}
'Java' 카테고리의 다른 글
[프로그래머스] x만큼 간격이 있는 n개의 숫자 (0) | 2021.01.28 |
---|---|
[프로그래머스] 하샤드수 (0) | 2021.01.28 |
[프로그래머스] 짝수와 홀수 (0) | 2021.01.27 |
[프로그래머스] 자릿수 더하기 (0) | 2021.01.27 |
[프로그래머스] 약수의 합 (0) | 2021.01.27 |