Skip to content

Instantly share code, notes, and snippets.

@TheoKlein
Last active February 27, 2016 03:48
Show Gist options
  • Select an option

  • Save TheoKlein/1cf0d2c83bda70375cb1 to your computer and use it in GitHub Desktop.

Select an option

Save TheoKlein/1cf0d2c83bda70375cb1 to your computer and use it in GitHub Desktop.
ZeroJudge_a007_C
#include <stdio.h>
#include <stdlib.h>
int check(int num);
int prime[1000000] = {2,3,5,7};
int i = 4;
void buildprime();
int main() {
int input = 0;
buildprime();
while(scanf("%d",&input) != EOF){
if(check(input))
printf("質數\n");
else
printf("非質數\n");
}
return 0;
}
int check(int num){
int k = 0;
for(k = 0 ; k < i && prime[k] * prime[k] <= num ; k++){
if(num % prime[k] == 0)
return 0;
}
return 1;
}
void buildprime(){
int currentPrime = 7;
int j = 4;
for(i = 4 , j = 4 ; currentPrime < 999999 ; i++ , j = 6 - j){
currentPrime = currentPrime + j;
if(check(currentPrime) == 1)
prime[i]=currentPrime;
else
i--;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment