Skip to content

Commit 305e48d

Browse files
authored
Added Java implementation
1 parent 052ae3a commit 305e48d

File tree

1 file changed

+34
-0
lines changed
  • Math/eulers_totient_function/Java

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// A simple java program to calculate
2+
// Euler's Totient Function
3+
import java.io.*;
4+
5+
class GFG {
6+
7+
// Function to return GCD of a and b
8+
static int gcd(int a, int b)
9+
{
10+
if (a == 0)
11+
return b;
12+
return gcd(b % a, a);
13+
}
14+
15+
// A simple method to evaluate
16+
// Euler Totient Function
17+
static int phi(int n)
18+
{
19+
int result = 1;
20+
for (int i = 2; i < n; i++)
21+
if (gcd(i, n) == 1)
22+
result++;
23+
return result;
24+
}
25+
26+
// Driver code
27+
public static void main(String[] args)
28+
{
29+
int n;
30+
31+
for (n = 1; n <= 10; n++)
32+
System.out.println("phi(" + n + ") = " + phi(n));
33+
}
34+
}

0 commit comments

Comments
 (0)