This is our solution and implementation to problem #65 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: Convergents of e 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
The square root of 2 can be written as an infinite continued fraction.
$$\sqrt{2} = 1 + \dfrac{1}{2 + \dfrac{1}{2 + \dfrac{1}{2 + \dfrac{1}{2 + ...}}}}$$
The infinite continued fraction can be written, $$\sqrt{2} = [1; (2)]$$ $$(2)$$ indicates that 2 repeats ad infinitum. In a similar way, $$\sqrt{23} = [4; (1, 3, 1, 8)]$$
It turns out that the sequence of partial values of continued fractions for square roots provide the best rational approximations. Let us consider the convergents for $$\sqrt{2}$$
$$\begin{align} &1 + \dfrac{1}{2} = \dfrac{3}{2} \\ &1 + \dfrac{1}{2 + \dfrac{1}{2}} = \dfrac{7}{5}\\ &1 + \dfrac{1}{2 + \dfrac{1}{2 + \dfrac{1}{2}}} = \dfrac{17}{12}\\ &1 + \dfrac{1}{2 + \dfrac{1}{2 + \dfrac{1}{2 + \dfrac{1}{2}}}} = \dfrac{41}{29} \end{align}$$
Hence the sequence of the first ten convergents for $$\sqrt{2}$$ are:
$$1, \dfrac{3}{2}, \dfrac{7}{5}, \dfrac{17}{12}, \dfrac{41}{29}, \dfrac{99}{70}, \dfrac{239}{169}, \dfrac{577}{408}, \dfrac{1393}{985}, \dfrac{3363}{2378}, ...$$
What is most surprising is that the important mathematical constant,
$$e = [2; 1, 2, 1, 1, 4, 1, 1, 6, 1, ... , 1, 2k, 1, ...]$$
The first ten terms in the sequence of convergents for e are:
$$2, 3, \dfrac{8}{3}, \dfrac{11}{4}, \dfrac{19}{7}, \dfrac{87}{32}, \dfrac{106}{39}, \dfrac{193}{71}, \dfrac{1264}{465}, \dfrac{1457}{536}, ...$$
The sum of digits in the numerator of the 10th convergent is $$1 + 4 + 5 + 7 = 17$$
Find the sum of digits in the numerator of the 100th convergent of the continued fraction for $$e$$
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 3ms.
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 272.
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.