콜라츠 추측 Level 2
https://programmers.co.kr/learn/challenge_codes/13
class Collatz {
public int collatz(int num) {
int answer = 0;
while(answer < 500){
num = (num%2==0) ? (num /= 2) : (num = num*3+1);
answer++;
if (num == 1)
return answer;
}
return -1;
}
// 아래는 테스트로 출력해 보기 위한 코드입니다.
public static void main(String[] args) {
Collatz c = new Collatz();
int ex = 1056935;
System.out.println(c.collatz(ex));
}
}
'프로그래밍 > 알고리즘(자바)' 카테고리의 다른 글
정수 내림차순으로 배치하기 Level 2 #split #Arrays.sort #Collections.reverse #Arrays.asList #String.join (0) | 2018.05.07 |
---|---|
2016년 Level 2 #달력(요일) 알고리즘 (0) | 2018.05.07 |
최솟값 만들기 Level 2 #Arrays.sort (0) | 2018.05.07 |
소수 찾기 Level 2 #Math.sqrt #소수 (0) | 2018.05.06 |
삼각형출력하기 Level 1 (0) | 2018.05.06 |