from lib_misc import gcd from lib_misc import get_digit_count 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) def next_expension(n, d): n, d = add_fractions(1, 1, n, d) return add_fractions(1, 1, d, n) def euler_057(): c = 0 n, d = (3, 2) for i in range(1000): if get_digit_count(n) > get_digit_count(d): c += 1 n, d = next_expension(n, d) return c if __name__ == "__main__": print("e057.py: " + str(euler_057())) assert(euler_057() == 153)