-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
109 lines (102 loc) · 2.84 KB
/
Main.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import java.util.*;
import java.io.*;
class Main {
// private static final Scanner console = new Scanner(System.in);
public static void main(String[] args) {
// int n = 0;
// try {
// File myObj = new File("collatzConjecture.txt");
// if (myObj.createNewFile()) {
// System.out.println("File created: " + myObj.getName());
// } else {
// System.out.println("File already exists.");
// }
// } catch (IOException e) {
// System.out.println("An error occurred.");
// e.printStackTrace();
// }
// try {
// FileWriter myWriter = new FileWriter("collatzConjecture.txt", true);
// for (int i = 0; i < 10000; i++) {
// n = (int) (Math.random() * 9998 + 2);
// collatzConjecture(n);
// }
// myWriter.close();
// } catch (IOException e) {
// System.out.println("An error occurred.");
// e.printStackTrace();
// }
// arrayQueue queue = new arrayQueue(1);
// System.out.println("Initial Queue");
// System.out.println(queue);
// for(int i=2; i<9; i++) {
// queue.addQueue(i);
// System.out.println(queue);
// }
// queue.addQueue(9);
// System.out.println(queue);
// queue.deleteQueue();
// System.out.println(queue);
// queue.addQueue(9);
// System.out.println(queue);
// queue.addQueue(10);
// System.out.println(queue);
// for(int i=0; i<9; i++) {
// queue.deleteQueue();
// System.out.println(queue);
// }
// arrayQueue queue = new arrayQueue(8);
// int n = 8;
// for(int i=0; i<19; i++) {
// if(check(n)) {
// queue.addQueue((n-1)/3);
// }
// queue.addQueue(2*n);
// System.out.println("Deleted: " + queue.front());
// queue.deleteQueue();
// n = queue.front();
// }
linkedListQueue queue = new linkedListQueue(8);
int n = 8;
for(int i=0; i<19; i++) {
System.out.println("Deleted: " + queue.front());
if(check(n)) {
queue.addQueue((n-1)/3);
System.out.println("Added: " + (n-1)/3);
}
System.out.println("Added: " + 2*n);
queue.addQueue(2*n);
queue.deleteQueue();
n = queue.front();
}
}
public static void collatzConjecture(int n) {
ArrayList<Integer> list = new ArrayList<Integer>();
int placeholder = n;
list.add(n);
while (n != 1) {
if (n % 2 == 0) {
n /= 2;
} else {
n = 3 * n + 1;
}
list.add(n);
}
try {
FileWriter myWriter = new FileWriter("collatzConjecture.txt", true);
myWriter.append(placeholder + ":\n");
System.out.println("Successfully wrote: " + placeholder + ": to the file.");
myWriter.append(list.size() + "\n\n");
System.out.println("Successfully wrote: " + list.size() + " to the file.");
myWriter.close();
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
// checks if the non-even option is correct
public static boolean check(double n) {
n = (n - 1) / 3;
return (n == (int) n && n % 2 != 0);
}
}