Totient Maximum
Euler's totient function, varphi(n), counts the number of integers k with 1 <= k <= n that are relatively prime to n. Find the value of n <= 1,000,000 for which n / varphi(n) is a maximum.
Problem Statement
This archive keeps the full statement, math, and original media on the page.
Euler's totient function, $\phi(n)$ [sometimes called the phi function], is defined as the number of positive integers not exceeding $n$ which are relatively prime to $n$. For example, as $1$, $2$, $4$, $5$, $7$, and $8$, are all less than or equal to nine and relatively prime to nine, $\phi(9)=6$.
| $n$ | Relative Prime | $\phi(n)$ | $n/\phi(n)$ |
| 2 | $1$ | $1$ | $2$ |
| 3 | $1, 2$ | $2$ | $1.5$ |
| 4 | $1, 3$ | $2$ | $2$ |
| 5 | $1,2,3,4$ | $4$ | $1.25$ |
| 6 | $1,5$ | $2$ | $3$ |
| 7 | $1,2,3,4,5,6$ | $6$ | $1.1666\ldots$ |
| 8 | $1,3,5,7$ | $4$ | $2$ |
| 9 | $1,2,4,5,7,8$ | $6$ | $1.5$ |
| 10 | $1,3,7,9$ | $4$ | $2.5$ |
It can be seen that $n = 6$ produces a maximum $n/\phi(n)$ for $n\leq 10$.
Find the value of $n\leq 1\,000\,000$ for which $n/\phi(n)$ is a maximum.
Problem 69: Totient Maximum
Mathematical Analysis
Theorem 1 (Ratio Formula)
For , the ratio depends only on the set of distinct prime divisors of :
Proof. By Euler’s product formula, . Dividing by :
The exponents do not appear in this expression, so the ratio depends only on the set .
Theorem 2 (Monotonicity in Prime Factors)
Let be a set of distinct primes. Then:
- Adding any prime strictly increases by the factor .
- The factor is a strictly decreasing function of , so smaller primes contribute larger multiplicative gains.
Proof. (1) is immediate since for all primes . For (2), observe that , which is strictly decreasing in .
Theorem 3 (Primorial Optimality)
Among all positive integers , the ratio is maximized when is the largest primorial not exceeding . That is, where are the primes in increasing order and is the largest index such that .
Proof. We establish optimality by a dominance argument.
Claim 1: The optimizer is squarefree. Suppose is not squarefree, so for some prime with . Then has and (by Theorem 1). Since , we can “free” a factor of in our budget without changing the ratio. Using this budget to include a new prime not dividing (if one exists with ) strictly increases the ratio by Theorem 2(1). If no such exists, achieves the same ratio with a smaller value, but adding back factors cannot help. In either case, we can assume is squarefree.
Claim 2: The optimizer uses consecutive smallest primes. Let be squarefree with . If some is not the -th smallest prime (i.e., ), we can replace by (a smaller prime). This does not increase (since ), and by Theorem 2(2), the factor strictly increases the ratio. Repeating this argument yields .
Claim 3: is maximized subject to . By Theorem 2(1), adding an additional prime strictly increases the ratio, so we include as many consecutive primes as the bound allows.
Computation
| Primes included | ||
|---|---|---|
| 2 | 2.000 | |
| 6 | 3.000 | |
| 30 | 3.750 | |
| 210 | 4.375 | |
| 2310 | 4.813 | |
| 30030 | 4.991 | |
| 510510 | 5.214 | |
| 9699690 |
Since , the answer is .
Editorial
The maximizing value is the largest primorial not exceeding the bound. We therefore multiply consecutive primes in increasing order, beginning with , and stop as soon as the next prime would push the product past . The product accumulated just before that overshoot is the desired value of .
Pseudocode
Initialize the running product to 1.
Traverse the primes in increasing order:
if multiplying by the next prime would exceed 1,000,000, stop the process
otherwise incorporate that prime into the running product
Return the final product.
Complexity
- Time: where denotes the primorial index — effectively multiplications.
- Space: .
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
int main() {
// By Primorial Optimality (Theorem 3), the answer is the largest
// primorial not exceeding 1,000,000.
// 2 * 3 * 5 * 7 * 11 * 13 * 17 = 510510 <= 1000000
// 2 * 3 * 5 * 7 * 11 * 13 * 17 * 19 = 9699690 > 1000000
const int LIMIT = 1000000;
int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};
long long n = 1;
for (int p : primes) {
if (n * p > LIMIT) break;
n *= p;
}
cout << n << endl;
return 0;
}
"""
Project Euler Problem 69: Totient Maximum
Find the value of n <= 1,000,000 for which n/phi(n) is a maximum.
By Theorem 3 (Primorial Optimality), n is the largest primorial <= 1,000,000.
n/phi(n) = prod p/(p-1) over distinct prime factors, maximized by including
the most consecutive smallest primes whose product does not exceed the limit.
"""
def main():
LIMIT = 1_000_000
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
n = 1
for p in primes:
if n * p > LIMIT:
break
n *= p
print(n)
if __name__ == "__main__":
main()