-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrime.java
More file actions
282 lines (229 loc) · 6.1 KB
/
Prime.java
File metadata and controls
282 lines (229 loc) · 6.1 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**
* Prime.java
* @author Herb Wolfe, Jr <hwolfe71@gmail.com>
*
* This program displays a list of prime numbers
* and provides other functions for determining working with primes.
*
*/
import static java.lang.System.*;
import java.util.*;
import java.io.*;
class Prime {
private List <Long> primes;
private static Long firstPrime = new Long(2); // First Prime number
/**
* Interactive method to display prime numbers to console.
*/
public static void main(String [] args) {
int count = 1;
boolean goAgain = true;
Prime aPrimeList = new Prime();
Scanner in = new Scanner(System.in);
String answer;
while (goAgain) {
out.println("Prime # " + count + ": "
+ aPrimeList.getPrime(count));
count++;
out.println("Would you like to see prime # " + count
+ "? (Y/N): ");
answer = in.next();
answer = answer.toUpperCase().substring(0,1);
goAgain = (answer.equals("Y"));
if (goAgain) {
aPrimeList.addNextPrime();
}
}
aPrimeList.printAllPrimesToFile();
return;
}
/**
* Default constructor - Creates a new list of 1 prime.
*/
public Prime() {
this(1);
}
/**
* Constructs a list of primes of the specified size.
* @param size - the number of primes in the list.
*/
public Prime(int size) {
this.primes = new ArrayList<Long>();
this.primes.add(firstPrime);
this.growToSize(size);
}
/**
* Calculate the next prime number, adding it to the List
* Uses the sieve of Eratosthenes to find the next prime
*/
private void addNextPrime() {
long nextPrime = getLastPrime();
if (nextPrime == 2) {
nextPrime = 3;
} else {
nextPrime += 2;
while (hasFactors(nextPrime)) {
// All primes > 2 are odd
nextPrime += 2;
}
}
this.primes.add(nextPrime);
}
/**
* Grow the list to at least size primes
* @param size - the minimum number of primes.
*/
public void growToSize(int size) {
while (size > primes.size()) {
addNextPrime();
}
}
/**
* Grow the list until the last number is larger than value.
* @param value - the minimum number of primes.
*/
public void growToValue(long value) {
while (value > this.getLastPrime()) {
addNextPrime();
}
}
/**
* Print the current List of primes to stdout
*/
public void printAllPrimes() {
printPrimes(primes.size());
}
/**
* Print the first num primes to stdout
* @param num - how many primes to print
*/
public void printPrimes(int num) {
this.growToSize(num);
for (Long p : this.primes.subList(0, num) ) {
System.out.println(p);
}
}
/**
* Print the current List of primes to a file, primes.txt
*/
private void printAllPrimesToFile() {
printPrimesToFile(primes.size());
}
/**
* Print the first num primes to a file, primes.txt
* @param num - how many primes to print
*/
private void printPrimesToFile(int num) {
try {
PrintWriter pwout = new PrintWriter("primes.txt");
this.growToSize(num);
for (Long p : this.primes.subList(0, num) ) {
pwout.println(p);
}
pwout.close();
} catch (FileNotFoundException ex) {
out.println(ex.getMessage());
out.println("in " + System.getProperty("user.dir"));
System.exit(1);
}
}
/**
* Get the nth prime number
* @param nth - the index of the prime number
* @return - the prime number at the given index
*/
public long getPrime(int nth) {
this.growToSize(nth);
return this.primes.get(nth - 1);
}
/**
* Return the index of the prime number, if it exists
* @param num - the number to search for
* @return - the index of the number or -1 if it is not in the list
*/
private int indexOf(int num) {
return indexOf((long)num);
}
/**
* Return the index of the prime number, if it exists
* @param num - the number to search for
* @return - the index of the number or -1 if it is not in the list
*/
private int indexOf(long num) {
return this.primes.indexOf(num);
}
/**
* Get the last prime number in the List
* @return - the last prime number currently in the list
*/
public long getLastPrime() {
return getPrime(this.getNumPrimes());
}
/**
* How many primes are in the list
* @return - the number of primes in the list
*/
public int getNumPrimes() {
return this.primes.size();
}
/**
* Determine whether the number is prime
* @param num - the number to be tested
* @return whether or not the number is prime
*/
public boolean isPrime(long num) {
this.growToValue(num);
return ((num > 1) && (this.indexOf(num) > -1));
}
/**
* int version of above
* @param num - the integer to test
* @return whether or not the number is prime
*/
public boolean isPrime(int num) {
return isPrime((long)num);
}
/**
* Determine whether the number has any (prime) factors - used
* internally to grow list of primes.
* @param num - the number to check
* @return whether or not the number has any prime factors
*/
private boolean hasFactors(long num) {
boolean factors = false;
// The largest factor of a number is it's square root.
int max = (int)Math.ceil(Math.sqrt(num));
for (Long p : this.primes) {
factors = (num % p == 0);
if (factors || p > max) {
break;
}
}
return factors;
}
/**
* Print the prime factors of a number
* @param num - the number to print the factors
*/
public void printFactors(long num) {
String factorList = new String();
this.growToValue(num);
if (num < 2) {
out.println(num + " is less than 2 and therefore has no prime factors!");
} else if (this.indexOf(num) > -1) {
out.println(num + " is a prime, therefore it has only one prime factor, itself!");
} else {
while (num > 1) {
for (Long p : primes) {
if (num % p == 0) {
factorList = factorList + Long.toString(p) + " ";
while (num % p == 0) {
num /= p;
}
}
}
}
out.println(factorList);
}
}
}