-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathphiFunction2.cpp
More file actions
60 lines (49 loc) · 1.01 KB
/
phiFunction2.cpp
File metadata and controls
60 lines (49 loc) · 1.01 KB
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
#include<bits/stdc++.h>
using namespace std;
bool isprime(int n)
{
if(n==1) return false;
else if (n==2) return true;
else if(n>2 && n%2==0) return false;
else
{
for(int i=3;i<=sqrt(n);i+=2)
{
if(n%i==0) return false;
}
}
return true;
}
int phi(const int n)
{
// Base case
if ( n < 2 )
return 0;
// Lehmer's conjecture
if ( isprime(n) )
return n-1;
// Even number?
if ( n & 1 == 0 ) {
int m = n >> 1;
return !(m & 1) ? phi(m)<<1 : phi(m);
}
// For all primes ...
for ( std::vector<int>::iterator p = primes.begin();
p != primes.end() && *p <= n;
++p )
{
int m = *p;
if ( n % m ) continue;
// phi is multiplicative
int o = n/m;
int d = binary_gcd(m, o);
return d==1? phi(m)*phi(o) : phi(m)*phi(o)*d/phi(d);
}
}
int main()
{
int num;
scanf("%d",&num);
printf("Phi(%d) = %d\n",num,phi(num));
return 0;
}