All Euler problems
Project Euler

Totient Permutation

Find the value of n, 1 < n < 10^7, for which varphi(n) is a permutation of the digits of n and the ratio n/varphi(n) is minimized.

Source sync Apr 19, 2026
Problem #0070
Level Level 03
Solved By 25,532
Languages C++, Python
Answer 8319823
Length 457 words
number_theorycombinatoricsoptimization

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 used to determine the number of positive numbers less than or equal to \(n\) which are relatively prime to \(n\). For example, as \(1, 2, 4, 5, 7\), and \(8\), are all less than nine and relatively prime to nine, \(\phi (9)=6\).

The number \(1\) is considered to be relatively prime to every positive number, so \(\phi (1)=1\).

Interestingly, \(\phi (87109)=79180\), and it can be seen that \(87109\) is a permutation of \(79180\).

Find the value of \(n\), \(1 < n < 10^7\), for which \(\phi (n)\) is a permutation of \(n\) and the ratio \(n/\phi (n)\) produces a minimum.

Problem 70: Totient Permutation

Mathematical Analysis

Theorem 1 (Ratio Formula)

For n=p1a1pkakn = p_1^{a_1} \cdots p_k^{a_k}:

nφ(n)=i=1kpipi1.\frac{n}{\varphi(n)} = \prod_{i=1}^{k} \frac{p_i}{p_i - 1}.

Proof. Follows directly from φ(n)=npn(11/p)\varphi(n) = n \prod_{p \mid n}(1 - 1/p). \square

Theorem 2 (Minimization Principles)

To minimize n/φ(n)n/\varphi(n) subject to the permutation constraint:

  1. Use as few distinct prime factors as possible (each factor p/(p1)>1p/(p-1) > 1 increases the ratio).
  2. Use primes as large as possible (p/(p1)1p/(p-1) \to 1 as pp \to \infty).

Proof. Statement (1): If nn has kk distinct prime factors, then n/φ(n)=i=1kpi/(pi1)(2/1)kn/\varphi(n) = \prod_{i=1}^k p_i/(p_i - 1) \geq (2/1)^k in the worst case, and is always >1> 1 for k1k \geq 1. Fewer factors yield a smaller product.

Statement (2): Among ratios with the same number of prime factors, pi/(pi1)\prod p_i/(p_i-1) is minimized by choosing the largest possible primes (since p/(p1)p/(p-1) is strictly decreasing). \square

Theorem 3 (Exclusion of Primes)

If n=pn = p is prime, then φ(p)=p1\varphi(p) = p - 1 is never a digit permutation of pp.

Proof. Two integers are digit permutations of each other only if they are congruent modulo 9 (since the digit sum is invariant under permutation, and a number is congruent to its digit sum mod 9). However, p(p1)=1p - (p-1) = 1, so pp1+1(mod9)p \equiv p - 1 + 1 \pmod{9}, meaning p≢p1(mod9)p \not\equiv p - 1 \pmod{9} (as 1≢0(mod9)1 \not\equiv 0 \pmod{9}). Therefore pp and p1p - 1 cannot be digit permutations. \square

Corollary (Semiprimes Are Optimal Candidates)

Since primes are excluded by Theorem 3, the candidates with the fewest prime factors are semiprimes n=pqn = pq (with p,qp, q distinct primes or n=p2n = p^2). For semiprimes:

φ(pq)=(p1)(q1),pqφ(pq)=pq(p1)(q1).\varphi(pq) = (p-1)(q-1), \quad \frac{pq}{\varphi(pq)} = \frac{pq}{(p-1)(q-1)}.

This ratio is minimized when pp and qq are large and close to 1073162\sqrt{10^7} \approx 3162.

Proof. For fixed product pqpq, the ratio pq/((p1)(q1))pq/((p-1)(q-1)) is minimized when pp and qq are as close as possible (by AM-GM, the denominator (p1)(q1)(p-1)(q-1) is maximized for pqp \approx q). Furthermore, larger primes yield a ratio closer to 1. \square

Remark on n=p2n = p^2

For n=p2n = p^2, we have φ(p2)=p(p1)\varphi(p^2) = p(p-1) and n/φ(n)=p/(p1)n/\varphi(n) = p/(p-1), which has only one prime factor contributing. However, the permutation constraint p2p(p1)p^2 \leftrightarrow p(p-1) is hard to satisfy for large pp, and in practice the semiprime form n=pqn = pq with pqp \neq q yields the optimum.

Search Strategy

Proposition. It suffices to search over pairs (p,q)(p, q) of primes with pqp \leq q and pq<107pq < 10^7. For each pair, check whether (p1)(q1)(p-1)(q-1) is a digit permutation of pqpq, and track the pair minimizing pq/((p1)(q1))pq/((p-1)(q-1)).

Proof of sufficiency. By Theorem 3, nn cannot be prime. If nn has three or more distinct prime factors, then n/φ(n)(2/1)(3/2)(5/4)=15/4=3.75n/\varphi(n) \geq (2/1)(3/2)(5/4) = 15/4 = 3.75, whereas the semiprime candidate yields a ratio near 1. Products of prime powers with three or more factors are similarly dominated. Hence we need only search semiprimes. \square

Digit Permutation Criterion

Lemma. Two positive integers aa and bb are digit permutations of each other if and only if they have the same multiset of decimal digits, equivalently, the same sorted digit string.

Proof. By definition. \square

Editorial

The search is restricted to semiprimes n=pqn = pq, because the ratio n/φ(n)n/\varphi(n) is minimized there among non-prime candidates, especially when pp and qq are large and close to 107\sqrt{10^7}. We enumerate prime pairs (p,q)(p,q) with pqp \le q and pq<107pq < 10^7, compute φ(pq)=(p1)(q1)\varphi(pq) = (p-1)(q-1), and test whether φ(pq)\varphi(pq) is a digit permutation of pqpq. Among all pairs passing that test, we keep the one with the smallest ratio n/φ(n)n/\varphi(n).

Pseudocode

Generate the primes below the search bound.
Begin with no candidate and with the best ratio larger than every feasible value.

For each prime p whose square is still below 10^7:
    pair p with primes q starting from p itself
    stop the inner scan once pq reaches the bound

    for each admissible product n = pq:
        compute phi(n) = (p - 1)(q - 1)
        compare the decimal digit multisets of n and phi(n)

        if they are permutations of one another and the ratio n / phi(n) improves the current best:
            record this n as the new best candidate

Return the recorded best candidate.

Complexity

  • Sieve: O(NloglogN)O(N \log \log N) where N=107N = 10^7.
  • Search: O(π(N)2)O(\pi(\sqrt{N})^2) pairs. By the Prime Number Theorem, π(N)N/lnN\pi(\sqrt{N}) \approx \sqrt{N}/\ln\sqrt{N}, so this is O(N/log2N)O(N/\log^2 N).
  • Space: O(N)O(N) for the prime sieve.

Answer

8319823\boxed{8319823}

Code

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

C++ project_euler/problem_070/solution.cpp
#include <bits/stdc++.h>
using namespace std;

bool is_permutation(int a, int b) {
    int digits_a[10] = {}, digits_b[10] = {};
    while (a > 0) { digits_a[a % 10]++; a /= 10; }
    while (b > 0) { digits_b[b % 10]++; b /= 10; }
    for (int i = 0; i < 10; i++)
        if (digits_a[i] != digits_b[i]) return false;
    return true;
}

int main() {
    const int LIMIT = 10000000;

    // Sieve of Eratosthenes
    vector<bool> is_prime(LIMIT, true);
    is_prime[0] = is_prime[1] = false;
    for (int i = 2; (long long)i * i < LIMIT; i++) {
        if (is_prime[i]) {
            for (int j = i * i; j < LIMIT; j += i)
                is_prime[j] = false;
        }
    }

    vector<int> primes;
    for (int i = 2; i < LIMIT; i++) {
        if (is_prime[i]) primes.push_back(i);
    }

    double best_ratio = 1e18;
    int best_n = 0;

    // Search semiprimes n = p * q with p <= q, pq < LIMIT
    for (int i = 0; i < (int)primes.size(); i++) {
        int p = primes[i];
        if ((long long)p * p >= LIMIT) break;
        for (int j = i; j < (int)primes.size(); j++) {
            int q = primes[j];
            long long n = (long long)p * q;
            if (n >= LIMIT) break;

            long long phi = (long long)(p - 1) * (q - 1);
            double ratio = (double)n / phi;

            if (ratio < best_ratio && is_permutation((int)n, (int)phi)) {
                best_ratio = ratio;
                best_n = (int)n;
            }
        }
    }

    cout << best_n << endl;

    return 0;
}