# Convergents of e (Euler Problem 65)¶

Back to overview.

Hence the sequence of the first ten convergents for √2 are:

1, 3/2, 7/5, 17/12, 41/29, 99/70, 239/169, 577/408, 1393/985, 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, 8/3, 11/4, 19/7, 87/32, 106/39, 193/71, 1264/465, 1457/536, ...

The sum of digits in the numerator of the 10th convergent is 1+4+5+7=17.

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

In [1]:

    def gcd(a, b):
        if b > a:
            a, b = b, a
        while a % b != 0:
            a, b = b, a % b
        return b

    def add_fractions(n1, d1, n2, d2):
        d = d1 * d2
        n1 = n1 * (d // d1)
        n2 = n2 * (d // d2)
        n = n1 + n2
        p = gcd(n, d)
        return (n // p, d // p)

In [2]:

    def next_expansion(previous_numerator, previous_denumerator, value):
        if previous_numerator == 0:
            return (value, 1)
        return add_fractions(previous_denumerator, previous_numerator, value, 1)

    e_sequence = [2] + [n for i in range(2, 1000, 2) for n in (1, i, 1)]

    n, d = 0, 1

    for i in range(100, 0, -1):
        n, d = next_expansion(n, d, e_sequence[i - 1])

    s = sum([int(l) for l in str(n)])
    print(s)
    assert(s == 272)

    272

In [ ]: