Skip to content

Commit 99242d4

Browse files
authored
Merge pull request #369 from ishikasinha-d/Sieve-of-Eratosthenes
Create Sieve-of-Eratosthenes.c++
2 parents f96f136 + fe44ba9 commit 99242d4

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

Numbers/Sieve-of-Eratosthenes.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Sieve of Eratosthenes
2+
//time complexity = O(n* log(logn))
3+
4+
#include<bits/stdc++.h>
5+
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
6+
using namespace std;
7+
typedef long long int ll;
8+
const ll N= 10e6;
9+
vector<bool> Prime(N+1,true);
10+
using namespace std;
11+
12+
void sieve(){
13+
ll i,j;
14+
Prime[0]=Prime[1]=false;
15+
16+
//adding this otpimization will reduce n/2 computations
17+
//since even no.s are not prime
18+
for(i=2;i<N+1;i+=2)
19+
Prime[i]=false;
20+
21+
for(i=2;i<N+1;i++)
22+
{
23+
if(Prime[i])
24+
for(j=i*i;j<N+1;j+=i)
25+
if(Prime[j])
26+
Prime[j]=false;
27+
}
28+
}
29+
30+
int main()
31+
{
32+
fast;
33+
34+
ll i;
35+
sieve();
36+
for(i=1;i<100;i++)
37+
if(Prime[i])
38+
cout<<i<<" ";
39+
return 0;
40+
41+
}

0 commit comments

Comments
 (0)