| Total Complexity | 4 |
| Total Lines | 49 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | """ |
||
| 2 | Project Euler Problem 2: Even Fibonacci Numbers |
||
| 3 | =============================================== |
||
| 4 | |||
| 5 | .. module:: solutions.problem2 |
||
| 6 | :synopsis: My solution to problem #2. |
||
| 7 | |||
| 8 | The source code for this problem can be |
||
| 9 | `found here <https://bitbucket.org/nekedome/project-euler/src/master/solutions/problem2.py>`_. |
||
| 10 | |||
| 11 | Problem Statement |
||
| 12 | ################# |
||
| 13 | |||
| 14 | Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with :math:`1` and |
||
|
|
|||
| 15 | :math:`2`, the first :math:`10` terms will be: |
||
| 16 | :math:`1, 2, 3, 5, 8, 13, 21, 34, 55, 89, \\dots` |
||
| 17 | |||
| 18 | By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the |
||
| 19 | even-valued terms. |
||
| 20 | |||
| 21 | Solution Discussion |
||
| 22 | ################### |
||
| 23 | |||
| 24 | Simply iterate over the Fibonacci numbers up to four million and sum all the even ones. |
||
| 25 | |||
| 26 | Solution Implementation |
||
| 27 | ####################### |
||
| 28 | |||
| 29 | .. literalinclude:: ../../solutions/problem2.py |
||
| 30 | :language: python |
||
| 31 | :lines: 34- |
||
| 32 | """ |
||
| 33 | |||
| 34 | from itertools import takewhile |
||
| 35 | |||
| 36 | from lib.numbertheory import is_even |
||
| 37 | from lib.sequence import Fibonaccis |
||
| 38 | |||
| 39 | |||
| 40 | def solve(): |
||
| 41 | """ Compute the answer to Project Euler's problem #2 """ |
||
| 42 | upper_bound = 4000000 |
||
| 43 | numbers = [fib_i for fib_i in takewhile(lambda x: x <= upper_bound, Fibonaccis()) if is_even(fib_i)] |
||
| 44 | answer = sum(numbers) |
||
| 45 | return answer |
||
| 46 | |||
| 47 | |||
| 48 | expected_answer = 4613732 |
||
| 49 |
This check looks for lines that are too long. You can specify the maximum line length.