-
Notifications
You must be signed in to change notification settings - Fork 1
/
Code.cpp
37 lines (33 loc) · 1.04 KB
/
Code.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
VL sieve(ll n) {
ll check[n+5];
VL primes ;
memset(check,0,sizeof(check));
check[1] = 1 ;
for ( ll i = 4 ; i <= n ; i = i+2) check[i] = 1 ;
for ( ll i = 3 ; i*i <= n ; i += 2) {
if (!check[i]) {
for ( ll j = i*i ; j <= n ; j += 2*i){
check[j] = 1 ;
}
}
}
for ( ll i = 1 ; i <= n ; i++) if (!check[i]) primes.PB(i);
return primes ;
}
VL primefactorize(ll n) {
ll primerange = (ll)sqrt(n)+1 ;
VL primes = sieve(primerange);
VL primefactors ;
if ( n == 1) {
primefactors.PB(1);
return primefactors;
}
for ( ll i = 0 ; primes[i] <= sqrt(n) && i < primes.size() ; i++) {
while (!(n%primes[i])) {
n = n/primes[i];
primefactors.PB(primes[i]);
}
}
if ( n > 1) primefactors.PB(n);
return primefactors ;
}