Skip to content

Commit e5024f0

Browse files
committed
Sieve Time Complexity Added
1 parent 2761b96 commit e5024f0

File tree

3 files changed

+47
-14
lines changed

3 files changed

+47
-14
lines changed

Algorithm/22 Sieve of Eratosthenes.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11

2+
/**
3+
4+
Sieve of Eratosthenes
5+
=====================
6+
7+
Time Complexity : O( N log(logN) )
8+
Memory Complexity : O( N )
9+
10+
**/
11+
212
/** Which of the favors of your Lord will you deny ? **/
313

414
#include<bits/stdc++.h>

Algorithm/23 Bit Sieve.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11

2+
/**
3+
4+
Bit Sieve
5+
=========
6+
7+
Time Complexity : O( N log(logN) )
8+
Memory Complexity : N bit
9+
10+
**/
11+
212
/** Which of the favors of your Lord will you deny ? **/
313

414
#include<bits/stdc++.h>

Algorithm/24 Segmented Sieve.cpp

+27-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11

2+
/**
3+
4+
Segmented Sieve
5+
===============
6+
7+
Precalculating primes : O( sqrt(R) log(log(sqrt(R))) )
8+
Later segmented sieve : O( (R-L+1) log(logR) )
9+
10+
So,
11+
Time Complexity : O( (R-L+1) log(logR) + sqrt(R) log(log(sqrt(R))) )
12+
13+
**/
14+
215
/** Which of the favors of your Lord will you deny ? **/
316

417
#include<bits/stdc++.h>
@@ -67,17 +80,17 @@ string to_str(T x)
6780
6881
Segmented Sieve
6982
70-
Range of [A,B] ~ 10^5
83+
Range of [L,R] ~ 10^5
7184
7285
**/
7386

74-
const int pnmax = 1e6+10;
75-
const int pnmax2 = 1e5+10; /** diff of B and A **/
87+
const int pnmax = 1e6+10; /** ~ sqrt(R) **/
88+
const int pnmax2 = 1e5+10; /** diff of R and L **/
7689

77-
LL LIM = 1e6+5;
90+
LL LIM = 1e6+5; /** ~ sqrt(R) **/
7891
vector<LL>primes;
79-
bool isP[nmax];
80-
bool isPFinal[nmax2];
92+
bool isP[pnmax];
93+
bool isPFinal[pnmax2];
8194

8295
void sieve()
8396
{
@@ -95,31 +108,31 @@ void sieve()
95108
}
96109
}
97110

98-
void segmented_sieve(LL A ,LL B)
111+
void segmented_sieve(LL L ,LL R)
99112
{
100-
for(LL i=0; i<=nmax2; i++)
113+
for(LL i=0; i<=pnmax2; i++)
101114
isPFinal[i]=true;
102115

103116
for(LL i=0;i<primes.size();i++)
104117
{
105118
LL p=primes[i];
106119
LL j=p*p;
107120

108-
if(j<A)
109-
j=((A+p-1)/p)*p;
121+
if(j<L)
122+
j=((L+p-1)/p)*p;
110123

111-
for(;j<=B;j+=p)
112-
isPFinal[j-A]=false;
124+
for(;j<=R;j+=p)
125+
isPFinal[j-L]=false;
113126

114127
}
115128

116129
/** print **/
117-
for(LL i=A;i<=B;i++)
130+
for(LL i=L;i<=R;i++)
118131
{
119132
if(i==1LL)
120133
continue;
121134

122-
if(isPFinal[i-A])
135+
if(isPFinal[i-L])
123136
cout<<i<<endl;
124137
}
125138
}

0 commit comments

Comments
 (0)