-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem-14.java
More file actions
34 lines (28 loc) · 763 Bytes
/
problem-14.java
File metadata and controls
34 lines (28 loc) · 763 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
33
34
public class Problem14 {
public static void main(String[] args) {
int length = 0;
int templen = 0;
int longnum = 0;
for (long i = 1; i < 1000000; i++) {
templen = collatz(i);
if (templen > length) {
length = templen;
longnum = (int) i;
}
}
System.out.println("The number under 1,000,000 with the longest Collatz Sequence: " + longnum);
}
public static int collatz(long a) {
int counter = 1;
while (a != 1) {
if (a % 2 == 0) {
a = a/2;
}
else {
a = 3*a + 1;
}
counter++;
}
return counter;
}
}