Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prime Numbers Lecture L4.4 Sieve of Eratosthenes.

Similar presentations


Presentation on theme: "Prime Numbers Lecture L4.4 Sieve of Eratosthenes."— Presentation transcript:

1 Prime Numbers Lecture L4.4 Sieve of Eratosthenes

2 // first create an array of flags, where // each member of the array stands for a odd number // starting with 3. for (j = 0; j < size; j++) { flags[j] = 1; } Find prime numbers using Sieve of Eratosthenes j 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 size = 1024

3 Find prime numbers using Sieve of Eratosthenes j 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 primeCount = 0; for (j = 0; j < size; j++) { if (flags[j] == 1) { // found a prime, count it primeCount++; // now set all of the multiples of // the current prime number to false // because they couldn't possibly be prime a = 2*j + 3;// odd numbers starting with 3 b = a + j; while(b < size) { flags[b] = 0; b = b + a; } a b 3 3 5 6 7 9 9 12 1115 13 18 15 21 17 24 19 27 21 30 23 33 25 36 27 39 29 42 31 45 33 48

4 Find prime numbers using Sieve of Eratosthenes j 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 primeCount = 0; for (j = 0; j < size; j++) { if (flags[j] == 1) { // found a prime, count it primeCount++; // now set all of the multiples of // the current prime number to false // because they couldn't possibly be prime a = 2*j + 3;// odd numbers starting with 3 b = a + j; while(b < size) { flags[b] = 0; b = b + a; } a b 3 3 5 6 7 9 9 12 1115 13 18 15 21 17 24 19 27 21 30 23 33 25 36 27 39 29 42 31 45 33 48

5 Find prime numbers using Sieve of Eratosthenes j 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 primeCount = 0; for (j = 0; j < size; j++) { if (flags[j] == 1) { // found a prime, count it primeCount++; // now set all of the multiples of // the current prime number to false // because they couldn't possibly be prime a = 2*j + 3;// odd numbers starting with 3 b = a + j; while(b < size) { flags[b] = 0; b = b + a; } a b 3 3 5 6 7 9 9 12 1115 13 18 15 21 17 24 19 27 21 30 23 33 25 36 27 39 29 42 31 45 33 48

6 Find prime numbers using Sieve of Eratosthenes j 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 primeCount = 0; for (j = 0; j < size; j++) { if (flags[j] == 1) { // found a prime, count it primeCount++; // now set all of the multiples of // the current prime number to false // because they couldn't possibly be prime a = 2*j + 3;// odd numbers starting with 3 b = a + j; while(b < size) { flags[b] = 0; b = b + a; } a b 3 3 5 6 7 9 9 12 1115 13 18 15 21 17 24 19 27 21 30 23 33 25 36 27 39 29 42 31 45 33 48

7 Find prime numbers using Sieve of Eratosthenes j 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 1 1 0 1 1 0 1 1 0 1 1 0 1 1 1 primeCount = 0; for (j = 0; j < size; j++) { if (flags[j] == 1) { // found a prime, count it primeCount++; // now set all of the multiples of // the current prime number to false // because they couldn't possibly be prime a = 2*j + 3;// odd numbers starting with 3 b = a + j; while(b < size) { flags[b] = 0; b = b + a; } a b 3 3 5 6 7 9 9 12 1115 13 18 15 21 17 24 19 27 21 30 23 33 25 36 27 39 29 42 31 45 33 48

8 Find prime numbers using Sieve of Eratosthenes j 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 primeCount = 0; for (j = 0; j < size; j++) { if (flags[j] == 1) { // found a prime, count it primeCount++; // now set all of the multiples of // the current prime number to false // because they couldn't possibly be prime a = 2*j + 3;// odd numbers starting with 3 b = a + j; while(b < size) { flags[b] = 0; b = b + a; } a b 3 3 5 6 7 9 9 12 1115 13 18 15 21 17 24 19 27 21 30 23 33 25 36 27 39 29 42 31 45 33 48

9 Find prime numbers using Sieve of Eratosthenes j 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 1 1 0 1 1 0 1 1 0 1 0 0 1 1 0 primeCount = 0; for (j = 0; j < size; j++) { if (flags[j] == 1) { // found a prime, count it primeCount++; // now set all of the multiples of // the current prime number to false // because they couldn't possibly be prime a = 2*j + 3;// odd numbers starting with 3 b = a + j; while(b < size) { flags[b] = 0; b = b + a; } a b 3 3 5 6 7 9 9 12 1115 13 18 15 21 17 24 19 27 21 30 23 33 25 36 27 39 29 42 31 45 33 48


Download ppt "Prime Numbers Lecture L4.4 Sieve of Eratosthenes."

Similar presentations


Ads by Google