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)
| 72 | |
| 73 | |
| 74 | def 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 | |
| 98 | if __name__ == "__main__": |