Skip to content

Instantly share code, notes, and snippets.

@bgnori
Created May 18, 2012 05:52
Show Gist options
  • Save bgnori/2723445 to your computer and use it in GitHub Desktop.
Save bgnori/2723445 to your computer and use it in GitHub Desktop.
#include<stdio.h>
#include<time.h>
#define N (10*1000*1000)
int table[N+1];
int uncheck_min = 0;
int calc(int x){
int i;
for (i = 2; i*i <= x; i++){
if(isprime(i)){
if (0 == (x% i )){
return 0;
}
}
}
return 1;
}
int isprime(int n){
while (!(n < uncheck_min)){
table[uncheck_min] = calc(uncheck_min);
uncheck_min += 1;
}
return table[n];
}
int main(){
int i;
clock_t start, end;
FILE* fp;
table[2] = 1;
uncheck_min = 3;
start = clock();
for (i = 0; i <=N; i++){
isprime(i);
}
end = clock();
printf("%f s\n", (double)(end - start)/CLOCKS_PER_SEC);
printf("%d\n", CLOCKS_PER_SEC);
fp = fopen("result.txt", "w");
for (i = 0; i <=N; i++){
if(table[i]){
fprintf(fp, "%d\n", i);
}
}
fclose(fp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment