|
1 | 1 | package Others;
|
2 | 2 |
|
3 |
| -/** @author Varun Upadhyay (https://github.com/varunu28) */ |
| 3 | +import java.util.Arrays; |
| 4 | + |
| 5 | +/** |
| 6 | + * Sieve of Eratosthenes is an ancient algorithm for finding all prime numbers up to any given limit. |
| 7 | + * It does so by iteratively marking as composite (i.e., not prime) the multiples of each prime, |
| 8 | + * starting with the first prime number, 2. |
| 9 | + * The multiples of a given prime are generated as a sequence of numbers starting from that prime, |
| 10 | + * with constant difference between them that is equal to that prime. |
| 11 | + * This is the sieve's key distinction from using trial division to sequentially test each |
| 12 | + * candidate number for divisibility by each prime. |
| 13 | + * Once all the multiples of each discovered prime have been marked as composites, the remaining |
| 14 | + * unmarked numbers are primes. |
| 15 | + * <p> |
| 16 | + * Poetry about Sieve of Eratosthenes: |
| 17 | + * <p><i>Sift the Two's and Sift the Three's:</i></p> |
| 18 | + * <p><i>The Sieve of Eratosthenes.</i></p> |
| 19 | + * <p><i>When the multiples sublime,</i></p> |
| 20 | + * <p><i>The numbers that remain are Prime.</i></p> |
| 21 | + * |
| 22 | + * @see <a href="https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes">Wiki</a> |
| 23 | + */ |
4 | 24 | public class SieveOfEratosthenes {
|
5 | 25 |
|
6 | 26 | /**
|
7 |
| - * This method implements the Sieve of Eratosthenes Algorithm |
8 |
| - * |
9 |
| - * @param n The number till which we have to check for prime Prints all the prime numbers till n |
| 27 | + * @param n The number till which we have to check for prime Prints all the prime numbers till n. |
| 28 | + * Should be more than 1. |
| 29 | + * @return array of all prime numbers between 0 to n |
10 | 30 | */
|
11 |
| - public static void findPrimesTillN(int n) { |
12 |
| - int[] arr = new int[n + 1]; |
13 |
| - |
14 |
| - for (int i = 0; i <= n; i++) { |
15 |
| - arr[i] = 1; |
16 |
| - } |
| 31 | + public static int[] findPrimesTill(int n) { |
| 32 | + // Create array where index is number and value is flag - is that number a prime or not. |
| 33 | + // size of array is n + 1 cause in Java array indexes starts with 0 |
| 34 | + Type[] numbers = new Type[n + 1]; |
17 | 35 |
|
18 |
| - arr[0] = arr[1] = 0; |
| 36 | + // Start with assumption that all numbers except 0 and 1 are primes. |
| 37 | + Arrays.fill(numbers, Type.PRIME); |
| 38 | + numbers[0] = numbers[1] = Type.NOT_PRIME; |
19 | 39 |
|
20 |
| - for (int i = 2; i <= Math.sqrt(n); i++) { |
21 |
| - if (arr[i] == 1) { |
| 40 | + double cap = Math.sqrt(n); |
| 41 | + // Main algorithm: mark all numbers which are multiples of some other values as not prime |
| 42 | + for (int i = 2; i <= cap; i++) { |
| 43 | + if (numbers[i] == Type.PRIME) { |
22 | 44 | for (int j = 2; i * j <= n; j++) {
|
23 |
| - arr[i * j] = 0; |
| 45 | + numbers[i * j] = Type.NOT_PRIME; |
24 | 46 | }
|
25 | 47 | }
|
26 | 48 | }
|
27 | 49 |
|
| 50 | + //Write all primes to result array |
| 51 | + int primesCount = (int) Arrays.stream(numbers) |
| 52 | + .filter(element -> element == Type.PRIME) |
| 53 | + .count(); |
| 54 | + int[] primes = new int[primesCount]; |
| 55 | + |
| 56 | + int primeIndex = 0; |
28 | 57 | for (int i = 0; i < n + 1; i++) {
|
29 |
| - if (arr[i] == 1) { |
30 |
| - System.out.print(i + " "); |
| 58 | + if(numbers[i] == Type.PRIME) { |
| 59 | + primes[primeIndex++] = i; |
31 | 60 | }
|
32 | 61 | }
|
33 | 62 |
|
34 |
| - System.out.println(); |
| 63 | + return primes; |
| 64 | + } |
| 65 | + |
| 66 | + private enum Type { |
| 67 | + PRIME, NOT_PRIME |
35 | 68 | }
|
36 | 69 |
|
37 |
| - // Driver Program |
38 | 70 | public static void main(String[] args) {
|
39 | 71 | int n = 100;
|
40 |
| - |
41 |
| - // Prints 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 |
42 |
| - findPrimesTillN(n); |
| 72 | + System.out.println("Searching for all primes from zero to " + n); |
| 73 | + int[] primes = findPrimesTill(n); |
| 74 | + System.out.println("Found: " + Arrays.toString(primes)); |
43 | 75 | }
|
44 | 76 | }
|
0 commit comments