-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linear sieve v1.cpp
94 lines (59 loc) · 1.64 KB
/
Linear sieve v1.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
Copyright (C) Pawel Masluch, 2021. All rights reserved.
Bibliography:
1. https://eduinf.waw.pl/inf/alg/001_search/0012.php
2. https://codeforces.com/blog/entry/54090
3. https://cp-algorithms.com/algebra/prime-sieve-linear.html
4. https://www.cs.utexas.edu/users/misra/scannedPdf.dir/linearSieve.pdf
*/
#include<cstdio>
#include<bitset>
typedef long long LL;
#define REP(i,a,b) for(int i=a; i<=b; ++i)
const int MAX_N = 100000000;
// isPrime[x] = 0 iff x is not prime
// isPrime[x] = 1 iff x is prime
std::bitset<MAX_N+1> isPrime;
void linearSieve(int n){ // we find primes x, for x in {0,1,2,...,n} and MAX_N >= n >= 2
isPrime[0] = isPrime[1] = 0; // 0 and 1 are not primes
REP(i,2,n){
isPrime[i] = 1; // initially, all numbers >= 2 are primes
}
int p=2, q=2;
while( p*q <= n ){
LL P = p;
int k=1;
while( P*q <= n ){ // P*q = p^k * q <= n
isPrime[ P*q ] = 0; // P*q = p^k * q is composite
P *= p;
++k;
}
// we find the smallest number, thought cuurently as prime, bigger than q
do{
++q;
}
while( isPrime[q] == 0 );
if( p*q > n ){ // p*q > n
// we find the smallest number, thought currently as prime, bigger than q
do{
++p;
}
while( isPrime[p] == 0 );
q = p;
}
}
}
int main(){
int n; // we find primes x, for x in {0,1,2,...,n} and MAX_N >= n >= 2
scanf( "%d", &n );
linearSieve(n);
REP(i,std::max(2,n-100),n){
if( isPrime[i] == 0 ){
printf( "isPrime[%d] = 0\n", i );
}
else{
printf( "isPrime[%d] = 1\n", i );
}
}
return 0;
}