Project Euler #57: Square root convergents

  • + 0 comments

    Paythan

    [def continued_fraction_expansion_sqrt2(N): numerators = [3] denominators = [2] iterations = []

    for i in range(1, N):
        numerator = numerators[-1] + 2 * denominators[-1]
        denominator = numerators[-1] + denominators[-1]
        numerators.append(numerator)
        denominators.append(denominator)
    
        # Check if the numerator has more digits than the denominator
        if len(str(numerator)) > len(str(denominator)):
            iterations.append(i + 1)  # Add 1 to make it 1-based index
    
    return iterations
    

    Input

    N = int(input())

    Get the iterations where the numerator has more digits than the denominator

    result = continued_fraction_expansion_sqrt2(N)

    Output the result

    for iteration in result: print(iteration)](https://)