Returns the sum of all even fibonacci sequence elements that are lower or equal to n. >>> solution(10) 10 >>> solution(15) 10 >>> solution(2) 2 >>> solution(1) 0 >>> solution(34) 44
(n: int = 4000000)
| 17 | |
| 18 | |
| 19 | def solution(n: int = 4000000) -> int: |
| 20 | """ |
| 21 | Returns the sum of all even fibonacci sequence elements that are lower |
| 22 | or equal to n. |
| 23 | |
| 24 | >>> solution(10) |
| 25 | 10 |
| 26 | >>> solution(15) |
| 27 | 10 |
| 28 | >>> solution(2) |
| 29 | 2 |
| 30 | >>> solution(1) |
| 31 | 0 |
| 32 | >>> solution(34) |
| 33 | 44 |
| 34 | """ |
| 35 | |
| 36 | i = 1 |
| 37 | j = 2 |
| 38 | total = 0 |
| 39 | while j <= n: |
| 40 | if j % 2 == 0: |
| 41 | total += j |
| 42 | i, j = j, i + j |
| 43 | |
| 44 | return total |
| 45 | |
| 46 | |
| 47 | if __name__ == "__main__": |