This is our solution and implementation to problem #57 on Project Euler.
Our code is written in TypeScript, a language which is built on-top of JavaScript and transpiles to it. We've included the problem statement, our code (which is commented for greater clarity), our video which outlines our analysis and implementation approach, and the solution + how long it took to calculate it.
Note: the code and contents here might be slightly different than what is in the video. We've made some improvements to some of the code since recording.
If you would like to view the original problem and solve it, please visit: Square Root Convergents on Project Euler. If you're having trouble solving this problem, or are just curious to see how others have solved it, feel free to take a look, but please put solid effort into solving this before viewing the actual solution to the problem.
Problem Statement
It is possible to show that the square root of two can be expressed as an infinite continued fraction.
$$\sqrt 2 =1+ \frac 1 {2+ \frac 1 {2 +\frac 1 {2+ \dots}}}$$
By expanding this for the first four iterations, we get:
$$1 + \frac 1 2 = \frac 32 = 1.5$$
$$1 + \frac 1 {2 + \frac 1 2} = \frac 7 5 = 1.4$$
$$1 + \frac 1 {2 + \frac 1 {2+\frac 1 2}} = \frac {17}{12} = 1.41666 \dots$$
$$1 + \frac 1 {2 + \frac 1 {2+\frac 1 {2+\frac 1 2}}} = \frac {41}{29} = 1.41379 \dots$$
The next three expansions are $$\frac {99}{70}$$ $$\frac {239}{169}$$ and $$\frac {577}{408}$$ but the eighth expansion, $$\frac {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?
Our Solution
Our solution is given in the TypeScript files below. This solution uses more than one code file. Some solutions use utilities which were created and enhanced while working on this and previous Project Euler problems. Some code in the utilities files might not be used in this particular problem.
Results
This implementation found the solution in 814ms.
If you would like to view the answer, click below to reveal. Please consider reviewing the implementation and trying to code your own solution before viewing the answer.
View Answer
The answer is 153.
All of our solutions are hosted on GitHub. The code on this page was pulled from the repo and the solution and execution time were calculated based on that code.