Prime Numbers Lecture L4.4 Sieve of Eratosthenes
// 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 size = 1024
Find prime numbers using Sieve of Eratosthenes j 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
Find prime numbers using Sieve of Eratosthenes j 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
Find prime numbers using Sieve of Eratosthenes j 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
Find prime numbers using Sieve of Eratosthenes j 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
Find prime numbers using Sieve of Eratosthenes j 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
Find prime numbers using Sieve of Eratosthenes j 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
Find prime numbers using Sieve of Eratosthenes j 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