Created
September 3, 2016 18:55
-
-
Save ffledgling/c6a814354e85735a4b56e98de7f61598 to your computer and use it in GitHub Desktop.
Compute Primes
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
/* Calculate primes upto the first 10^6 numbers | |
* Checked and tested the first 1000 primes for sanity sake | |
*/ | |
#define PRIME_SIZE 500000 // Half of 10^6, we really don't need to store even numbers | |
#define PRIME_SIZE_ROOT 708 // precomputed sqrt(PRIME_SIZE) | |
#include<iostream> | |
#include<cstdio> | |
using namespace std; | |
int main() { | |
// Precompute a sieve of numbers | |
int P[PRIME_SIZE]; | |
memset(P, 0, sizeof(P)); // Set array to 0s | |
int i=0; | |
for(i=3; i<PRIME_SIZE_ROOT; i+=2) { | |
for(int j=3*i; j<PRIME_SIZE; j+=2*i) { | |
P[(j-3)/2]=1; | |
} | |
} | |
for(int k=0; k<PRIME_SIZE; k++) { | |
if(P[k]==0) printf("%d\n", k*2 + 3); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment