-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSeiveVariations.cpp
41 lines (38 loc) · 926 Bytes
/
SeiveVariations.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
const int N = 1e6 + 7 ;
vector<int> isPrime(N,true);
void seive(){
isPrime[0] = isPrime[1] = false ;
for(int i = 2 ; i < N ; ++i){
if(isPrime[i]){
for(int j = i*i ; j < N ; j+=i){
isPrime[j] = false ;
}
}
}
}
vector<int> lowestPrime(N,N) ;
void seiveLPF(){
isPrime[0] = isPrime[1] = false ;
int n = isPrime.size();
for(int i=0 ; i<n ; ++i){
lowestPrime[i] = i ;
}
for(int i = 2 ; i < N ; ++i){
if(isPrime[i]){
for(int j = i*i ; j < N ; j+=i){
isPrime[j] = false ;
if(lowestPrime[j] == j){
lowestPrime[j] = i ;
}
}
}
}
}
vector<int> PrimeFactors(int num){
vector<int> primes ;
while(num > 1){
primes.pb(lowestPrime[num]);
num = num/lowestPrime[num];
}
return primes ;
}