Skip to content

Instantly share code, notes, and snippets.

@jzau
Created October 24, 2013 13:31
Show Gist options
  • Select an option

  • Save jzau/7137379 to your computer and use it in GitHub Desktop.

Select an option

Save jzau/7137379 to your computer and use it in GitHub Desktop.
print all prime number which less than N using c++
#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