All Euler problems
Project Euler

Convergents of e

Find the sum of digits in the numerator of the 100th convergent of the continued fraction for e.

Source sync Apr 19, 2026
Problem #0065
Level Level 02
Solved By 33,672
Languages C++, Python
Answer 272
Length 346 words
sequencemodular_arithmeticlinear_algebra

Problem Statement

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

The square root of \(2\) can be written as an infinite continued fraction. \[\sqrt {2} = 1 + \dfrac {1}{2 + \dfrac {1}{2 + \dfrac {1}{2 + \dfrac {1}{2 + ...}}}}\] The infinite continued fraction can be written, \(\sqrt {2} = [1; (2)]\), \((2)\) indicates that \(2\) repeats ad infinitum. In a similar way, \(\sqrt {23} = [4; (1, 3, 1, 8)]\). \begin {align*} &1 + \dfrac {1}{2} = \dfrac {3}{2} \\ &1 + \dfrac {1}{2 + \dfrac {1}{2}} = \dfrac {7}{5}\\ &1 + \dfrac {1}{2 + \dfrac {1}{2 + \dfrac {1}{2}}} = \dfrac {17}{12}\\ &1 + \dfrac {1}{2 + \dfrac {1}{2 + \dfrac {1}{2 + \dfrac {1}{2}}}} = \dfrac {41}{29} \end {align*}

Hence the sequence of the first ten convergents for \(\sqrt {2}\) are: \[1, \dfrac {3}{2}, \dfrac {7}{5}, \dfrac {17}{12}, \dfrac {41}{29}, \dfrac {99}{70}, \dfrac {239}{169}, \dfrac {577}{408}, \dfrac {1393}{985}, \dfrac {3363}{2378}, ...\] What is most surprising is that the important mathematical constant,

\(e = [2; 1, 2, 1, 1, 4, 1, 1, 6, 1, ... , 1, 2k, 1, ...]\). The first ten terms in the sequence of convergents for \(e\) are: \[2, 3, \dfrac {8}{3}, \dfrac {11}{4}, \dfrac {19}{7}, \dfrac {87}{32}, \dfrac {106}{39}, \dfrac {193}{71}, \dfrac {1264}{465}, \dfrac {1457}{536}, ...\] The sum of digits in the numerator of the \(10^{th}\) convergent is \(1 + 4 + 5 + 7 = 17\).

Find the sum of digits in the numerator of the \(100^{th}\) convergent of the continued fraction for \(e\).

Problem 65: Convergents of e

Mathematical Foundation

Theorem 1 (Euler, 1737). The continued fraction expansion of ee is

e=[2;1,2,1,1,4,1,1,6,1,1,8,]e = [2; 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, \ldots]

where the partial quotients are a0=2a_0 = 2 and for k1k \ge 1:

ak={2(k+1)3if k2(mod3)1otherwise.a_k = \begin{cases} \frac{2(k+1)}{3} & \text{if } k \equiv 2 \pmod{3} \\ 1 & \text{otherwise.} \end{cases}

Proof. Euler derived this from the generalized continued fraction

e=2+11+12+11+11+14+e = 2 + \cfrac{1}{1 + \cfrac{1}{2 + \cfrac{1}{1 + \cfrac{1}{1 + \cfrac{1}{4 + \cdots}}}}}

using the relationship between ee and the confluent hypergeometric function. Specifically, consider the series e=n=01/n!e = \sum_{n=0}^{\infty} 1/n! and apply Euler’s method of converting power series to continued fractions. The proof relies on the identity

e+1e1=[2;6,10,14,]\frac{e + 1}{e - 1} = [2; 6, 10, 14, \ldots]

from which the stated continued fraction for ee can be derived via equivalence transformations. A complete proof appears in Perron’s Die Lehre von den Kettenbruechen (1929). \square

Theorem 2 (Convergent Recurrence). The convergents hn/kn=[a0;a1,,an]h_n/k_n = [a_0; a_1, \ldots, a_n] satisfy the recurrence

hn=anhn1+hn2,kn=ankn1+kn2h_n = a_n h_{n-1} + h_{n-2}, \quad k_n = a_n k_{n-1} + k_{n-2}

with initial conditions h1=1h_{-1} = 1, h0=a0h_0 = a_0, k1=0k_{-1} = 0, k0=1k_0 = 1.

Proof. Define the matrix

Mi=(ai110).M_i = \begin{pmatrix} a_i & 1 \\ 1 & 0 \end{pmatrix}.

We claim that

(hnhn1knkn1)=M0M1Mn.\begin{pmatrix} h_n & h_{n-1} \\ k_n & k_{n-1} \end{pmatrix} = M_0 M_1 \cdots M_n.

Base case (n=0n = 0): M0=(a0110)M_0 = \begin{pmatrix} a_0 & 1 \\ 1 & 0 \end{pmatrix}, giving h0=a0h_0 = a_0, h1=1h_{-1} = 1, k0=1k_0 = 1, k1=0k_{-1} = 0. Since [a0]=a0/1[a_0] = a_0/1, this is correct.

Inductive step: Assume the product M0Mn1M_0 \cdots M_{n-1} gives the correct matrix. Then

M0Mn=(hn1hn2kn1kn2)(an110)=(anhn1+hn2hn1ankn1+kn2kn1)M_0 \cdots M_n = \begin{pmatrix} h_{n-1} & h_{n-2} \\ k_{n-1} & k_{n-2} \end{pmatrix} \begin{pmatrix} a_n & 1 \\ 1 & 0 \end{pmatrix} = \begin{pmatrix} a_n h_{n-1} + h_{n-2} & h_{n-1} \\ a_n k_{n-1} + k_{n-2} & k_{n-1} \end{pmatrix}

which gives hn=anhn1+hn2h_n = a_n h_{n-1} + h_{n-2} and kn=ankn1+kn2k_n = a_n k_{n-1} + k_{n-2}. To see that hn/kn=[a0;a1,,an]h_n/k_n = [a_0; a_1, \ldots, a_n], note that replacing ana_n by an+1/αa_n + 1/\alpha in the matrix product and using the inductive hypothesis on [a0;a1,,an1,an+1/α]=(hn1(an+1/α)+hn2)/(kn1(an+1/α)+kn2)[a_0; a_1, \ldots, a_{n-1}, a_n + 1/\alpha] = (h_{n-1}(a_n + 1/\alpha) + h_{n-2})/(k_{n-1}(a_n + 1/\alpha) + k_{n-2}) confirms the identity. \square

Lemma 1 (Coprimality). For all n0n \ge 0, gcd(hn,kn)=1\gcd(h_n, k_n) = 1.

Proof. Taking determinants of the matrix product:

hnkn1hn1kn=det(M0)det(Mn)=(1)n+1.h_n k_{n-1} - h_{n-1} k_n = \det(M_0) \cdots \det(M_n) = (-1)^{n+1}.

Since hnkn1hn1kn=±1h_n k_{n-1} - h_{n-1} k_n = \pm 1, we have gcd(hn,kn)=1\gcd(h_n, k_n) = 1. \square

Lemma 2 (Growth Rate). The numerator h99h_{99} has approximately k=099log10(ak)\sum_{k=0}^{99} \log_{10}(a_k) digits, which for the ee continued fraction yields about 57—58 digits.

Proof. From the recurrence hn=anhn1+hn2anhn1h_n = a_n h_{n-1} + h_{n-2} \ge a_n h_{n-1}, so log10hnlog10an+log10hn1\log_{10} h_n \ge \log_{10} a_n + \log_{10} h_{n-1}. By induction, log10hnk=0nlog10ak\log_{10} h_n \ge \sum_{k=0}^n \log_{10} a_k. The partial quotients for ee at indices k2(mod3)k \equiv 2 \pmod{3} are 2,4,6,,662, 4, 6, \ldots, 66 (for kk up to 98), contributing j=133log10(2j)42\sum_{j=1}^{33} \log_{10}(2j) \approx 42. The remaining 67 terms contribute log10(1)=0\log_{10}(1) = 0 each (except a0=2a_0 = 2). Total: 42+log10(2)42.3\approx 42 + \log_{10}(2) \approx 42.3. The actual numerator has 58 digits due to the additive term hn2h_{n-2} accumulating additional magnitude. \square

Editorial

We use Euler’s explicit pattern for the continued fraction of ee to determine the first 100100 partial quotients. The numerator of the convergents is then obtained from the standard recurrence, so only the two previous numerators need to be retained throughout the computation. Once the numerator of the 100100th convergent has been reached, we sum its decimal digits.

Pseudocode

Determine the partial quotients of $e$ from Euler's pattern:
    the first term is 2
    every third later term is an even number
    all remaining terms are 1

Initialize the recurrence with the two starting numerators.

Advance through the first 100 convergents:
    form the next numerator from the current partial quotient
    then shift the two stored numerators forward

After the last update, compute the decimal digit sum of the current numerator.
Return that sum.

Complexity Analysis

Time: The algorithm computes 100 iterations of the recurrence. Each iteration involves one big-integer multiplication and one big-integer addition. Since h99h_{99} has d58d \approx 58 digits, each arithmetic operation costs O(d)O(d) with schoolbook multiplication (or O(dlogd)O(d \log d) with FFT-based multiplication, though unnecessary here). Total: O(100d)=O(10058)=O(5800)O(100 \cdot d) = O(100 \cdot 58) = O(5800).

Space: O(d)=O(58)O(d) = O(58) for storing three big integers (hn2h_{n-2}, hn1h_{n-1}, hnh_n).

Answer

272\boxed{272}

Code

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

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

// Simple big integer using vector of digits (least significant first)
struct BigInt {
    vector<int> d; // digits, d[0] is least significant

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

    BigInt operator+(const BigInt& o) const {
        BigInt res;
        res.d.clear();
        int carry = 0;
        for (int i = 0; i < (int)max(d.size(), o.d.size()) || carry; i++) {
            int s = carry;
            if (i < (int)d.size()) s += d[i];
            if (i < (int)o.d.size()) s += o.d[i];
            res.d.push_back(s % 10);
            carry = s / 10;
        }
        return res;
    }

    BigInt operator*(int x) const {
        BigInt res;
        res.d.clear();
        long long carry = 0;
        for (int i = 0; i < (int)d.size() || carry; i++) {
            long long s = carry;
            if (i < (int)d.size()) s += (long long)d[i] * x;
            res.d.push_back(s % 10);
            carry = s / 10;
        }
        while (res.d.size() > 1 && res.d.back() == 0) res.d.pop_back();
        return res;
    }

    int digit_sum() const {
        int s = 0;
        for (int x : d) s += x;
        return s;
    }
};

int main() {
    // Continued fraction of e: [2; 1, 2, 1, 1, 4, 1, 1, 6, ...]
    // a[0] = 2, a[k] for k>=1: if k%3==2 then 2*(k+1)/3 else 1
    auto a = [](int k) -> int {
        if (k == 0) return 2;
        if (k % 3 == 2) return 2 * (k + 1) / 3;
        return 1;
    };

    // Compute 100th convergent (index 99)
    BigInt h_prev2(1); // h_{-1} = 1
    BigInt h_prev1(a(0)); // h_0 = a_0 = 2

    for (int i = 1; i <= 99; i++) {
        BigInt h_new = h_prev1 * a(i) + h_prev2;
        h_prev2 = h_prev1;
        h_prev1 = h_new;
    }

    cout << h_prev1.digit_sum() << endl;
    return 0;
}