### Advanced algorithm: the Sieve of Eratosthenes

• Sieve of Eratosthenes

Sieve of Eratosthenes

 In Mathematics, the sieve of Eratosthenes, is a simple, ancient algorithm for finding all prime numbers up to a specified integer N The algorithm is named after Eratosthenes of Cyrene, an ancient Greek mathematician. The sieve algorithm was described and attributed to Eratosthenes in the Introduction to Arithmetic by Nicomachus. Reference: Wikipedia

• How to find all prime numbers upto N

• Illustrative example: find all primes upto 120

 ``` 2 3 4 5 6 7 8 9 ..... 120 ```

• Let's re-arrange it in a way that we can display all candidates:

 ``` 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 ```

• Find the first unmarked number: 2

Delete all number that are divisible by 2:

 ``` 2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 101 103 105 107 109 111 113 115 117 119 ```

• Find the next unmarked number: 3

Delete all number that are divisible by 3:

 ``` 2 3 5 7 11 13 17 19 23 25 29 31 35 37 41 43 47 49 53 55 59 61 65 67 71 73 77 79 83 85 89 91 95 97 101 103 107 109 113 115 119 ```

• Find the next unmarked number: 5

Delete all number that are divisible by 5:

 ``` 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 49 53 59 61 67 71 73 77 79 83 89 91 97 101 103 107 109 113 119 ```

• And so on...

Repeat until you process all numbers in the list.

The red numbers (marked) are prime numbers

• Here is a nice animation from Wikipedia:

• The Sieve of Eratosthenes Algorithm

• Problem description:

• Write a method sieveOfE() with:

 input: the number N output: a boolean array The array element with index i is true if the number i is prime and false otherwise.

 ``` public class PrimeSieve { public static boolean[] sieveOfE( int N ) { .... } } ```

• Pseudo code:

 ``` boolean[] out = new boolean[ N + 1 ]; /* ------------------------ Initialize out[] ------------------------ */ out[0] = false; out[1] = false; set all other values out[i] = true; // They are all candidates /* ------------------------ Perform the sieve of E ------------------------ */ Find the next value out[i] == true; Set: out[2*i] = false, out[3*i] = false, .... (upto out[N]) Find the next value out[i] == true; Set: out[2*i] = false, out[3*i] = false, .... (upto out[N]) And so on... ```

• Refined pseudo code:

 ``` boolean[] out = new boolean[ N + 1 ]; /* ------------------------ Initialize out[] ------------------------ */ out[0] = false; out[1] = false; for ( i = 2; i <= N; i++ ) out[i] = true; // They are all candidates /* ------------------------ Perform the sieve of E ------------------------ */ k = 1; // Start before the first prime number while ( k <= N ) { Starting from k+1, find next prime number i; Set: out[2*i] = false, out[3*i] = false, .... (upto out[N]) k = i; } ```

• Java Method:

 ``` public static boolean[] sieveOfE ( int N ) { boolean[] isPrime = new boolean[ N + 1 ]; // Return variable int i, j, k; /* ------------------------ Initialize isPrime[] ------------------------ */ isPrime[0] = false; // 0 is not prime isPrime[1] = false; // 1 is not prime for ( i = 2; i <= N; i++ ) isPrime[i] = true; // All other number are possible candidates /* ------------------------ Perform the sieve of E ------------------------ */ k = 2; // Start with 2 to find all primes while ( k <= N ) { /* ---------------------------------------- Starting from k, find next prime number number i A prime number is detected by: isPrime[i] == true ---------------------------------------- */ for ( i = k; i <= N; i++ ) if ( isPrime[i] ) break; // Found ! /* -------------------------------------- Set: isPrime[2*i] = false, isPrime[3*i] = false, .... (upto isPrime[N]) -------------------------------------- */ for ( j = 2*i; j <= N; j = j + i ) isPrime[j] = false; k = i+1; // Set up k for next iteration !!! } return isPrime; } ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac Primes1.java To run:          java Primes1