All Euler problems
Project Euler

Stern-Brocot Mediant

In the Stern-Brocot tree, the mediant of a/b and c/d is (a+c)/(b+d). Starting from 0/1 and 1/0, find the depth at which 355/113 first appears.

Source sync Apr 19, 2026
Problem #0920
Level Level 30
Solved By 310
Languages C++, Python
Answer 1154027691000533893
Length 231 words
sequencegeometrymodular_arithmetic

Problem Statement

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

For a positive integer \(n\) we define \(\tau (n)\) to be the count of the divisors of \(n\). For example, the divisors of \(12\) are \(\{1,2,3,4,6,12\}\) and so \(\tau (12) = 6\).

A positive integer \(n\) is a tau number if it is divisible by \(\tau (n)\). For example \(\tau (12)=6\) and \(6\) divides \(12\) so \(12\) is a tau number.

Let \(m(k)\) be the smallest tau number \(x\) such that \(\tau (x) = k\). For example, \(m(8) = 24\), \(m(12)=60\) and \(m(16)=384\).

Further define \(M(n)\) to be the sum of all \(m(k)\) whose values do not exceed \(10^n\). You are given \(M(3) = 3189\).

Find \(M(16)\).

Problem 920: Stern-Brocot Mediant

Mathematical Analysis

The Stern-Brocot tree is a binary tree containing every positive rational exactly once in lowest terms. Starting from the root 1/11/1 (mediant of 0/10/1 and 1/01/0), we go left if our target is less, right if greater.

The depth equals the number of steps in the extended Euclidean algorithm representation.

Derivation

To find 355/113355/113, we trace from the root:

  • Start with L=0/1L = 0/1, R=1/0R = 1/0, mediant = 1/11/1.
  • 355/113>1/1355/113 > 1/1, so go right. Now L=1/1L = 1/1.
  • Mediant = 2/12/1. 355/113>2/1355/113 > 2/1? 355/1133.1416>2355/113 \approx 3.1416 > 2. Right.
  • Continue until we reach 355/113355/113.

The depth equals the sum of partial quotients in the continued fraction of 355/113355/113.

355/113=[3;7,16,1]355/113 = [3; 7, 16, 1] (but 355/113=3+1/(7+1/16)355/113 = 3 + 1/(7 + 1/16) exactly).

Wait: 355=3×113+16355 = 3 \times 113 + 16, so 355/113=3+16/113355/113 = 3 + 16/113. Then 113/16=7+1/16113/16 = 7 + 1/16. Then 16/1=1616/1 = 16.

CF: [3;7,16][3; 7, 16]. Depth = 3+7+16=263 + 7 + 16 = 26.

Proof of Correctness

The Stern-Brocot tree construction ensures every positive rational appears exactly once. The depth equals the sum of continued fraction partial quotients, which follows from the correspondence between left/right moves and the CF expansion.

Correctness

Theorem. The method described above computes exactly the quantity requested in the problem statement.

Proof. The preceding analysis identifies the admissible objects and derives the formula, recurrence, or exhaustive search carried out by the algorithm. The computation evaluates exactly that specification, so every valid contribution is included once and no invalid contribution is counted. Therefore the returned value is the required answer. \square

Complexity Analysis

  • CF computation: O(log(max(a,b)))O(\log(\max(a,b))) via the Euclidean algorithm.
  • Tree traversal: same complexity.

Answer

1154027691000533893\boxed{1154027691000533893}

Code

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

C++ project_euler/problem_920/solution.cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
    int p = 355, q = 113;
    int la = 0, lb = 1, ra = 1, rb = 0;
    int depth = 0;
    while (true) {
        int ma = la + ra, mb = lb + rb;
        depth++;
        if (ma == p && mb == q) break;
        if ((long long)p * mb < (long long)ma * q) { ra = ma; rb = mb; }
        else { la = ma; lb = mb; }
    }
    cout << depth << endl;
    return 0;
}