-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem014.java
More file actions
32 lines (32 loc) · 966 Bytes
/
Problem014.java
File metadata and controls
32 lines (32 loc) · 966 Bytes
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
import java.util.Map;
import java.util.HashMap;
public class Problem14 {
public static void main(String[] args) {
Map<Integer, Integer> chains = new HashMap<Integer, Integer>();
chains.put(1, 1);
int largest = 1;
int largestStart = 1;
for (int i = 1; i < 1000000; i++) {
double j = i;
int length = 1;
while (j != 1) {
if (chains.containsKey(j)) {
length += chains.get(j);
j = 1;
} else if (j % 2 == 0) {
j /= 2;
length++;
} else {
j = 3 * j + 1;
length++;
}
}
if (length > largest) {
largest = length;
largestStart = i;
}
chains.put(i, length);
}
System.out.println(largestStart + ": " + largest);
}
}