Skip to content

Commit 47bbbe2

Browse files
author
Nadim-Mahmud
committed
O(n) sieve added
1 parent 0b79406 commit 47bbbe2

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Source/Number Theory/SIeve O_n.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
Description : pr stores all primes and lp stores lowest prime factors
3+
Complexity : O(n)
4+
*/
5+
6+
const int N = 10000000;
7+
int lp[N+1];
8+
vector<int> pr;
9+
10+
void sieve(){
11+
int i,j;
12+
for (i=2; i<=N; ++i) {
13+
if (lp[i] == 0) {
14+
lp[i] = i;
15+
pr.push_back (i);
16+
}
17+
for (j=0; j<(int)pr.size() && pr[j]<=lp[i] && i*pr[j]<=N; ++j)
18+
lp[i*pr[j]] = pr[j];
19+
}
20+
}

0 commit comments

Comments
 (0)