File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments