Created
February 12, 2013 13:11
-
-
Save rohith2506/4762960 to your computer and use it in GitHub Desktop.
To calculate prime numbers using sieve of erosthenes
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
/* | |
to calculate prime numbers using sieve of erosthenes | |
@author:rohit | |
*/ | |
#include <iostream> | |
#include <stdio.h> | |
#include <cmath> | |
#include <stdlib.h> | |
using namespace std; | |
int main(){ | |
int n; | |
cin>>n; | |
bool prime[n+1]; | |
int m=(int)(sqrt(n)); | |
for(int i=0;i<n+1;i++) | |
prime[i]=true; | |
prime[0]=false; | |
prime[1]=false; | |
cout<<m<<endl; | |
for(int i=2;i<=m;i++){ | |
if(prime[i]){ | |
for(int j=i*i;j<=n;j=j+i){ | |
prime[j]=false; | |
} | |
} | |
} | |
for(int i=0;i<n;i++) | |
if(prime[i]) | |
cout<<i<<" "; | |
cout<<endl; | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment