1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | public class Problem014 { public static void main(String[] args) { System.out.println(run()); } public static String run() { long temp; long result = 0; int max = 0; int count; for(int i = 2; i <= 1000000; ++i) { temp = i; count = 1; while (temp != 1) { if (temp % 2 == 0) { temp /= 2; } else { temp = (3 * temp) + 1; } ++count; } if(count > max) { max = count; result = i; } } return Long.toString(result); } } | cs |
정답: 837799
실행 시간: 0.6초
단순하게 풀었다. 문제의 내용을 그대로 코드로 옮겼다.
다만 각 과정을 거칠 때마다 count 를 1 씩 늘리며 횟수를 세고, max 와 비교해 가장 긴 과정을 거치는 수 i 를 알아내는 부분만 추가되었을 뿐이다.
'Programming > Solutions' 카테고리의 다른 글
[Project Euler] Problem 016 (0) | 2019.04.16 |
---|---|
[Project Euler] Problem 015 (0) | 2019.04.16 |
[Project Euler] Problem 013 (0) | 2019.04.09 |
[Project Euler] Problem 012 (0) | 2019.04.08 |
[Project Euler] Problem 011 (0) | 2019.04.08 |