Skip to content

Commit 941880d

Browse files
author
Nadim-Mahmud
committed
Prime factorization on log n
1 parent a07d79d commit 941880d

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
Description : prime factorization
3+
Complexity : O(log n)
4+
Limitation : memory complexity O(n) so it works up to 10^7
5+
*/
6+
7+
const int N = 10000000;
8+
int lp[N+1];
9+
vector<int> pr;
10+
11+
void sieve(){
12+
int i,j;
13+
for (i=2; i<=N; ++i) {
14+
if (lp[i] == 0) {
15+
lp[i] = i;
16+
pr.push_back (i);
17+
}
18+
for (j=0; j<(int)pr.size() && pr[j]<=lp[i] && i*pr[j]<=N; ++j)
19+
lp[i*pr[j]] = pr[j];
20+
}
21+
}
22+
23+
vector<int> prime_fact(int n){
24+
vector<int> pf;
25+
while (n != 1){
26+
pf.push_back(lp[n]);
27+
n = n / lp[n];
28+
}
29+
return pf;
30+
}

0 commit comments

Comments
 (0)