MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / solution

Function solution

project_euler/problem_065/sol1.py:74–95  ·  view source on GitHub ↗

Returns the sum of the digits in the numerator of the max-th convergent of the continued fraction for e. >>> solution(9) 13 >>> solution(10) 17 >>> solution(50) 91

(max_n: int = 100)

Source from the content-addressed store, hash-verified

72
73
74def solution(max_n: int = 100) -> int:
75 """
76 Returns the sum of the digits in the numerator of the max-th convergent of
77 the continued fraction for e.
78
79 >>> solution(9)
80 13
81 >>> solution(10)
82 17
83 >>> solution(50)
84 91
85 """
86 pre_numerator = 1
87 cur_numerator = 2
88
89 for i in range(2, max_n + 1):
90 temp = pre_numerator
91 e_cont = 2 * i // 3 if i % 3 == 0 else 1
92 pre_numerator = cur_numerator
93 cur_numerator = e_cont * pre_numerator + temp
94
95 return sum_digits(cur_numerator)
96
97
98if __name__ == "__main__":

Callers 1

sol1.pyFile · 0.70

Calls 1

sum_digitsFunction · 0.85

Tested by

no test coverage detected