Created With

linkProblem 2 - Even Fibonacci numbers

linkQuestion

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

linkSolution

euler_002.py
1linkdef generate_fibonacci_sequence(term_limit, first_term, second_term):

2link sequence = [first_term, second_term]

3link while True:

4link next_number = sequence[-1] + sequence[-2]

5link if next_number < term_limit:

6link sequence.append(next_number)

7link else:

8link break

9link

10link return sequence

11link

12linkfour_million_sequence = generate_fibonacci_sequence(4000000, 1, 2)

13linkprint(sum([n for n in four_million_sequence if n % 2 == 0])) # // @see [List Comprehension](https://www.programiz.com/python-programming/list-comprehension)

linkDiscussion

linkAnswer

Show Answerchevron_right

4613732

Problem 2 - Even Fibonacci numbersQuestionSolutionDiscussionAnswer

Home

Project Eulerchevron_right
Essayschevron_right

Awesome Stuff