Even Fibonacci numbers
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.
http://projecteuler.net/problem=2
Brute Force
Patterns
Let’s take a look at the values of the sequence.
What we can see is that $F_3$, $F_6$, $F_9$, … $F_{(n\,multiple\,of\,3)}$ are the even fibonacci numbers.
Using this is a bit of information is going to take some manipulation. A fibonacci number can be generalized as:
Using to replace we get:
Using to replace we get:
Using to replace a single we get:
Going the other way, replacing with we get:
For example:
We can use this to solve our problem!
Better Brute Force
We are using a trick to set the new value of the variable, fb
. We want to set fb
to , but the variable we were using for , fa
has been set to . We can use our knowledge that and set fb
to (fa - fb) / 4
.