Problem 2
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:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …

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

The problem here is twofold, namely:

1. To find all the terms in the Fibonacci sequence that are even AND less than four million, and,
2. Find the sum of all those terms.

Figuring out the Fibonacci sequence is relatively easy for a computer (as it’s just an iterated sum, and sums are P problems). However, you don’t want to just generate a zillion numbers and sum them blindly. Heed these words, even more if you’re just learning code: stating the problem correctly is half the battle. In this particular case, once you know what you should do in theory, your code should follow it. Namely, you should:

1. Figure out the terms of the Fibonacci sequence,
2. Take those that are even AND less than four million; and
3. Add these together, print the result.

An elegant algorithm doesn’t come out of nowhere, it’s the result of an elegant problem/solution statement. The lesson? Learn to state the problem and its solution in the simplest terms you can.

← Project Euler Guide: Problem 1 | Project Euler Guide: Problem 2 | Project Euler Guide: Problem 3 →

Log in or register to write something here or to contact authors.