-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollatz.java
More file actions
30 lines (22 loc) · 936 Bytes
/
Collatz.java
File metadata and controls
30 lines (22 loc) · 936 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
import java.util.Scanner;
class Main {
public static void main(String[] args) {
int steps = 0;
System.out.println("Enter a number:");
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int x = n;
while(n > 1){
if(n % 2 == 0) {
n /= 2;
steps++;
System.out.println(n);
}
else { n = (n * 3) + 1;
steps++;
System.out.println(n);
}
}
System.out.println("It takes \'" + steps + "\' steps to reach \'1\' using the Collatz conjecture on the number \'" + x +"\'.");
}
}