|
| 1 | +/** |
| 2 | + * Computes Euler's totient function phi(n), which counts the number of integers in [1, n] that are |
| 3 | + * relatively prime to n. |
| 4 | + * |
| 5 | + * <p>Uses trial division to find prime factors and applies the product formula: |
| 6 | + * phi(n) = n * product of (1 - 1/p) for each distinct prime factor p of n. |
| 7 | + * |
| 8 | + * <p>Time: O(sqrt(n)) |
| 9 | + * |
| 10 | + * @author William Fiset, william.alexandre.fiset@gmail.com |
| 11 | + */ |
1 | 12 | package com.williamfiset.algorithms.math; |
2 | 13 |
|
3 | | -import java.util.*; |
4 | | - |
5 | 14 | public class EulerTotientFunction { |
6 | 15 |
|
| 16 | + /** |
| 17 | + * Computes Euler's totient phi(n). |
| 18 | + * |
| 19 | + * @param n a positive integer. |
| 20 | + * @return the number of integers in [1, n] that are coprime to n. |
| 21 | + * @throws IllegalArgumentException if n is not positive. |
| 22 | + */ |
7 | 23 | public static long eulersTotient(long n) { |
8 | | - for (long p : new HashSet<Long>(primeFactorization(n))) n -= (n / p); |
9 | | - return n; |
10 | | - } |
11 | | - |
12 | | - private static ArrayList<Long> primeFactorization(long n) { |
13 | | - ArrayList<Long> factors = new ArrayList<Long>(); |
14 | | - if (n <= 0) throw new IllegalArgumentException(); |
15 | | - else if (n == 1) return factors; |
16 | | - PriorityQueue<Long> divisorQueue = new PriorityQueue<Long>(); |
17 | | - divisorQueue.add(n); |
18 | | - while (!divisorQueue.isEmpty()) { |
19 | | - long divisor = divisorQueue.remove(); |
20 | | - if (isPrime(divisor)) { |
21 | | - factors.add(divisor); |
22 | | - continue; |
23 | | - } |
24 | | - long next_divisor = pollardRho(divisor); |
25 | | - if (next_divisor == divisor) { |
26 | | - divisorQueue.add(divisor); |
27 | | - } else { |
28 | | - divisorQueue.add(next_divisor); |
29 | | - divisorQueue.add(divisor / next_divisor); |
| 24 | + if (n <= 0) |
| 25 | + throw new IllegalArgumentException("n must be positive."); |
| 26 | + long result = n; |
| 27 | + for (long p = 2; p * p <= n; p++) { |
| 28 | + if (n % p == 0) { |
| 29 | + while (n % p == 0) |
| 30 | + n /= p; |
| 31 | + result -= result / p; |
30 | 32 | } |
31 | 33 | } |
32 | | - return factors; |
33 | | - } |
34 | | - |
35 | | - private static long pollardRho(long n) { |
36 | | - if (n % 2 == 0) return 2; |
37 | | - // Get a number in the range [2, 10^6] |
38 | | - long x = 2 + (long) (999999 * Math.random()); |
39 | | - long c = 2 + (long) (999999 * Math.random()); |
40 | | - long y = x; |
41 | | - long d = 1; |
42 | | - while (d == 1) { |
43 | | - x = (x * x + c) % n; |
44 | | - y = (y * y + c) % n; |
45 | | - y = (y * y + c) % n; |
46 | | - d = gcf(Math.abs(x - y), n); |
47 | | - if (d == n) break; |
48 | | - } |
49 | | - return d; |
50 | | - } |
51 | | - |
52 | | - private static long gcf(long a, long b) { |
53 | | - return b == 0 ? a : gcf(b, a % b); |
54 | | - } |
55 | | - |
56 | | - private static boolean isPrime(long n) { |
57 | | - |
58 | | - if (n < 2) return false; |
59 | | - if (n == 2 || n == 3) return true; |
60 | | - if (n % 2 == 0 || n % 3 == 0) return false; |
61 | | - |
62 | | - int limit = (int) Math.sqrt(n); |
63 | | - |
64 | | - for (int i = 5; i <= limit; i += 6) if (n % i == 0 || n % (i + 2) == 0) return false; |
65 | | - |
66 | | - return true; |
| 34 | + // If n still has a prime factor greater than sqrt(original n). |
| 35 | + if (n > 1) |
| 36 | + result -= result / n; |
| 37 | + return result; |
67 | 38 | } |
68 | 39 |
|
69 | 40 | public static void main(String[] args) { |
70 | | - |
71 | | - // Prints 8 because 1,2,4,7,8,11,13,14 are all |
72 | | - // less than 15 and relatively prime with 15 |
| 41 | + // phi(15) = 8 because 1,2,4,7,8,11,13,14 are coprime with 15. |
73 | 42 | System.out.printf("phi(15) = %d\n", eulersTotient(15)); |
74 | 43 |
|
75 | 44 | System.out.println(); |
76 | 45 |
|
77 | | - for (int x = 1; x <= 11; x++) { |
| 46 | + for (int x = 1; x <= 11; x++) |
78 | 47 | System.out.printf("phi(%d) = %d\n", x, eulersTotient(x)); |
79 | | - } |
80 | 48 | } |
81 | 49 | } |
0 commit comments