Created
January 19, 2018 01:53
-
-
Save milon/aecaa8aa7b99c621131e0262c10bed82 to your computer and use it in GitHub Desktop.
Prime Number: Sieve of Eratosthenes
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
| //Sieve of Eratosthenes | |
| //Author: Milon | |
| #include<cstdio> | |
| #include<cmath> | |
| using namespace std; | |
| #define max 19000000 | |
| bool prime[max+1]; | |
| void sieve() { | |
| long int i,j; | |
| for(i=2;i<=max;i++) | |
| prime[i]=true; | |
| for(i=2;i*i<=max;){ | |
| for(j=2*i;j<=max;j+=i){ | |
| prime[j]=false; | |
| } | |
| i++; | |
| while(!prime[i]) | |
| i++; | |
| } | |
| } | |
| int main() { | |
| sieve(); | |
| long int n; | |
| while(scanf("%ld",&n)==1){ | |
| if(prime[n]==true) | |
| printf("prime\n"); | |
| else | |
| printf("not prime\n"); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment