-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem - 14.java
More file actions
30 lines (28 loc) · 852 Bytes
/
Problem - 14.java
File metadata and controls
30 lines (28 loc) · 852 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
package Projecteuler;
import java.util.Scanner;
class CollatzProblem {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
long in = sc.nextLong();
sc.close();
long leng = 0, temp, max = 0, num = 0;
for (long i = 1; i <= in; i++) {
temp = i;
leng = 0;
while (temp != 1) {
if (temp % 2 == 0) {
temp = temp / 2;
} else {
temp = (3 * temp) + 1;
}
leng++;
}
if (leng > max) {
max = leng;
num = i;
}
}
System.out.println("Longest chain under " + in + " is produced by: " + num);
}
}