All Euler problems
Project Euler

Counting Fractions in a Range

How many reduced proper fractions n/d with gcd(n, d) = 1 lie strictly between 1/3 and 1/2 for d <= 12,000?

Source sync Apr 19, 2026
Problem #0073
Level Level 02
Solved By 28,301
Languages C++, Python
Answer 7295372
Length 389 words
number_theorylinear_algebrabrute_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, \textcolor{red}{\mathbf{\frac 3 8}}, \textcolor{red}{\mathbf{\frac 2 5}}, \textcolor{red}{\mathbf{\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 $3$ fractions between $\dfrac 1 3$ and $\dfrac 1 2$.

How many fractions lie between $\dfrac 1 3$ and $\dfrac 1 2$ in the sorted set of reduced proper fractions for $d \le 12\,000$?

Problem 73: Counting Fractions in a Range

Mathematical Analysis

Theorem 1 (Range Bounds for Fixed Denominator)

For a fixed denominator d2d \geq 2, the integers nn satisfying 1/3<n/d<1/21/3 < n/d < 1/2 and gcd(n,d)=1\gcd(n, d) = 1 are precisely those in the set:

N(d)={nZ:d/3+1n(d1)/2, gcd(n,d)=1}.\mathcal{N}(d) = \{n \in \mathbb{Z} : \lfloor d/3 \rfloor + 1 \leq n \leq \lfloor (d-1)/2 \rfloor,\ \gcd(n, d) = 1\}.

Proof. The condition n/d>1/3n/d > 1/3 gives n>d/3n > d/3, so nd/3+1n \geq \lfloor d/3 \rfloor + 1 (since nn is a positive integer and d/3d/3 may not be an integer). The condition n/d<1/2n/d < 1/2 gives n<d/2n < d/2, so nd/21=(d1)/2n \leq \lceil d/2 \rceil - 1 = \lfloor (d-1)/2 \rfloor. The fraction is reduced if and only if gcd(n,d)=1\gcd(n, d) = 1. \square

Theorem 2 (Mobius Identity for Coprimality)

For any positive integers nn and dd:

[gcd(n,d)=1]=kgcd(n,d)μ(k),[\gcd(n,d) = 1] = \sum_{k \mid \gcd(n,d)} \mu(k),

where μ\mu is the Mobius function and [][\cdot] denotes the Iverson bracket.

Proof. Let g=gcd(n,d)g = \gcd(n, d) and define F(g)=kgμ(k)F(g) = \sum_{k \mid g} \mu(k). By the Mobius inversion identity (or equivalently, since μ\mu is the Dirichlet inverse of the constant function 1\mathbf{1}), we have F=μ1=εF = \mu * \mathbf{1} = \varepsilon, where ε\varepsilon is the identity for Dirichlet convolution: ε(m)=[m=1]\varepsilon(m) = [m = 1].

Explicitly, for g=p1a1prarg = p_1^{a_1} \cdots p_r^{a_r} with g>1g > 1:

F(g)=i=1r(j=0aiμ(pij))=i=1r(1+(1)+0+)=i=1r0=0,F(g) = \prod_{i=1}^{r} \left(\sum_{j=0}^{a_i} \mu(p_i^j)\right) = \prod_{i=1}^{r}(1 + (-1) + 0 + \cdots) = \prod_{i=1}^{r} 0 = 0,

and F(1)=μ(1)=1F(1) = \mu(1) = 1. Hence F(g)=[g=1]=[gcd(n,d)=1]F(g) = [g = 1] = [\gcd(n,d) = 1]. \square

Theorem 3 (Mobius Summation Formula)

The total count of reduced fractions between 1/31/3 and 1/21/2 with denominator N\leq N is:

C(N)=k=1Nμ(k)G ⁣(Nk),C(N) = \sum_{k=1}^{N} \mu(k) \cdot G\!\left(\left\lfloor \frac{N}{k} \right\rfloor\right),

where G(M)=d=2M((d1)/2d/3)G(M) = \sum_{d=2}^{M} \left(\lfloor (d-1)/2 \rfloor - \lfloor d/3 \rfloor\right) counts the total integers in the range (d/3,d/2)(d/3, d/2) summed over denominators d=2,,Md = 2, \ldots, M (without the coprimality constraint).

Proof. Starting from:

C(N)=d=2NnN(d)1=d=2Nd/3<n<d/2[gcd(n,d)=1].C(N) = \sum_{d=2}^{N} \sum_{n \in \mathcal{N}(d)} 1 = \sum_{d=2}^{N} \sum_{\lfloor d/3 \rfloor < n < \lceil d/2 \rceil} [\gcd(n,d) = 1].

Applying Theorem 2:

C(N)=d=2Nnkgcd(n,d)μ(k).C(N) = \sum_{d=2}^{N} \sum_{n} \sum_{k \mid \gcd(n,d)} \mu(k).

Exchanging summation order with the substitutions d=kdd = kd', n=knn = kn':

C(N)=k=1Nμ(k)d=1N/k#{nZ:d/3<n<d/2}.C(N) = \sum_{k=1}^{N} \mu(k) \sum_{d'=1}^{\lfloor N/k \rfloor} \#\{n' \in \mathbb{Z} : d'/3 < n' < d'/2\}.

The inner count equals (d1)/2d/3\lfloor (d'-1)/2 \rfloor - \lfloor d'/3 \rfloor when this is non-negative, and 0 otherwise. This gives the stated formula. \square

Remark (Mobius Sieve)

The Mobius function μ(n)\mu(n) can be computed for all nNn \leq N via a sieve analogous to the Sieve of Eratosthenes:

  1. Initialize μ[n]=1\mu[n] = 1 for all nn.
  2. For each prime pNp \leq N: negate μ[m]\mu[m] for all multiples mm of pp; set μ[m]=0\mu[m] = 0 for all multiples mm of p2p^2.

This correctly computes μ\mu because: μ(n)=0\mu(n) = 0 if nn has a squared prime factor; otherwise μ(n)=(1)k\mu(n) = (-1)^k where kk is the number of distinct prime factors, and each prime negation toggles the sign.

Editorial

The direct approach would inspect every denominator dd, generate the numerators in the interval (d/3,d/2)(d/3, d/2), and then filter by gcd(n,d)=1\gcd(n,d)=1. The Mobius identity lets us perform that filtering in bulk instead of checking gcds one fraction at a time.

After expanding the coprimality condition with μ\mu, the problem reorganizes into divisor classes indexed by kk. For a fixed kk, we only need to count how many raw numerators fall into the interval for denominators up to N/k\lfloor N/k \rfloor; the Mobius value μ(k)\mu(k) then decides whether that whole class contributes positively, negatively, or not at all. So the candidates are generated by the interval bounds for each denominator, while the Mobius weights perform the filtering that enforces reduced fractions.

Pseudocode

Sieve the Mobius function up to N.
Set the final count to 0.

For each divisor scale k from 1 to N:
    If mu[k] is 0, continue to the next scale

    Let limit be floor(N / k)
    Set a subtotal for this scale to 0

    For each denominator d from 2 to limit:
        Let lo be the smallest integer strictly greater than d / 3
        Let hi be the largest integer strictly smaller than d / 2

        If the interval from lo to hi is nonempty:
            add its size to the subtotal

    Add mu[k] times this subtotal to the final count

Return the final count.

Complexity Analysis

Mobius sieve: O(NloglogN)O(N \log \log N).

Double sum: The total number of iterations is k=1NN/k=O(NlogN)\sum_{k=1}^{N} \lfloor N/k \rfloor = O(N \log N) (harmonic sum).

  • Time: O(NlogN)O(N \log N).
  • Space: O(N)O(N) for the Mobius sieve.

For N=12,000N = 12{,}000, this runs in well under a second.

Answer

7295372\boxed{7295372}

Code

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

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

int main() {
    // Count reduced fractions n/d with 1/3 < n/d < 1/2, d <= 12000.
    // Mobius summation: C(N) = sum_k mu(k) * G(floor(N/k)).

    const int N = 12000;

    // Sieve the Mobius function
    vector<int> mu(N + 1, 1);
    vector<bool> is_prime(N + 1, true);

    for (int p = 2; p <= N; p++) {
        if (is_prime[p]) {
            for (int m = 2 * p; m <= N; m += p)
                is_prime[m] = false;
            for (long long m = (long long)p * p; m <= N; m += (long long)p * p)
                mu[m] = 0;
            for (int m = p; m <= N; m += p)
                mu[m] *= -1;
        }
    }

    long long count = 0;
    for (int k = 1; k <= N; k++) {
        if (mu[k] == 0) continue;
        int limit = N / k;
        long long c = 0;
        for (int d = 2; d <= limit; d++) {
            int lo = d / 3 + 1;
            int hi = (d - 1) / 2;
            if (hi >= lo) c += hi - lo + 1;
        }
        count += mu[k] * c;
    }

    cout << count << endl;
    return 0;
}