Skip to content

Commit 9f28d59

Browse files
authored
Merge pull request div-bargali#245 from cp1307/patch-1
Create Euler's Totient Function
2 parents c9f4808 + 7e7d895 commit 9f28d59

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Euler's Totient Function
2+
#include <iostream>
3+
using namespace std;
4+
5+
// Function to return gcd of a and b
6+
int gcd(int a, int b)
7+
{
8+
if (a == 0)
9+
return b;
10+
return gcd(b % a, a);
11+
}
12+
13+
// A simple method to evaluate Euler Totient Function
14+
int phi(unsigned int n)
15+
{
16+
unsigned int result = 1;
17+
for (int i = 2; i < n; i++)
18+
if (gcd(i, n) == 1)
19+
result++;
20+
return result;
21+
}
22+
23+
// Driver program to test above function
24+
int main()
25+
{
26+
int n;
27+
for (n = 1; n <= 10; n++)
28+
cout << "phi("<<n<<") = " << phi(n) << endl;
29+
return 0;
30+
}
31+
32+

0 commit comments

Comments
 (0)