All Euler problems
Project Euler

Fraction Tree Traversal

The Stern-Brocot tree contains every positive rational number exactly once in lowest terms. Starting from (1)/(1) at the root, perform a breadth-first traversal and collect fractions. Find the sum...

Source sync Apr 19, 2026
Problem #0968
Level Level 39
Solved By 86
Languages C++, Python
Answer 885362394
Length 228 words
searchnumber_theorybrute_force

Problem Statement

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

Define as the sum of over all quintuples of non-negative integers such that the sum of each two of the five variables is restricted by a given value. In other words, , , etc.

For example, and .

Define a sequence as follows:

  • , ;
  • for .

Also define .

Find . Give your answer modulo .

Problem 968: Fraction Tree Traversal

Mathematical Analysis

Stern-Brocot Tree Structure

Definition. The Stern-Brocot tree is a binary tree where each node ab\frac{a}{b} has:

  • Left child: aa+b\frac{a}{a+b}
  • Right child: a+bb\frac{a+b}{b}

The root is 11\frac{1}{1}.

Theorem (Stern-Brocot). Every positive rational pq\frac{p}{q} with gcd(p,q)=1\gcd(p, q) = 1 appears exactly once in the Stern-Brocot tree.

Proof sketch. The tree is constructed by mediant insertion. At each node, the left subtree contains rationals smaller than the node, and the right subtree contains larger rationals. The Euclidean algorithm establishes a bijection between paths in the tree and positive rationals in lowest terms. \square

BFS Order

Level 0: 11\frac{1}{1} Level 1: 12,21\frac{1}{2}, \frac{2}{1} Level 2: 13,32,23,31\frac{1}{3}, \frac{3}{2}, \frac{2}{3}, \frac{3}{1} Level 3: 14,43,35,52,25,53,34,41\frac{1}{4}, \frac{4}{3}, \frac{3}{5}, \frac{5}{2}, \frac{2}{5}, \frac{5}{3}, \frac{3}{4}, \frac{4}{1}

At level kk, there are 2k2^k nodes.

Properties

Proposition. The sum of all fractions at level kk is 32k13 \cdot 2^{k-1} for k1k \ge 1.

Proposition. At each level, the numerators and denominators are symmetric: if ab\frac{a}{b} appears, so does ba\frac{b}{a}.

Derivation

Editorial

BFS using a queue. We repeat until 1000 fractions have been processed. We evaluate the closed-form expressions derived above directly from the relevant parameters and return the resulting value.

Pseudocode

Initialize queue with $\frac{1}{1}$
Dequeue a fraction $\frac{a}{b}$; add $a$ to the sum
Enqueue children $\frac{a}{a+b}$ and $\frac{a+b}{b}$
Repeat until 1000 fractions have been processed

Proof of Correctness

BFS explores all nodes at depth dd before depth d+1d+1. The tree is infinite, so 1000 nodes are always available.

Complexity Analysis

O(K)O(K) time and space for K=1000K = 1000 fractions.

Answer

885362394\boxed{885362394}

Code

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

C++ project_euler/problem_968/solution.cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
    queue<pair<long long,long long>> q;
    q.push({1,1});
    long long total = 0;
    for (int i = 0; i < 1000; i++) {
        auto [a,b] = q.front(); q.pop();
        total += a;
        q.push({a, a+b});
        q.push({a+b, b});
    }
    cout << total << endl;
    return 0;
}