Skip to content

Instantly share code, notes, and snippets.

@pallavagarwal07
Created August 27, 2015 09:52
Show Gist options
  • Save pallavagarwal07/70c6f160ccd991c0af17 to your computer and use it in GitHub Desktop.
Save pallavagarwal07/70c6f160ccd991c0af17 to your computer and use it in GitHub Desktop.
Code to generate array of primes
int prime_list[N], len=0;
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;
}
}
}
void updatePrimeList(){
int i=0;
for(i=0; i<N; i++)
if(!seive[i]){
prime_list[len] = i;
len++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment