-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathGenerateAListOfPrimes.java
More file actions
39 lines (36 loc) · 893 Bytes
/
GenerateAListOfPrimes.java
File metadata and controls
39 lines (36 loc) · 893 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
31
32
33
34
35
36
37
38
39
package chapter06MathAndLogicPuzzles;
/**
*
* Solution: We cross off all numbers divisible by 2. Then, we look for the next
* prime(the next non-crossed off number) and cross off all numbers divisible by
* it.
*
*/
public class GenerateAListOfPrimes {
public boolean[] sieveOfEratosthenes(int max) {
boolean[] flags = new boolean[max + 1];
for (int i = 0; i <= max; i++) {
flags[i] = true;
}
int prime = 2;
while (prime <= Math.sqrt(max)) {
crossOff(flags, prime);
prime = getNextPrime(flags, prime);
}
return flags;
}
private void crossOff(boolean[] flags, int prime) {
// 4, 6, 8..
// 9, 12, 15...
for (int i = prime * prime; i < flags.length; i += prime) {
flags[i] = false;
}
}
private int getNextPrime(boolean[] flags, int prime) {
int next = prime + 1;
while (next < flags.length && !flags[next]) {
next++;
}
return next;
}
}