#!/usr/bin/env python3 """ Created on 09 Oct 2021 @author: David Doblas Jiménez @email: daviddoji@pm.me Solution for problem 57 of Project Euler https://projecteuler.net/problem=57 """ from fractions import Fraction from utils import timeit @timeit("Problem 57") def compute(): """ 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: 1 + 1/2 = 3/2 = 1.5 1 + 1/2+1/2 = 7/5 = 1.4 1 + 1/2+1/2+1/2 = 17/12 = 1.41666... 1 + 1/2+1/2+1/2+1/2 = 41/29 = 1.41379... The next three expansions are 99/70, 239/169, and 577/408, but the eighth expansion, 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? """ ans = 0 f = Fraction(1, 2) for i in range(1000): f = 1 / (2 + f) result = 1 + f if len(str(result.numerator)) > len(str(result.denominator)): ans += 1 return ans if __name__ == "__main__": print(f"Result for Problem 57: {compute()}")