-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler's totient function.cpp
81 lines (59 loc) · 1.34 KB
/
euler's totient function.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
EULERS TOTIENT FUNCTION
#include<bits/stdc++.h>
using namespace std;
int n=1000001;
vector<long long> arr(n);
void euler(){
for(int i=0;i<=n;i++){
arr[i]=i;
}
for(int i=2;i<=n;i++){
if(arr[i]==i){
for(int j=2*i;j<=n;j+=i){
arr[j]*=(i-1);
arr[j]/=i;
}
arr[i]=i-1;
}
}
}
int main(){
euler();
int t;
cin>>t;
while(t--){
int x;
cin>>x;
cout<<arr[x]<<endl;
}
return 0;
}
//for single digit
#include<bits/stdc++.h>
using namespace std;
int euler(int n){
int result=n;
//let there be n numbers of coprimes from 1 top n
for(int i=2;i*i<=n;i++){
if(result%i==0){
while(result%i==0){
result/=i; //remove that prime (n is no longer divisible by any multiple of the prime p)
}
n-=(n/i); //subtracing the number of numbers which are multiples of the prime i
}
}
if(result>1){ // there for result is a prime itself consider 34 (17 * 2) we will devide it by all primes from 1 to 5(if its divisible) (i*i <=n ) , still result will be 17.
n-=(n/result);
}
return n;
}
int main(){
int t;
cin>>t;
while(t--){
int x;
cin>>x;
cout<<euler(x)<<endl;
}
return 0;
}