Created
October 24, 2013 13:31
-
-
Save jzau/7137379 to your computer and use it in GitHub Desktop.
print all prime number which less than N using 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<iostream> | |
| #include<cmath> | |
| #define N 200000 | |
| using namespace std; | |
| bool prime[N]; | |
| int main() | |
| { | |
| int i, j; | |
| prime[0] = prime[1] = false; | |
| prime[2] = true; | |
| for(i=3; i<N; i++) | |
| { | |
| if(i%2) | |
| prime[i]=true; | |
| else | |
| prime[i] = false; | |
| } | |
| for(i=3; i<=sqrt(N*1.0); i=i+2) | |
| { | |
| if(prime[i]) | |
| { | |
| for(j=i+i; j<N; j+=i) | |
| prime[j]=false; | |
| } | |
| } | |
| for(i=2; i<N; i++) | |
| { | |
| if( prime[i] ) | |
| cout << i << endl; | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment