Euler's totient function for large numbers
I made this algorithm to compute Euler's totient function for large numbers. A sieve is used.
#include <iostream>
#include <cstdint>
#include <vector>
#include <cmath>
typedef uint64_t integer;
integer euler_totient(integer n) {
integer phi_n=n;
integer len_SIEVE=(integer) std::sqrt(n)+1;
std::vector<char> SIEVE(len_SIEVE, true);
for (integer p=2; p<len_SIEVE; p++)
if (SIEVE[p]==true)
if (n%p==0) {
for (integer m=p; m<len_SIEVE; m+=p)
SIEVE[m]=false;
phi_n/=p;
phi_n*=p-1;
}
return phi_n;
}
int main() {
std::cout << euler_totient(100000000) << std::endl;
return 0;
}
Is it a good solution?
Can it be improved in any way?
EDIT: I changed the algorithm because I realized that the second part was useless.
user140242
- 163
- 6
lang-cpp