-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEuler's Totient Phi.cpp
More file actions
52 lines (48 loc) · 936 Bytes
/
Copy pathEuler's Totient Phi.cpp
File metadata and controls
52 lines (48 loc) · 936 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
#define N 1000
int divisors[N];
int primeFactors(int x) {
int i, flag, count;
flag = count = 0;
while(x%2==0) {
flag = 1;
x/=2;
}
if(flag==1) {
divisors[count++] = 2;
}
flag = 0;
for(i=3; i*i<=x; i+=2) {
flag = 0;
while(x%i==0) {
x/=i;
flag = 1;
}
if(flag==1) {
divisors[count++] = i;
}
}
if(x>2) {
divisors[count++] = x;
}
return count;
}
int euler_totient_phi(int n) {
int i, l;
float x = n;
l = primeFactors(n);
for(i=0; i<l; i++) {
x*=1-(1.0/divisors[i]);
}
return (int)x;
}
int main()
{
int x;
while(scanf("%d", &x)==1) {
cout << "Euler's Totient Phi: " << euler_totient_phi(x) << endl;
}
}