-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimeFactorsExercise.java
More file actions
190 lines (151 loc) · 4.55 KB
/
Copy pathPrimeFactorsExercise.java
File metadata and controls
190 lines (151 loc) · 4.55 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package tutor;
import java.util.Scanner;
public class PrimeFactorsExercise {
public static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int decision;
do {
System.out.println("\n\n");
System.out.println("-----------------");
System.out.println("Hello there, what solution would you like to use?");
System.out.println("-----------------");
System.out.println("-- String [1] --");
System.out.println("-- Array [2] --");
System.out.println("-- Methods [3] --");
System.out.println("-- Exit [0] --");
System.out.println("-----------------");
System.out.print ("> ");
decision = scanner.nextInt();
System.out.println("\n\n");
if (decision == 1) {
System.out.println("-- Now using the string solution --");
PrimeFactorsStringSolution();
} else if (decision == 2) {
System.out.println("-- Now using the array solution --");
PrimeFactorsArraySolution();
} else if (decision == 3) {
System.out.println("-- Now using the method solution --");
PrimeFactorsMethodSolution();
}
} while (decision != 0);
System.out.println("Make sure to know how this code works... Good Luck");
}
public static void PrimeFactorsStringSolution() {
int given, number;
int divisor;
int decision;
String factors;
do {
System.out.print("Enter a number: ");
given = number = scanner.nextInt();
divisor = 2;
factors = "";
while (number > 1) { //4, 0
while (number % divisor == 0) { // 4%2=0; 2%2=0; 0%2=false
factors += divisor; // 2, 2
factors = (number % divisor == 0 && number/divisor != 1) ? factors + ", " : factors;
number /= divisor; // 4/2 = 2; 2/2=0
}
divisor++;
}
System.out.print("Prime factors for " + given + " are:");
System.out.println(" " + factors);
System.out.println("Continue? Yes[1] No[0]");
System.out.print("> ");
decision = scanner.nextInt();
} while (decision != 0);
}
public static void PrimeFactorsArraySolution() {
int given, number;
int divisor;
int decision;
int index, size;
int[] factors, temp;
do {
System.out.print("Enter a number: ");
given = number = scanner.nextInt();
divisor = 2;
index = size = 0;
factors = new int[size];
while (number > 1) {
while (number % divisor == 0) {
size++;
temp = factors;
factors = new int[size];
for (int i = 0; i < temp.length; i++) {
factors[i] = temp[i];
}
factors[index] = divisor;
number /= divisor;
index++;
}
divisor++;
}
System.out.print("Prime factors for " + given + " are:");
System.out.print(" ");
for (int i = 0; i < factors.length; i++) {
System.out.print(factors[i]);
if (i+1 != factors.length) {
System.out.print(", ");
}
}
size = 0;
index = 0;
factors = new int[size];
System.out.println("\nContinue? Yes[1] No[0]");
System.out.print("> ");
decision = scanner.nextInt();
} while (decision != 0);
}
public static void PrimeFactorsMethodSolution() {
int number;
int decision;
int[] factors;
do {
System.out.print("Enter a number: ");
number = scanner.nextInt();
factors = clearArray();
factors = getPrimeFactors(factors, number);
System.out.print("Prime factors for " + number + " are:");
System.out.print(" " + arrayToString(factors));
System.out.println("\nContinue? Yes[1] No[0]");
System.out.print("> ");
decision = scanner.nextInt();
} while (decision != 0);
}
public static int[] getPrimeFactors(int[] factors, int given) {
int size = 0, index = 0, divisor = 2;
while (given > 1) {
while (given % divisor == 0) {
size++;
factors = increaseArray(factors, size - factors.length);
factors[index] = divisor;
given /= divisor;
index++;
}
divisor++;
}
return factors;
}
public static int[] increaseArray(int[] array, int increaseBy) {
int[] temp = array;
array = new int[array.length + increaseBy];
for (int i = 0; i < temp.length; i++) {
array[i] = temp[i];
}
return array;
}
public static String arrayToString(int[] array) {
String string = "";
for (int i = 0; i < array.length; i++) {
string += array[i];
if (i+1 != array.length) {
string += ", ";
}
}
return string;
}
public static int[] clearArray() {
return new int[0];
}
}