-
Notifications
You must be signed in to change notification settings - Fork 12
/
sieve.cpp
51 lines (44 loc) · 881 Bytes
/
sieve.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/// *** Normal Sieve [ n*sqrt(n) ]
/// [ mark can be check for is prime ]
#define SZ 100009
int pr[78600],in=0;
bool mr[SZ+3];
void sieve(){
int i,j,sq,p;
sq=sqrt(SZ)+2;
mr[1]=1;
for(i=2; i<sq; i++){
if(!mr[i]){
for(j=i*i;j<=SZ;j+=i){
mr[j]=1;
}
}
}
for(i=2; i<SZ; i++){
if(!mr[i]){
pr[++in] = i;
}
}
}
/// *** Efficient Sieve [ n*log(n)~]
/// [mr array can't be use for prime check || Odd numbers only]
#define SZ 1000009
int pr[78600],in=0;
bool mr[SZ+3];
void sieve(){
int i,j,sq,p;
sq=sqrt(SZ)+2;
for(i=3; i<sq; i+=2){
if(!mr[i]){
for(j=i*i; j<=SZ; j+=i<<1){
mr[j]=1;
}
}
}
pr[++in] = 2;
for(i=3; i<SZ; i+=2){
if(!mr[i]){
pr[++in] = i;
}
}
}