-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEuler's Totient Function - phi.cpp
66 lines (54 loc) · 1.31 KB
/
Euler's Totient Function - phi.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
#include <bits/stdc++.h>
#include <ext/rope>
using namespace std;
using namespace __gnu_cxx;
using ci = const int;
using ld = long double;
using llint = long long;
using ullint = unsigned long long;
using pii = pair <int,int>;
using pcc = pair <char,char>;
using pss = pair <string,string>;
using vi = vector <int>;
using vb = vector <bool>;
using vii = vi::iterator;
#define INF (1<<30)
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define all(c) c.begin(), c.end()
#define ms(name,val) memset(name, val, sizeof name)
#define np nullptr
// returns count of numbers in
// range [1, n] which are relatively
// prime to n -> gcd(t1, n) = 1
int phi(int n)
{
int res = n;
// consider all prime factors of n and
// subtract their multiples from result
// e.g. n = 10
// 1 2 3 4 5 6 7 8 9 10
// we will check 2, and since 10 can be
// divided by 2, we subtract 2, 4, 6, 8
// and 10 from the result, that is res/n
for (int p = 2; p*p <= n; ++p)
{
if (n % p == 0)
{
res -= (res / p);
while (n % p == 0) n /= p;
}
}
if (n > 1)
res -= (res / n);
return res;
}
int main()
{
ios_base::sync_with_stdio(0);
//cin.tie(0);
for (int t1 = 1; t1 <= 10; ++t1)
cout << phi(t1) << '\n';
return 0;
}