Last active
August 27, 2015 09:46
-
-
Save pallavagarwal07/8aed41d50f87ccbea45f to your computer and use it in GitHub Desktop.
Sieve of eratosthenes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define N 1000001 | |
int seive[N]; | |
void fillSieve(){ | |
int i, j; | |
seive[0] = seive[1] = 1; // 0, 1 aren't primes | |
int n = sqrt(N); | |
i = 2; | |
for(j=2; i*j<N; j++) | |
seive[i*j] = 1; | |
for(i=3; i<=n; i+=2){ | |
if(seive[i]) // Anything marked 1 isn't prime | |
continue; | |
for(j=2; i*j<N; j++){ | |
seive[i*j] = 1; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment