We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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://)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #57: Square root convergents
You are viewing a single comment's thread. Return to all comments →
Paythan
[def continued_fraction_expansion_sqrt2(N): numerators = [3] denominators = [2] 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://)