Last active
February 27, 2016 03:48
-
-
Save TheoKlein/1cf0d2c83bda70375cb1 to your computer and use it in GitHub Desktop.
ZeroJudge_a007_C
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> | |
| #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