-
Notifications
You must be signed in to change notification settings - Fork 0
/
TotientMaximum.java
40 lines (32 loc) · 924 Bytes
/
TotientMaximum.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package dev;
public class TotientMaximum {
public static void main(String[] args) {
int limit = 1000000;
double maxRatio = 0;
int result = 0;
for (int i = 2; i <= limit; i++) {
double ratio = (double) i / phi(i);
if (ratio > maxRatio) {
maxRatio = ratio;
result = i;
}
}
System.out.println("The value of n ≤ 1,000,000 for which n/φ(n) is a maximum is: " + result);
}
// Calculates Euler's totient function φ(n)
public static int phi(int n) {
int result = n;
for (int p = 2; p * p <= n; p++) {
if (n % p == 0) {
result -= result / p;
while (n % p == 0) {
n /= p;
}
}
}
if (n > 1) {
result -= result / n;
}
return result;
}
}