Solution to Project Euler Problem 57: Square root convergents - It is possible to show that the square root of two can be expressed as an infinite continued fraction. √2 = 1 + (1/(2 + 1/(2 + 1/(2 + ...)))). By expanding this for the first four iterations, we get: 1 + 1/2 = 3/2 = 1.5, 1 + 1/(1 + 1/2) = 7/5 = 1.4, 1 + (1/(2 + 1/(2 + 1/2))) = 1.41666... 1 + (1/(2 + 1/(2 + 1/(2 + 1/2)))) = 1.41379... The next three expansions are 99/70, 239/269, 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?


Updated: Jan. 28, 2021 — Training Time: 2 minutes
Overseen by: Archangel Macsika
All Training Resources

Scroll for more menu list

Topic: Project Euler Problem 57: Square root convergents.

Difficulty: Easy.

Objective: It is possible to show that the square root of two can be expressed as an infinite continued fraction.
√2 = 1 + (1/(2 + 1/(2 + 1/(2 + ...))))
By expanding this for the first four iterations, we get:
1 + 1/2 = 3/2 = 1.5
1 + 1/(1 + 1/2) = 7/5 = 1.4
1 + (1/(2 + 1/(2 + 1/2))) = 1.41666...
1 + (1/(2 + 1/(2 + 1/(2 + 1/2)))) = 1.41379... The next three expansions are 99/70, 239/269, 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?

Input: None.

Expected Output: 153.

Sikademy Solution in Java Programming Language

Sikademy Solution in Python Programming Language

Was this training resource helpful?