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 |
||
0 ignored issues
–
show
|
|||
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 |
||
0 ignored issues
–
show
|
|||
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)] |
||
0 ignored issues
–
show
|
|||
44 | answer = sum(numbers) |
||
45 | return answer |
||
46 | |||
47 | |||
48 | expected_answer = 4613732 |
||
0 ignored issues
–
show
The name
expected_answer does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$ ).
This check looks for invalid names for a range of different identifiers. You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements. If your project includes a Pylint configuration file, the settings contained in that file take precedence. To find out more about Pylint, please refer to their site. ![]() |
|||
49 |
This check looks for lines that are too long. You can specify the maximum line length.