-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFactorize_by_sieve.cpp
53 lines (45 loc) · 1.01 KB
/
Factorize_by_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
51
52
53
#define f(i, n) for(int i = 0; i < (n); ++i)
#define fa(i, a, n) for(int i = (a); i <= (n); ++i)
#define pb push_back
typedef long long ll;
typedef vector<int> vi;
vector <int> factorize(ll n){
//O(sqrt(n))
vi ans;
for(ll i = 2; i * i <= n; i++){
while(n % i == 0){
ans.pb(i);
n /= i;
}
}
if(n != 1) ans.pb(n;)
return ans;
}
vector <int> factorizeFast(ll n){
//O(log(n))
//Storage O(n) - problematic in 1e9 cases
vi ans;
int minprime[n + 1];
f(i, n + 1){
minprime[i] = 0;
}
for(int i = 2; i * i <= n; i++){
if(minprime[i] == 0){ //if i is prime
for(int j = i * i; j <= n; j += i){
if(minprime[j] == 0){
minprime[j] = i;
}
}
}
}
fa(i, 2, n){
if(minprime[i] == 0){
minprime[i] = i;
}
}
while(n != 1){
ans.pb(minprime[n]);
n /= minprime[n];
}
return ans;
}