Skip to content
Prev Previous commit
Move solution() below helper function, rename variables
  • Loading branch information
peteryao7 committed Oct 16, 2020
commit 27d0614fd9f5e3332dc3ea25c5ebb5c610a7f7c9
51 changes: 25 additions & 26 deletions project_euler/problem_065/sol1.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""
Project Euler 65
https://projecteuler.net/problem=65
Project Euler Problem 65: https://projecteuler.net/problem=65

The square root of 2 can be written as an infinite continued fraction.

Expand Down Expand Up @@ -54,30 +53,6 @@
"""


def solution(max: int = 100) -> int:
"""
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
"""
n0 = 1
n1 = 2

for i in range(2, max + 1):
temp = n0
m = 2 * i // 3 if i % 3 == 0 else 1
n0 = n1
n1 = m * n0 + temp

return sum_digits(n1)


def sum_digits(num: int) -> int:
"""
Returns the sum of every digit in num.
Expand All @@ -96,5 +71,29 @@ def sum_digits(num: int) -> int:
return digit_sum


def solution(max: int = 100) -> int:
"""
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
"""
pre_numerator = 1
cur_numerator = 2

for i in range(2, max + 1):
temp = pre_numerator
e_cont = 2 * i // 3 if i % 3 == 0 else 1
pre_numerator = cur_numerator
cur_numerator = e_cont * pre_numerator + temp

return sum_digits(cur_numerator)


if __name__ == "__main__":
print(f"{solution() = }")