Skip to content

Commit fa750c9

Browse files
committed
Add solution for Project Euler 65, Fixes: TheAlgorithms#2695
1 parent 6541236 commit fa750c9

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

Diff for: DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,8 @@
652652
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_56/sol1.py)
653653
* Problem 63
654654
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_63/sol1.py)
655+
* Problem 65
656+
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_65/sol1.py)
655657
* Problem 67
656658
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_67/sol1.py)
657659
* Problem 76

Diff for: project_euler/problem_65/__init__.py

Whitespace-only changes.

Diff for: project_euler/problem_65/sol1.py

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
"""
2+
The square root of 2 can be written as an infinite continued fraction.
3+
4+
sqrt(2) = 1 + 1 / (2 + 1 / (2 + 1 / (2 + 1 / (2 + ...))))
5+
6+
The infinite continued fraction can be written, sqrt(2) = [1;(2)], (2)
7+
indicates that 2 repeats ad infinitum. In a similar way, sqrt(23) =
8+
[4;(1,3,1,8)].
9+
10+
It turns out that the sequence of partial values of continued
11+
fractions for square roots provide the best rational approximations.
12+
Let us consider the convergents for sqrt(2).
13+
14+
1 + 1 / 2 = 3/2
15+
1 + 1 / (2 + 1 / 2) = 7/5
16+
1 + 1 / (2 + 1 / (2 + 1 / 2)) = 17/12
17+
1 + 1 / (2 + 1 / (2 + 1 / (2 + 1 / 2))) = 41/29
18+
19+
Hence the sequence of the first ten convergents for sqrt(2) are:
20+
1, 3/2, 7/5, 17/12, 41/29, 99/70, 239/169, 577/408, 1393/985, 3363/2378, ...
21+
22+
What is most surprising is that the important mathematical constant,
23+
e = [2;1,2,1,1,4,1,1,6,1,...,1,2k,1,...].
24+
25+
The first ten terms in the sequence of convergents for e are:
26+
2, 3, 8/3, 11/4, 19/7, 87/32, 106/39, 193/71, 1264/465, 1457/536, ...
27+
28+
The sum of digits in the numerator of the 10th convergent is
29+
1 + 4 + 5 + 7 = 17.
30+
31+
Find the sum of the digits in the numerator of the 100th convergent
32+
of the continued fraction for e.
33+
"""
34+
35+
36+
def solution(max: int) -> int:
37+
"""
38+
The solution mostly comes down to finding an equation that will generate
39+
the numerator of the continued fraction. For the i-th numerator, the
40+
pattern is:
41+
42+
n_i = m_i * n_(i-1) + n_(i-2)
43+
44+
for m_i = the i-th index of the continued fraction representation of e,
45+
n_0 = 1, and n_1 = 2.
46+
47+
For example:
48+
n_9 = 6 * 193 + 106 = 1264
49+
1 + 2 + 6 + 4 = 13
50+
51+
n_10 = 1 * 193 + 1264 = 1457
52+
1 + 4 + 5 + 7 = 17
53+
54+
>>> solution(9)
55+
13
56+
>>> solution(10)
57+
17
58+
"""
59+
n0 = 1
60+
n1 = 2
61+
62+
for i in range(2, max + 1):
63+
temp = n0
64+
m = 2 * i // 3 if i % 3 == 0 else 1
65+
n0 = n1
66+
n1 = m * n0 + temp
67+
68+
return sum_digits(n1)
69+
70+
71+
def sum_digits(num: int) -> int:
72+
"""
73+
Adds all the single digits of an int together.
74+
75+
>>> sum_digits(1)
76+
1
77+
>>> sum_digits(12345)
78+
15
79+
>>> sum_digits(999001)
80+
28
81+
"""
82+
digit_sum = 0
83+
while num > 0:
84+
digit_sum += num % 10
85+
num //= 10
86+
return digit_sum
87+
88+
89+
if __name__ == "__main__":
90+
print(solution(100))

0 commit comments

Comments
 (0)