From 19fc56c73d6c09656eff32bcc0ec62654bdc4735 Mon Sep 17 00:00:00 2001 From: OREO1210 Date: Fri, 22 Oct 2021 03:58:16 +0530 Subject: [PATCH] added euler totient function --- C/Euler_totient_Function.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 C/Euler_totient_Function.c diff --git a/C/Euler_totient_Function.c b/C/Euler_totient_Function.c new file mode 100644 index 0000000..b6193ab --- /dev/null +++ b/C/Euler_totient_Function.c @@ -0,0 +1,29 @@ +//C program to calculate Euler's Totient Function +#include + +// Function to return gcd of a and b +int gcd(int a, int b) +{ + if (a == 0) + return b; + return gcd(b % a, a); +} + +// A simple method to evaluate Euler Totient Function +int phi(unsigned int n) +{ + unsigned int result = 1; + for (int i = 2; i < n; i++) + if (gcd(i, n) == 1) + result++; + return result; +} + +// Driver program to test above function +int main() +{ + int n; + for (n = 1; n <= 10; n++) + printf("phi(%d) = %d\n", n, phi(n)); + return 0; +} \ No newline at end of file