Algorithm/프로그래머스
[알고리즘] 콜라츠 추측 Java
rw-
2023. 11. 1. 12:51
728x90
class Solution {
public int solution(long num) {
int answer = 0;
while(num > 1) {
num = num % 2 == 0 ? num / 2 : (num * 3) + 1;
answer++;
if(answer >= 500) {
answer = -1;
break;
}
}
return answer;
}
}
728x90
반응형