-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPhi Function.cpp
More file actions
33 lines (32 loc) · 768 Bytes
/
Phi Function.cpp
File metadata and controls
33 lines (32 loc) · 768 Bytes
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
// Euler's totient function
//
// Time Complexity: O(sqrt(n))
int phi(int n){
int result = n;
for(int i = 2; i * i <= n; i++){
if(n % i == 0){
int count = 0;
while(n % i == 0){
n /= i;
}
result -= result / i;
}
}
if(n > 1) result -= result / n;
return result;
}
// Euler's totient function 1 to n in O(nlog(log(n)))
// use the same ideas as the Sieve of Eratosthenes
vector<int> phi_1_to_n(int n){
vector<int> vec(n + 1);
for(int i = 0; i <= n; i++)
vec[i] = i;
for(int i = 2; i <= n; i++){
if(vec[i] == i){
for(int j = i; j <= n; j += i){
vec[j] -= vec[j] / i;
}
}
}
return vec;
}