All Euler problems
Project Euler

Square Root Convergents

The square root of 2 can be expressed as the infinite continued fraction sqrt(2) = 1 + cfrac12 + cfrac12 + cfrac12 +... By expanding this for the first one thousand iterations, one obtains successi...

Source sync Apr 19, 2026
Problem #0057
Level Level 02
Solved By 46,711
Languages C++, Python
Answer 153
Length 493 words
sequencelinear_algebraalgebra

Problem Statement

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

It is possible to show that the square root of two can be expressed as an infinite continued fraction. $$\sqrt 2 =1+ \frac 1 {2+ \frac 1 {2 +\frac 1 {2+ \dots}}}$$ By expanding this for the first four iterations, we get:

$1 + \frac 1 2 = \frac 32 = 1.5$

$1 + \frac 1 {2 + \frac 1 2} = \frac 7 5 = 1.4$

$1 + \frac 1 {2 + \frac 1 {2+\frac 1 2}} = \frac {17}{12} = 1.41666 \dots$

$1 + \frac 1 {2 + \frac 1 {2+\frac 1 {2+\frac 1 2}}} = \frac {41}{29} = 1.41379 \dots$

The next three expansions are $\frac {99}{70}$, $\frac {239}{169}$, and $\frac {577}{408}$, but the eighth expansion, $\frac {1393}{985}$, is the first example where the number of digits in the numerator exceeds the number of digits in the denominator.

In the first one-thousand expansions, how many fractions contain a numerator with more digits than the denominator?

Problem 57: Square Root Convergents

Mathematical Development

Definition 1 (Continued Fraction Expansion). The continued fraction representation of 2\sqrt{2} is [1;2][1; \overline{2}], meaning a0=1a_0 = 1 and ak=2a_k = 2 for all k1k \geq 1. The nn-th expansion refers to the rational number obtained after nn substitution steps.

Theorem 1 (Recurrence Relation). Define the sequence of fractions pn/qnp_n/q_n by p0/q0=1/1p_0/q_0 = 1/1 and the recurrence

pn=pn1+2qn1,qn=pn1+qn1,n1.p_n = p_{n-1} + 2q_{n-1}, \qquad q_n = p_{n-1} + q_{n-1}, \qquad n \geq 1.

Then pn/qnp_n/q_n is the nn-th expansion of the continued fraction for 2\sqrt{2}.

Proof. The nn-th expansion satisfies pn/qn=1+1/(1+pn1/qn1)p_n/q_n = 1 + 1/(1 + p_{n-1}/q_{n-1}). Simplifying:

pnqn=1+11+pn1qn1=1+qn1qn1+pn1=(qn1+pn1)+qn1qn1+pn1=pn1+2qn1pn1+qn1.\frac{p_n}{q_n} = 1 + \frac{1}{1 + \frac{p_{n-1}}{q_{n-1}}} = 1 + \frac{q_{n-1}}{q_{n-1} + p_{n-1}} = \frac{(q_{n-1} + p_{n-1}) + q_{n-1}}{q_{n-1} + p_{n-1}} = \frac{p_{n-1} + 2q_{n-1}}{p_{n-1} + q_{n-1}}.

Reading off numerator and denominator gives the stated recurrence. One verifies p1/q1=(1+2)/(1+1)=3/2p_1/q_1 = (1 + 2)/( 1 + 1) = 3/2, which is the first expansion 1+1/21 + 1/2. \square

Theorem 2 (Matrix Formulation). The recurrence admits the matrix representation

(pnqn)=An(11),A=(1211).\begin{pmatrix} p_n \\ q_n \end{pmatrix} = A^n \begin{pmatrix} 1 \\ 1 \end{pmatrix}, \qquad A = \begin{pmatrix} 1 & 2 \\ 1 & 1 \end{pmatrix}.

Proof. Direct substitution: A(pn1qn1)=(pn1+2qn1pn1+qn1)=(pnqn)A \begin{pmatrix} p_{n-1} \\ q_{n-1} \end{pmatrix} = \begin{pmatrix} p_{n-1} + 2q_{n-1} \\ p_{n-1} + q_{n-1} \end{pmatrix} = \begin{pmatrix} p_n \\ q_n \end{pmatrix}. The result follows by induction with (p0q0)=(11)\begin{pmatrix} p_0 \\ q_0 \end{pmatrix} = \begin{pmatrix} 1 \\ 1 \end{pmatrix}. \square

Lemma 1 (Eigenvalues of AA). The matrix AA has eigenvalues λ1=1+2\lambda_1 = 1 + \sqrt{2} and λ2=12\lambda_2 = 1 - \sqrt{2}, with corresponding eigenvectors v1=(2,  1)v_1 = (\sqrt{2},\; 1)^\top and v2=(2,  1)v_2 = (-\sqrt{2},\; 1)^\top.

Proof. The characteristic polynomial is det(AλI)=(1λ)22=λ22λ1=0\det(A - \lambda I) = (1 - \lambda)^2 - 2 = \lambda^2 - 2\lambda - 1 = 0, yielding λ=1±2\lambda = 1 \pm \sqrt{2} by the quadratic formula. For λ1=1+2\lambda_1 = 1 + \sqrt{2}: Av1=(2+2,  2+1)=(1+2)(2,  1)=λ1v1A v_1 = (\sqrt{2} + 2,\; \sqrt{2} + 1)^\top = (1 + \sqrt{2})(\sqrt{2},\; 1)^\top = \lambda_1 v_1. The verification for v2v_2 is analogous. \square

Theorem 3 (Asymptotic Growth). As nn \to \infty,

pn=12[(1+2)n+1+(12)n+1],qn=122[(1+2)n+1(12)n+1].p_n = \frac{1}{2}\bigl[(1 + \sqrt{2})^{n+1} + (1 - \sqrt{2})^{n+1}\bigr], \qquad q_n = \frac{1}{2\sqrt{2}}\bigl[(1 + \sqrt{2})^{n+1} - (1 - \sqrt{2})^{n+1}\bigr].

In particular, pn,qnC(1+2)np_n, q_n \sim C \cdot (1 + \sqrt{2})^n as nn \to \infty, and pn/qn2p_n/q_n \to \sqrt{2}.

Proof. Decompose (11)=αv1+βv2\begin{pmatrix} 1 \\ 1 \end{pmatrix} = \alpha v_1 + \beta v_2 where α=122(2+1)\alpha = \frac{1}{2\sqrt{2}}(\sqrt{2} + 1) and β=122(21)\beta = \frac{1}{2\sqrt{2}}(\sqrt{2} - 1). Then

(pnqn)=αλ1nv1+βλ2nv2.\begin{pmatrix} p_n \\ q_n \end{pmatrix} = \alpha \lambda_1^n v_1 + \beta \lambda_2^n v_2.

Since λ2=210.4142<1|\lambda_2| = |\sqrt{2} - 1| \approx 0.4142 < 1, the λ2n\lambda_2^n term decays exponentially, so pn/qn21/1=2p_n/q_n \to \sqrt{2} \cdot 1 / 1 = \sqrt{2} (from the ratio of components of v1v_1). The closed-form expressions follow from expanding the decomposition. \square

Lemma 2 (Connection to Pell Numbers). The denominators qnq_n satisfy the second-order recurrence qn=2qn1+qn2q_n = 2q_{n-1} + q_{n-2} for n2n \geq 2, which is the recurrence for half-companion Pell numbers.

Proof. From Theorem 1, pn1=qnqn1p_{n-1} = q_n - q_{n-1}. Substituting into qn=pn1+qn1q_n = p_{n-1} + q_{n-1} at the next step: qn+1=pn+qn=(pn1+2qn1)+qnq_{n+1} = p_n + q_n = (p_{n-1} + 2q_{n-1}) + q_n. But pn1=qnqn1p_{n-1} = q_n - q_{n-1}, so qn+1=(qnqn1)+2qn1+qn=2qn+qn1q_{n+1} = (q_n - q_{n-1}) + 2q_{n-1} + q_n = 2q_n + q_{n-1}. \square

Theorem 4 (Digit Excess Criterion). The numerator pnp_n has strictly more decimal digits than qnq_n if and only if

log10pn>log10qn.\lfloor \log_{10} p_n \rfloor > \lfloor \log_{10} q_n \rfloor.

This occurs when there exists an integer mm such that qn<10mpnq_n < 10^m \leq p_n.

Proof. The number of digits of a positive integer xx is log10x+1\lfloor \log_{10} x \rfloor + 1. Thus pnp_n has more digits than qnq_n iff log10pn+1>log10qn+1\lfloor \log_{10} p_n \rfloor + 1 > \lfloor \log_{10} q_n \rfloor + 1, i.e., log10pn>log10qn\lfloor \log_{10} p_n \rfloor > \lfloor \log_{10} q_n \rfloor. This is equivalent to the existence of an integer mm with log10qn<mlog10pn\log_{10} q_n < m \leq \log_{10} p_n, i.e., qn<10mpnq_n < 10^m \leq p_n. \square

Proposition 1 (Frequency Estimate). The digit-excess events occur with approximate density log102/log10(1+2)0.1505/0.38270.393\log_{10}\sqrt{2} / \log_{10}(1 + \sqrt{2}) \approx 0.1505 / 0.3827 \approx 0.393 among the iterations, predicting roughly 393393 events per 10001000 iterations. The exact count is 153153.

Remark. The discrepancy between the heuristic and the exact count arises because the simple density argument counts the “fractional part in an interval” events, but the actual condition involves the floor function applied to log10\log_{10} of two correlated sequences, which introduces a more complex pattern. In practice, the digit-excess occurs roughly every 1000/1536.541000/153 \approx 6.54 iterations.

Editorial

We generate the first 1000 convergents of 2\sqrt{2} using the recurrence for successive numerators and denominators. After each update, only the decimal lengths of the current numerator and denominator need to be compared, so every expansion contributes either zero or one to the running count. The final count is the number of convergents whose numerator has more digits than the denominator.

Pseudocode

Algorithm: Counting Expansions of sqrt(2) with a Longer Numerator
Require: The first 1000 convergents of the continued fraction for sqrt(2).
Ensure: The number of convergents whose numerator has more decimal digits than the denominator.
1: Initialize the first convergent after 1 as (p, q) ← (3, 2) and set count ← 0.
2: For each expansion index in {1, 2, ..., 1000} do:
3:     If the decimal length of p exceeds that of q, increment count.
4:     Update (p, q) ← (p + 2q, p + q).
5: Return count.

Complexity Analysis

Theorem 5 (Time Complexity). The algorithm runs in O(N2)O(N^2) digit-level operations, where N=1000N = 1000.

Proof. By Theorem 3, pnp_n and qnq_n grow as (1+2)n(1 + \sqrt{2})^n, so the number of digits at step nn is Dn=Θ(nlog10(1+2))=Θ(n)D_n = \Theta(n \log_{10}(1 + \sqrt{2})) = \Theta(n). Each iteration performs two big-integer additions on numbers with O(Dn)O(D_n) digits, costing O(Dn)O(D_n). The digit comparison costs O(1)O(1) (just compare lengths). The total work is n=1NO(n)=O(N2)\sum_{n=1}^{N} O(n) = O(N^2). For N=1000N = 1000, this is O(106)O(10^6). \square

Space: O(DN)=O(N)O(D_N) = O(N) for storing pp and qq, which at step 1000 have approximately 1000×0.38273831000 \times 0.3827 \approx 383 digits.

Answer

153\boxed{153}

Code

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

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

// Big integer as vector of digits (least significant first)
typedef vector<int> BigInt;

BigInt fromInt(int x) {
    BigInt result;
    if (x == 0) { result.push_back(0); return result; }
    while (x > 0) {
        result.push_back(x % 10);
        x /= 10;
    }
    return result;
}

BigInt add(const BigInt& a, const BigInt& b) {
    BigInt result;
    int carry = 0;
    int n = max(a.size(), b.size());
    for (int i = 0; i < n || carry; i++) {
        int sum = carry;
        if (i < (int)a.size()) sum += a[i];
        if (i < (int)b.size()) sum += b[i];
        result.push_back(sum % 10);
        carry = sum / 10;
    }
    return result;
}

int numDigits(const BigInt& a) {
    return (int)a.size();
}

int main() {
    // Recurrence: p_n = p_{n-1} + 2*q_{n-1}, q_n = p_{n-1} + q_{n-1}
    BigInt p = fromInt(3);
    BigInt q = fromInt(2);

    int count = 0;
    for (int i = 1; i <= 1000; i++) {
        if (numDigits(p) > numDigits(q)) {
            count++;
        }
        BigInt twoq = add(q, q);
        BigInt p_new = add(p, twoq);
        BigInt q_new = add(p, q);
        p = p_new;
        q = q_new;
    }

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