All Euler problems
Project Euler

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.

Source sync Apr 19, 2026
Problem #0069
Level Level 02
Solved By 39,387
Languages C++, Python
Answer 510510
Length 446 words
number_theoryoptimizationbrute_force

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 n=p1a1p2a2pkakn = p_1^{a_1} p_2^{a_2} \cdots p_k^{a_k}, the ratio n/φ(n)n/\varphi(n) depends only on the set of distinct prime divisors of nn:

nφ(n)=pnpp1.\frac{n}{\varphi(n)} = \prod_{p \mid n} \frac{p}{p-1}.

Proof. By Euler’s product formula, φ(n)=npn(11/p)\varphi(n) = n \prod_{p \mid n}(1 - 1/p). Dividing nn by φ(n)\varphi(n):

nφ(n)=1pn(11/p)=pnpp1.\frac{n}{\varphi(n)} = \frac{1}{\prod_{p \mid n}(1 - 1/p)} = \prod_{p \mid n} \frac{p}{p-1}.

The exponents aia_i do not appear in this expression, so the ratio depends only on the set {p1,,pk}\{p_1, \ldots, p_k\}. \square

Theorem 2 (Monotonicity in Prime Factors)

Let P={p1,,pk}P = \{p_1, \ldots, p_k\} be a set of distinct primes. Then:

  1. Adding any prime qPq \notin P strictly increases pPpp1\prod_{p \in P} \frac{p}{p-1} by the factor qq1>1\frac{q}{q-1} > 1.
  2. The factor pp1\frac{p}{p-1} is a strictly decreasing function of pp, so smaller primes contribute larger multiplicative gains.

Proof. (1) is immediate since q/(q1)>1q/(q-1) > 1 for all primes q2q \geq 2. For (2), observe that p/(p1)=1+1/(p1)p/(p-1) = 1 + 1/(p-1), which is strictly decreasing in pp. \square

Theorem 3 (Primorial Optimality)

Among all positive integers nNn \leq N, the ratio n/φ(n)n/\varphi(n) is maximized when nn is the largest primorial not exceeding NN. That is, n=p1p2pkn^* = p_1 p_2 \cdots p_k where p1<p2<p_1 < p_2 < \cdots are the primes in increasing order and kk is the largest index such that p1pkNp_1 \cdots p_k \leq N.

Proof. We establish optimality by a dominance argument.

Claim 1: The optimizer nn^* is squarefree. Suppose nn is not squarefree, so n=p2mn = p^2 m for some prime pp with pmp \nmid m. Then n=pmn' = pm has n<nn' < n and n/φ(n)=n/φ(n)n'/\varphi(n') = n/\varphi(n) (by Theorem 1). Since n<nNn' < n \leq N, we can “free” a factor of pp in our budget without changing the ratio. Using this budget to include a new prime qq not dividing nn' (if one exists with nqNn'q \leq N) strictly increases the ratio by Theorem 2(1). If no such qq exists, nn' achieves the same ratio with a smaller value, but adding back factors cannot help. In either case, we can assume nn^* is squarefree.

Claim 2: The optimizer uses consecutive smallest primes. Let n=pj1pjkn = p_{j_1} \cdots p_{j_k} be squarefree with j1<<jkj_1 < \cdots < j_k. If some pjip_{j_i} is not the ii-th smallest prime (i.e., ji>ij_i > i), we can replace pjip_{j_i} by pip_i (a smaller prime). This does not increase nn (since pi<pjip_i < p_{j_i}), and by Theorem 2(2), the factor pi/(pi1)>pji/(pji1)p_i/(p_i - 1) > p_{j_i}/(p_{j_i} - 1) strictly increases the ratio. Repeating this argument yields n=p1p2pkn^* = p_1 p_2 \cdots p_k.

Claim 3: kk is maximized subject to p1pkNp_1 \cdots p_k \leq N. By Theorem 2(1), adding an additional prime pk+1p_{k+1} strictly increases the ratio, so we include as many consecutive primes as the bound NN allows. \square

Computation

Primes includedn=pin = \prod p_in/φ(n)n/\varphi(n)
{2}\{2\}22.000
{2,3}\{2,3\}63.000
{2,3,5}\{2,3,5\}303.750
{2,3,5,7}\{2,3,5,7\}2104.375
{2,3,5,7,11}\{2,3,5,7,11\}23104.813
{2,3,5,7,11,13}\{2,3,5,7,11,13\}300304.991
{2,3,5,7,11,13,17}\{2,3,5,7,11,13,17\}5105105.214
{2,3,5,7,11,13,17,19}\{2,3,5,7,11,13,17,19\}9699690>106> 10^6

Since 5105101,000,000<9,699,690510510 \le 1{,}000{,}000 < 9{,}699{,}690, the answer is n=2×3×5×7×11×13×17=510510n^* = 2 \times 3 \times 5 \times 7 \times 11 \times 13 \times 17 = 510510.

Editorial

The maximizing value is the largest primorial not exceeding the bound. We therefore multiply consecutive primes in increasing order, beginning with 22, and stop as soon as the next prime would push the product past 1,000,0001{,}000{,}000. The product accumulated just before that overshoot is the desired value of nn.

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: O(π1(N))O(\pi^{-1}(N)) where π1\pi^{-1} denotes the primorial index — effectively O(loglogN)O(\log \log N) multiplications.
  • Space: O(1)O(1).

Answer

510510\boxed{510510}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_069/solution.cpp
#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;
}