Skip to content

Commit 17ba354

Browse files
euler-totient function in c
1 parent 9a1c665 commit 17ba354

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

euler-totient.c

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// A simple C program to calculate Euler's Totient Function
2+
#include <stdio.h>
3+
4+
// Function to return gcd of a and b
5+
int gcd(int a, int b)
6+
{
7+
if (a == 0)
8+
return b;
9+
return gcd(b % a, a);
10+
}
11+
12+
// A simple method to evaluate Euler Totient Function
13+
int phi(unsigned int n)
14+
{
15+
unsigned int result = 1;
16+
for (int i = 2; i < n; i++)
17+
if (gcd(i, n) == 1)
18+
result++;
19+
return result;
20+
}
21+
22+
// Driver program to test above function
23+
int main()
24+
{
25+
int n;
26+
for (n = 1; n <= 10; n++)
27+
printf("phi(%d) = %d\n", n, phi(n));
28+
return 0;
29+
}

0 commit comments

Comments
 (0)