Skip to content

Instantly share code, notes, and snippets.

@niyaton
Last active December 18, 2015 16:49
Show Gist options
  • Save niyaton/5814324 to your computer and use it in GitHub Desktop.
Save niyaton/5814324 to your computer and use it in GitHub Desktop.
割とお手軽で高速な素数計算プログラムの決定版?
#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