Skip to content

Commit 6351bf2

Browse files
committed
Added Euler's totient Function
1 parent f869601 commit 6351bf2

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Others/EulersFunction.java

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//You can read more about Euler's totient function https://en.wikipedia.org/wiki/Euler%27s_totient_function
2+
public class EulersFunction {
3+
//This method returns us number of x that (x < n) and gcd(x, n) == 1 in O(sqrt(n)) time complexity;
4+
public static int getEuler(int n) {
5+
int result = n;
6+
for (int i = 2; i * i <= n; i++) {
7+
if(n % i == 0) {
8+
while (n % i == 0) n /= i;
9+
result -= result / i;
10+
}
11+
}
12+
if (n > 1) result -= result / n;
13+
return result;
14+
}
15+
public static void main(String[] args) {
16+
for(int i = 1; i < 100; i++) {
17+
System.out.println(getEuler(i));
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)