-
Notifications
You must be signed in to change notification settings - Fork 19.6k
/
Copy pathSieveOfEratosthenes.java
66 lines (57 loc) · 1.99 KB
/
SieveOfEratosthenes.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
package com.thealgorithms.maths;
import java.util.Arrays;
/**
* @brief utility class implementing <a href="https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes">Sieve of Eratosthenes</a>
*/
public final class SieveOfEratosthenes {
private SieveOfEratosthenes() {
}
private static void checkInput(int n) {
if (n <= 0) {
throw new IllegalArgumentException("n must be positive.");
}
}
private static Type[] sievePrimesTill(int n) {
checkInput(n);
Type[] isPrimeArray = new Type[n + 1];
Arrays.fill(isPrimeArray, Type.PRIME);
isPrimeArray[0] = Type.NOT_PRIME;
isPrimeArray[1] = Type.NOT_PRIME;
double cap = Math.sqrt(n);
for (int i = 2; i <= cap; i++) {
if (isPrimeArray[i] == Type.PRIME) {
for (int j = 2; i * j <= n; j++) {
isPrimeArray[i * j] = Type.NOT_PRIME;
}
}
}
return isPrimeArray;
}
private static int countPrimes(Type[] isPrimeArray) {
return (int) Arrays.stream(isPrimeArray).filter(element -> element == Type.PRIME).count();
}
private static int[] extractPrimes(Type[] isPrimeArray) {
int numberOfPrimes = countPrimes(isPrimeArray);
int[] primes = new int[numberOfPrimes];
int primeIndex = 0;
for (int curNumber = 0; curNumber < isPrimeArray.length; ++curNumber) {
if (isPrimeArray[curNumber] == Type.PRIME) {
primes[primeIndex++] = curNumber;
}
}
return primes;
}
/**
* @brief finds all of the prime numbers up to the given upper (inclusive) limit
* @param n upper (inclusive) limit
* @exception IllegalArgumentException n is non-positive
* @return the array of all primes up to the given number (inclusive)
*/
public static int[] findPrimesTill(int n) {
return extractPrimes(sievePrimesTill(n));
}
private enum Type {
PRIME,
NOT_PRIME,
}
}