Created
August 27, 2015 09:52
-
-
Save pallavagarwal07/70c6f160ccd991c0af17 to your computer and use it in GitHub Desktop.
Code to generate array of primes
This file contains hidden or 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
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