Skip to content

Instantly share code, notes, and snippets.

@pallavagarwal07
Last active August 27, 2015 09:46
Show Gist options
  • Save pallavagarwal07/8aed41d50f87ccbea45f to your computer and use it in GitHub Desktop.
Save pallavagarwal07/8aed41d50f87ccbea45f to your computer and use it in GitHub Desktop.
Sieve of eratosthenes
#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