Last active
December 18, 2015 16:49
-
-
Save niyaton/5814324 to your computer and use it in GitHub Desktop.
割とお手軽で高速な素数計算プログラムの決定版?
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
#include <stdio.h> | |
#define MAX 1000000 | |
int is_prime(int n){ | |
for(int i = 6; (i-1) * (i-1) <= n; i+=6){ | |
if( n % (i-1) == 0 || n %(i+1) == 0) | |
return 0; | |
} | |
return 1; | |
} | |
int main(){ | |
int c = 2; | |
printf("2\n3\n"); | |
for(int i = 6; i < MAX; i += 6){ | |
if(is_prime(i-1)) | |
printf("%d\n", i-1); | |
if(is_prime(i+1)) | |
printf("%d\n", i+1); | |
} | |
printf("%d",c); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment