All Euler problems
Project Euler

Continued Fraction Convergents

The continued fraction expansion of sqrt(2) is [1; 2, 2, 2,...]. Let p_n/q_n denote the n -th convergent. Find p_100 + q_100 mod 10^9+7.

Source sync Apr 19, 2026
Problem #0925
Level Level 36
Solved By 179
Languages C++, Python
Answer 400034379
Length 282 words
modular_arithmeticsequencelinear_algebra

Problem Statement

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

Let \(B(n)\) be the smallest number larger than \(n\) that can be formed by rearranging digits of \(n\), or \(0\) if no such number exists. For example, \(B(245) = 254\) and \(B(542) = 0\).

Define \(\displaystyle T(N) = \sum _{n=1}^N B(n^2)\). You are given \(T(10)=270\) and \(T(100)=335316\).

Define \(\displaystyle T(N) = \sum _{n=1}^N B(n^2)\). You are given \(T(10)=270\) and \(T(100)=335316\).

Problem 925: Continued Fraction Convergents

Mathematical Foundation

Theorem 1 (Convergent Recurrence). Let [a0;a1,a2,][a_0; a_1, a_2, \ldots] be a simple continued fraction. Define p1=1p_{-1} = 1, p0=a0p_0 = a_0, q1=0q_{-1} = 0, q0=1q_0 = 1, and for n1n \geq 1:

pn=anpn1+pn2,qn=anqn1+qn2.p_n = a_n p_{n-1} + p_{n-2}, \qquad q_n = a_n q_{n-1} + q_{n-2}.

Then pn/qnp_n / q_n is the nn-th convergent.

Proof. We prove by induction that

(pnpn1qnqn1)=i=0n(ai110).(M)\begin{pmatrix} p_n & p_{n-1} \\ q_n & q_{n-1} \end{pmatrix} = \prod_{i=0}^{n} \begin{pmatrix} a_i & 1 \\ 1 & 0 \end{pmatrix}. \tag{M}

Base case (n=0n = 0): The right side is (a0110)\begin{pmatrix} a_0 & 1 \\ 1 & 0 \end{pmatrix}, matching (p0,p1;q0,q1)=(a0,1;1,0)(p_0, p_{-1}; q_0, q_{-1}) = (a_0, 1; 1, 0).

Inductive step: Assuming (M) holds for n1n-1, multiply on the right by (an110)\begin{pmatrix} a_n & 1 \\ 1 & 0 \end{pmatrix}:

(pn1pn2qn1qn2)(an110)=(anpn1+pn2pn1anqn1+qn2qn1)=(pnpn1qnqn1).\begin{pmatrix} p_{n-1} & p_{n-2} \\ q_{n-1} & q_{n-2} \end{pmatrix} \begin{pmatrix} a_n & 1 \\ 1 & 0 \end{pmatrix} = \begin{pmatrix} a_n p_{n-1} + p_{n-2} & p_{n-1} \\ a_n q_{n-1} + q_{n-2} & q_{n-1} \end{pmatrix} = \begin{pmatrix} p_n & p_{n-1} \\ q_n & q_{n-1} \end{pmatrix}.

That pn/qnp_n/q_n equals the nn-th convergent follows from the standard theory of continued fractions (the finite truncation [a0;a1,,an][a_0; a_1, \ldots, a_n] evaluates to pn/qnp_n/q_n). \square

Theorem 2 (Determinant Identity / Pell Connection). For all n0n \geq 0:

pnqn1pn1qn=(1)n+1.p_n q_{n-1} - p_{n-1} q_n = (-1)^{n+1}.

In particular, for 2\sqrt{2}: pn22qn2=(1)n+1p_n^2 - 2 q_n^2 = (-1)^{n+1}.

Proof. Taking the determinant of both sides of (M):

pnqn1pn1qn=det(pnpn1qnqn1)=i=0ndet(ai110)=i=0n(1)=(1)n+1.p_n q_{n-1} - p_{n-1} q_n = \det\begin{pmatrix} p_n & p_{n-1} \\ q_n & q_{n-1} \end{pmatrix} = \prod_{i=0}^{n} \det\begin{pmatrix} a_i & 1 \\ 1 & 0 \end{pmatrix} = \prod_{i=0}^{n}(-1) = (-1)^{n+1}.

For 2\sqrt{2}, the Pell identity pn22qn2=(1)n+1p_n^2 - 2q_n^2 = (-1)^{n+1} follows from the fact that pn/qn2p_n/q_n \to \sqrt{2} and the determinant identity, combined with the recurrence structure. Specifically, one proves by induction that (pn,qn)(p_n, q_n) satisfies x22y2=(1)n+1x^2 - 2y^2 = (-1)^{n+1}. \square

Lemma 1 (Specialization to 2\sqrt{2}). For 2=[1;2,2,2,]\sqrt{2} = [1; 2, 2, 2, \ldots], we have a0=1a_0 = 1 and an=2a_n = 2 for n1n \geq 1, giving:

pn=2pn1+pn2,qn=2qn1+qn2,p_n = 2p_{n-1} + p_{n-2}, \quad q_n = 2q_{n-1} + q_{n-2},

with p0=1,p1=3,q0=1,q1=2p_0 = 1, p_1 = 3, q_0 = 1, q_1 = 2.

Proof. Direct substitution: p1=a1p0+p1=21+1=3p_1 = a_1 p_0 + p_{-1} = 2 \cdot 1 + 1 = 3 and q1=a1q0+q1=21+0=2q_1 = a_1 q_0 + q_{-1} = 2 \cdot 1 + 0 = 2. For n2n \geq 2, the recurrence with an=2a_n = 2 gives the stated formulas. \square

Theorem 3 (Matrix Exponentiation). For n1n \geq 1:

(pnpn1)=(2110)n1(31),\begin{pmatrix} p_n \\ p_{n-1} \end{pmatrix} = \begin{pmatrix} 2 & 1 \\ 1 & 0 \end{pmatrix}^{n-1} \begin{pmatrix} 3 \\ 1 \end{pmatrix},

enabling O(logn)O(\log n) computation via repeated squaring.

Proof. Rewrite pn=2pn1+pn2p_n = 2p_{n-1} + p_{n-2} as (pnpn1)=(2110)(pn1pn2)\begin{pmatrix} p_n \\ p_{n-1} \end{pmatrix} = \begin{pmatrix} 2 & 1 \\ 1 & 0 \end{pmatrix} \begin{pmatrix} p_{n-1} \\ p_{n-2} \end{pmatrix}. Iterating from the initial vector (p1,p0)T=(3,1)T(p_1, p_0)^T = (3, 1)^T yields the formula. The same applies to qnq_n with initial vector (2,1)T(2, 1)^T. \square

Lemma 2 (Growth Rate). The sequences pnp_n and qnq_n grow as Θ((1+2)n)\Theta((1+\sqrt{2})^n). Precisely, qn=(1+2)n+1(12)n+122q_n = \frac{(1+\sqrt{2})^{n+1} - (1-\sqrt{2})^{n+1}}{2\sqrt{2}}.

Proof. The characteristic equation of xn=2xn1+xn2x_n = 2x_{n-1} + x_{n-2} is λ22λ1=0\lambda^2 - 2\lambda - 1 = 0, with roots λ=1±2\lambda = 1 \pm \sqrt{2}. Solving with initial conditions q0=1,q1=2q_0 = 1, q_1 = 2 gives the closed form. Since 12<1|1 - \sqrt{2}| < 1, the second term vanishes exponentially. \square

Editorial

Compute p_n and q_n for the continued fraction expansion of sqrt(2), with recurrence p_n = 2p_{n-1} + p_{n-2}, q_n = 2q_{n-1} + q_{n-2}, starting from p_0=1, p_1=3, q_0=1, q_1=2. Return (p_n + q_n) mod 10^9+7. Key ideas:. We iterative approach: O(n) time.

Pseudocode

    Iterative approach: O(n) time
    p_prev = 1 // p_0
    p_curr = 3 // p_1
    q_prev = 1 // q_0
    q_curr = 2 // q_1

    For i from 2 to target_n:
        p_next = (2 * p_curr + p_prev) mod MOD
        p_prev = p_curr
        p_curr = p_next

        q_next = (2 * q_curr + q_prev) mod MOD
        q_prev = q_curr
        q_curr = q_next

    Return (p_curr + q_curr) mod MOD

Complexity Analysis

  • Time: O(n)O(n) for the iterative approach; O(logn)O(\log n) via 2×22 \times 2 matrix exponentiation.
  • Space: O(1)O(1) for the iterative approach; O(logn)O(\log n) stack space for recursive matrix exponentiation, or O(1)O(1) for iterative matrix exponentiation.

Answer

400034379\boxed{400034379}

Code

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

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

/*
 * Problem 925: Continued Fraction Convergents
 *
 * sqrt(2) = [1; 2, 2, 2, ...]. Find p_100 + q_100 mod 10^9+7.
 *
 * Convergent recurrence: p_n = 2*p_{n-1} + p_{n-2}, same for q.
 * Initial: p_0=1, p_1=3, q_0=1, q_1=2.
 *
 * Key results:
 *   - Pell equation: p_n^2 - 2*q_n^2 = (-1)^{n+1}
 *   - Best approximation: |sqrt(2) - p_n/q_n| < 1/(q_n * q_{n+1})
 *   - Growth: q_n ~ (1+sqrt(2))^n / (2*sqrt(2))
 *   - Matrix form: [p_n; p_{n-1}] = [[2,1],[1,0]]^{n-1} * [3;1]
 *
 * Complexity: O(n) iterative, O(log n) via matrix exponentiation.
 */

int main() {
    long long MOD = 1000000007;
    long long p0 = 1, p1 = 3, q0 = 1, q1 = 2;
    for (int i = 2; i <= 100; i++) {
        long long np = (2 * p1 % MOD + p0) % MOD;
        long long nq = (2 * q1 % MOD + q0) % MOD;
        p0 = p1; p1 = np;
        q0 = q1; q1 = nq;
    }
    cout << (p1 + q1) % MOD << endl;

    // Verify Pell equation for small n (without mod)
    long long pp0 = 1, pp1 = 3, qq0 = 1, qq1 = 2;
    for (int i = 2; i <= 10; i++) {
        long long npp = 2*pp1 + pp0;
        long long nqq = 2*qq1 + qq0;
        pp0 = pp1; pp1 = npp;
        qq0 = qq1; qq1 = nqq;
    }
    // p_10^2 - 2*q_10^2 should be +/- 1
    assert(abs(pp1*pp1 - 2*qq1*qq1) == 1);

    return 0;
}