https://school.programmers.co.kr/learn/courses/30/lessons/12943
class Solution {
public int solution(int num) {
int answer = -1;
int result =num;
int count= 0;
if(result==1){
return 0;
}
for(int i =0; i<500; i++){
if(result%2==0){
result =result/2;
count++;
if(result==1){
break;
}
}
else if(result%2==1){
result=result*3+1;
count++;
if(result==1){
break;
}
}
}
if(result!=1){
return -1;
}
return count;
}
}
class Collatz {
public int collatz(int num) {
int answer = 0;
int count = 0;
while (count <= 500) {
if (num % 2 == 0) {
num = num / 2;
count++;
} else if (num % 2 == 1) {
num = num * 3 + 1;
count++;
}
answer = count;
if (num == 1) {
break;
} else if (count == 500) {
answer = -1;
}
}
return answer;
}
```