All Euler problems
Project Euler

Counting Fractions

Consider the fraction n/d, where n and d are positive integers. If n < d and gcd(n, d) = 1, it is called a reduced proper fraction. How many elements are in the set of reduced proper fractions for...

Source sync Apr 19, 2026
Problem #0072
Level Level 03
Solved By 25,546
Languages C++, Python
Answer 303963552391
Length 404 words
number_theoryanalytic_mathbrute_force

Problem Statement

This archive keeps the full statement, math, and original media on the page.

Consider the fraction, \(\dfrac n d\), where \(n\) and \(d\) are positive integers. If \(n < d\) and \(\operatorname {HCF}(n,d)=1\), it is called a reduced proper fraction.

If we list the set of reduced proper fractions for \(d \le 8\) in ascending order of size, we get: \[\frac 1 8, \frac 1 7, \frac 1 6, \frac 1 5, \frac 1 4, \frac 2 7, \frac 1 3, \frac 3 8, \frac 2 5, \frac 3 7, \frac 1 2, \frac 4 7, \frac 3 5, \frac 5 8, \frac 2 3, \frac 5 7, \frac 3 4, \frac 4 5, \frac 5 6, \frac 6 7, \frac 7 8\] It can be seen that there are \(21\) elements in this set.

How many elements would be contained in the set of reduced proper fractions for \(d \le 1\,000\,000\)

Problem 72: Counting Fractions

Mathematical Analysis

Theorem 1 (Fractions Per Denominator)

The number of reduced proper fractions with denominator exactly dd is φ(d)\varphi(d).

Proof. A fraction n/dn/d with 1n<d1 \leq n < d is in lowest terms if and only if gcd(n,d)=1\gcd(n, d) = 1. By definition, φ(d)={n:1nd, gcd(n,d)=1}\varphi(d) = |\{n : 1 \leq n \leq d,\ \gcd(n,d) = 1\}|. For d2d \geq 2, since gcd(d,d)=d>1\gcd(d, d) = d > 1, the element n=dn = d does not contribute, so the count of nn with 1n<d1 \leq n < d and gcd(n,d)=1\gcd(n,d) = 1 equals φ(d)\varphi(d). \square

Theorem 2 (Euler’s Product Formula)

For n=p1a1p2a2pkakn = p_1^{a_1} p_2^{a_2} \cdots p_k^{a_k}:

φ(n)=npn(11p).\varphi(n) = n \prod_{p \mid n} \left(1 - \frac{1}{p}\right).

Proof. We prove two properties and combine them.

(i) Multiplicativity. If gcd(m,n)=1\gcd(m, n) = 1, then φ(mn)=φ(m)φ(n)\varphi(mn) = \varphi(m)\varphi(n). By the Chinese Remainder Theorem, the canonical ring isomorphism Z/mnZZ/mZ×Z/nZ\mathbb{Z}/mn\mathbb{Z} \cong \mathbb{Z}/m\mathbb{Z} \times \mathbb{Z}/n\mathbb{Z} restricts to a group isomorphism (Z/mnZ)×(Z/mZ)××(Z/nZ)×(\mathbb{Z}/mn\mathbb{Z})^\times \cong (\mathbb{Z}/m\mathbb{Z})^\times \times (\mathbb{Z}/n\mathbb{Z})^\times. Comparing cardinalities gives φ(mn)=φ(m)φ(n)\varphi(mn) = \varphi(m)\varphi(n).

(ii) Prime powers. For a prime power pap^a, an integer k{1,,pa}k \in \{1, \ldots, p^a\} satisfies gcd(k,pa)>1\gcd(k, p^a) > 1 if and only if pkp \mid k. There are pa1p^{a-1} such multiples, so φ(pa)=papa1=pa(11/p)\varphi(p^a) = p^a - p^{a-1} = p^a(1 - 1/p).

Applying (i) inductively to the prime factorization n=piain = \prod p_i^{a_i} and substituting (ii) yields the product formula. \square

Theorem 3 (Answer Formula)

The total number of reduced proper fractions with denominator N\leq N is:

d=2Nφ(d)=(d=1Nφ(d))1.\sum_{d=2}^{N} \varphi(d) = \left(\sum_{d=1}^{N} \varphi(d)\right) - 1.

Proof. By Theorem 1, the count is d=2Nφ(d)\sum_{d=2}^{N} \varphi(d). (We begin at d=2d = 2 since d=1d = 1 admits no proper fractions: the condition 1n<11 \leq n < 1 has no solutions.) Since φ(1)=1\varphi(1) = 1, this equals d=1Nφ(d)1\sum_{d=1}^{N} \varphi(d) - 1. \square

Lemma (Totient Sieve Correctness)

The sieve algorithm that initializes φ[d]=d\varphi[d] = d for all dd and, for each prime pNp \leq N, updates φ[m]φ[m](p1)/p\varphi[m] \leftarrow \varphi[m] \cdot (p-1)/p for all multiples mm of pp, correctly computes φ(d)\varphi(d) for all dNd \leq N.

Proof. After the sieve completes, each entry satisfies:

φ[d]=dpNp primepdp1p.\varphi[d] = d \cdot \prod_{\substack{p \leq N \\ p \text{ prime} \\ p \mid d}} \frac{p-1}{p}.

Every prime pp dividing dd satisfies pdNp \leq d \leq N, so the product ranges over all prime divisors of dd. By Theorem 2, this equals φ(d)\varphi(d).

Primes are correctly identified during the sieve: dd is prime if and only if φ[d]=d\varphi[d] = d when dd is first visited (since all primes p<dp < d dividing dd would have already modified φ[d]\varphi[d], and dd is composite if and only if it has such a prime divisor). \square

Remark (Asymptotic)

By a classical result in analytic number theory, d=1Nφ(d)=3N2π2+O(NlogN)\sum_{d=1}^{N} \varphi(d) = \frac{3N^2}{\pi^2} + O(N \log N). For N=106N = 10^6, this gives approximately 3.04×10113.04 \times 10^{11}.

Editorial

The denominator-by-denominator interpretation is the key simplification. For a fixed dd, exactly φ(d)\varphi(d) numerators produce reduced proper fractions, so the whole problem becomes the summatory totient

d=2106φ(d).\sum_{d=2}^{10^6} \varphi(d).

Computing each φ(d)\varphi(d) independently would waste work because nearby values share prime factors. The sieve avoids that. We begin with φ(d)=d\varphi(d) = d for every dd, and whenever we discover a prime pp, we apply the factor (11/p)(1 - 1/p) to every multiple of pp. By the time the sieve finishes, every denominator has been filtered by exactly its distinct prime divisors, so the array contains the correct totients and summing it gives the answer.

Pseudocode

Create an array phi with phi[d] = d for every d from 0 to N.

For each integer p from 2 to N:
    If phi[p] is still equal to p, then p is prime
        For each multiple m of p:
            replace phi[m] by phi[m] x (p - 1) / p

Add phi[2] through phi[N].
Return that total.

Complexity Analysis

Time: The sieve visits, for each prime pNp \leq N, all N/p\lfloor N/p \rfloor multiples. By Mertens’ second theorem:

pNp primeNp=N(lnlnN+M+O(1/lnN))=O(NloglogN),\sum_{\substack{p \leq N \\ p \text{ prime}}} \frac{N}{p} = N \cdot \left(\ln \ln N + M + O(1/\ln N)\right) = O(N \log \log N),

where MM is the Meissel—Mertens constant.

  • Time: O(NloglogN)O(N \log \log N).
  • Space: O(N)O(N) for the array φ[1..N]\varphi[1..N].

Answer

303963552391\boxed{303963552391}

Code

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

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

int main() {
    // Count reduced proper fractions n/d with d <= 1,000,000.
    // Answer = sum of phi(d) for d = 2 to N (Theorem 3).
    // Euler totient sieve in O(N log log N).

    const int N = 1000000;
    vector<int> phi(N + 1);
    iota(phi.begin(), phi.end(), 0);  // phi[i] = i

    for (int p = 2; p <= N; p++) {
        if (phi[p] == p) {  // p is prime
            for (int m = p; m <= N; m += p) {
                phi[m] = phi[m] / p * (p - 1);
            }
        }
    }

    long long total = 0;
    for (int d = 2; d <= N; d++) {
        total += phi[d];
    }

    cout << total << endl;
    return 0;
}