-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathCollatz-Conjecture.java
47 lines (37 loc) · 2.01 KB
/
Collatz-Conjecture.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
36
37
38
39
40
41
42
43
44
45
46
47
/*******
* SHaDeX0
* 09 - 10 - 2021 (Saturday)
* Collatz conjecture also called '3x+1 Problem'
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class _3xPlus1 {
public static void main(String[] args) {
System.out.println("Enter a number:");
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt(); //Input number to find its 3x + 1 expansion
ArrayList<Integer> maxMin = new ArrayList<>(); //To find max and min value of 'num's 3x + 1 expansion
System.out.println("Its 3x + 1 expansion:");
while (num != 1) {
System.out.print(num + " "); //Printing the expansion
maxMin.add(num); //Adding 'num' to the ArrayList
if (num % 2 == 0) { //If 'num' is even perform even operation
num = evenOperation(num);
} else { //If 'num' is odd perform odd operation
num = oddOperation(num);
}
}
System.out.print(num + " "); //Printing the last number as after the operations, num = 1 always
maxMin.add(num); //Adding 'num' = 1 to the ArrayList
System.out.println("\nMax value: " + Collections.max(maxMin) + //Displaying Max value in the expansion,
"\nMin Value: " + Collections.min(maxMin) + //Min value in the expansion and
"\nNumber of iterations: " + maxMin.size()); //Number of iterations 'num' took to reduce to 1
}
private static int evenOperation(int num) { //Function for doing the even operation
return (num / 2); //Halving 'num'
}
private static int oddOperation(int num) { //Function for doing the odd operation
return ((3 * num) + 1); //Multiplying 'num' and incrementing it by 1
}
}