Problem 56

Square root convergents

Problem 58

Square root convergents

Problem 57

It is possible to show that the square root of two can be expressed as an infinite continued fraction.
By expanding this for the first four iterations, we get:





The next three expansions are , , and , but the eighth expansion, , 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?
from itertools import starmap
from math import log10


def expansions(n):
    num, den = 2, 1  # r = 2
    for _ in range(n):
        yield num + den, num  # 1 + 1/r
        num, den = 2 * num + den, num  # r = 2 + 1/


def ndig(n):
    return int(log10(n) + 1)


def run(limit=1000):
    return sum(starmap(lambda num, den: ndig(num) > ndig(den), expansions(limit)))