The second problem is: By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
Lets again start with a python script solving this:
from itertools import takewhile, filterfalse
def fib():
lastfib = 1
fib = 0
tmp = 0
while True:
yield fib
tmp = fib
fib = lastfib + fib
lastfib = tmp
print(sum(filterfalse(lambda x: x % 2, takewhile(lambda x: x<4000000, fib()))))
Analytically, I have not gotten far. First thing I notice is that fib(n) is only even if n%3=0, else it is odd.
Think in binary or follow my proof if you do not believe me:
First step, fib(3⋅n) is always even:
Basis:
n=0 fib(0)=0
Assumption: Holds to n, therefore fib(n) is even
Induction: n→n+1
fib(3⋅(n+1)) =fib(3⋅n+3)=fib(3⋅n+2)+fib(3⋅n+1)=2⋅fib(2⋅n+1)+fib(3⋅n)
2⋅x,x∈N is always even, fib(3⋅n) is even and the sum of even numbers is even as well. Ergo fib(3⋅n) is always even!
But we still have to proof that fib(3⋅n) are the ONLY even numbers!
Lets take a look at fib(3⋅n+2) which should be always odd:
Basis:
n=0 fib(2)=1
Assumption: Holds to n, therefore fib(n+2) is odd
Induction: n→n+1
fib(3⋅(n+1)+2) =fib(3⋅n+5)=fib(3n+4)+fib(3⋅(n+1))=2⋅fib(3⋅(n+1))+fib(3⋅n+2)
A sum of a odd and an even number is always odd hence fib(3⋅n+2) is odd
What is left? fib(3⋅n+1)!
Basis:
n=0 fib(1)=1
Assumption: Holds to n, therefore fib(n+1) is odd
Induction: n→n+1
fib(3⋅(n+1)+1) =fib(3⋅n+4)=fib(3⋅n+2)+fib(3⋅(n+1))
And again, odd + even = odd!
So the only even numbers in the Fibonacci sequence are the ones with n%3=0!
But that's it, no idea how to battle this problem further.
Update
Well, after some thinking, I guess I found an analytic solution.
Lets have a look which Fibonacci numbers are in the sum of all even Fibonacci numbers: (remember, every third is even)
N 0 1 2 3 01000100002000030100400005000060010700008000090001
But these could also be written like this: N 0 1 2 3 00000101002010030000400105001060000700018000190000
This means that ∑_i=0Nf_even=∑_i=0Nf_odd and therefore we can also write it as: ∑_i=0Nf_even=21∑_i=0Nf_i
We now only need to know what the sum of all Fibonacci numbers to some n is: ∑_i=0Nf_i=f_N+2−1
Wherefore, the solution is: ∑_i=0Nf_even=2f_n+2−1with n chosen to be the biggest value fulfilling: f_n≤4000000