Edited the prime factorization function of 69 so it is quick now

This commit is contained in:
2021-11-14 22:20:56 +01:00
parent ec1fae253d
commit 673b908d16

View File

@@ -35,27 +35,31 @@ vector<int> sieve(int n){
return result; return result;
} }
vector<int> prime_terms(int n, vector<int> primes = vector<int>()){ vector<int> prime_terms(int n){
if(primes.empty()){
primes = sieve(n);
}
vector<int> result; vector<int> result;
for(int p : primes){ if(n % 2 == 0){
if(p > n){ result.push_back(2);
break;
while(n % 2 == 0){
n /= 2;
}
} }
if(n % p == 0){ for(int i = 3; i <= sqrt(n); i += 2){
result.push_back(p); if(n % i == 0){
result.push_back(i);
while(n % p == 0){ while(n % i == 0){
n /= p; n /= i;
} }
} }
} }
if(n > 2){
result.push_back(n);
}
return result; return result;
} }
@@ -76,7 +80,7 @@ int main(){
for(size_t i = 2; i < n_phi.size(); ++i){ for(size_t i = 2; i < n_phi.size(); ++i){
if(n_phi[i] == 0){ if(n_phi[i] == 0){
auto terms = prime_terms(i, primes); auto terms = prime_terms(i);
float acc = 1; float acc = 1;
for(int t : terms){ for(int t : terms){