-
Notifications
You must be signed in to change notification settings - Fork 0
/
P14.java
35 lines (31 loc) · 981 Bytes
/
P14.java
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
public class P14 {
public static void main(String[] args) {
StdOut.print("Enter N: ");
int N = StdIn.readInt();
SeparateChainingHashST<Integer, Integer> st = new SeparateChainingHashST<Integer, Integer>();
int current;
int chain;
int max = 0;
for (int i = 1; i < N; i++) {
current = i;
chain = 0;
while (current != 1) {
if (st.contains(current)) {
chain += st.get(current);
break;
}
else if (current % 2 == 0) {
current /= 2;
}
else {
current = 3 * current + 1;
}
chain++;
}
st.put(i, chain);
StdOut.println(st.get(i));
if (chain > max) max = i;
}
StdOut.println(max);
}
}