Skip to content

Commit 44740a6

Browse files
committed
add eular totient function
1 parent 35fccc1 commit 44740a6

File tree

2 files changed

+28
-498
lines changed

2 files changed

+28
-498
lines changed

Algorithms/EularTotient.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package Algorithms;
2+
3+
class EularTotient {
4+
/*
5+
* compute the eular totient(https://en.wikipedia.org/wiki/Euler%27s_totient_function)
6+
* of the given numer, speeding up the computation when we have prime numbers
7+
*
8+
* time complexity: O(k log n) where k = no of prime factors
9+
* this method is advantagious when we have large numbers whose prime factors
10+
* are small numbers eg. eular totient of 16'402'500'000 = 2^5 * 3^8 * 5^7 can be computed
11+
* in only 3 steps.
12+
*/
13+
private long totient(long n , long primes[]){
14+
long result = n;
15+
for(int i=0; primes[i] <= n; i++)
16+
{
17+
if(n < primes[i])
18+
break;
19+
if(n % primes[i] == 0)
20+
result -= result / primes[i];
21+
while (n % primes[i] == 0)
22+
n /= primes[i];
23+
}
24+
if(n > 1)
25+
result -= result / n;
26+
return result;
27+
}
28+
}

0 commit comments

Comments
 (0)