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)\textrm{fib}\left(n\right) is only even if n%3=0n \% 3 = 0, else it is odd.

Think in binary or follow my proof if you do not believe me:

First step, fib(3n)\textrm{fib}\left(3 \cdot n\right) is always even:

Basis:

n=0 fib(0)=0 n = 0 \\\ \textrm{fib}\left(0\right) = 0

Assumption: Holds to nn, therefore fib(n)\textrm{fib}\left(n\right) is even

Induction: nn+1n \rightarrow n + 1

fib(3(n+1))=fib(3n+3) =fib(3n+2)+fib(3n+1) =2fib(2n+1)+fib(3n) \begin{align} \textrm{fib}\left(3\cdot \left(n+1\right) \right) &= \textrm{fib}\left(3\cdot n + 3\right) \\\ &= \textrm{fib}\left(3 \cdot n + 2\right) + \textrm{fib}\left(3 \cdot n + 1\right) \\\ &= 2 \cdot \textrm{fib}\left(2 \cdot n + 1 \right) + \textrm{fib}\left(3 \cdot n \right) \end{align}

2x,xN2 \cdot x, x \in N is always even, fib(3n)\textrm{fib}\left(3 \cdot n\right) is even and the sum of even numbers is even as well. Ergo fib(3n)\textrm{fib}\left(3 \cdot n\right) is always even!

But we still have to proof that fib(3n)\textrm{fib}\left(3 \cdot n\right) are the ONLY even numbers!

Lets take a look at fib(3n+2)\textrm{fib}\left(3 \cdot n + 2\right) which should be always odd:

Basis:

n=0 fib(2)=1 n = 0 \\\ \textrm{fib}\left(2\right) = 1

Assumption: Holds to nn, therefore fib(n+2)\textrm{fib}\left(n + 2\right) is odd

Induction: nn+1n \rightarrow n + 1

fib(3(n+1)+2)=fib(3n+5) =fib(3n+4)+fib(3(n+1)) =2fib(3(n+1))+fib(3n+2) \begin{align} \textrm{fib}\left(3 \cdot \left(n+1\right) + 2\right) &= \textrm{fib}\left(3 \cdot n + 5\right) \\\ &= \textrm{fib}\left(3n + 4\right) + \textrm{fib}\left(3 \cdot \left(n + 1\right)\right) \\\ &= 2 \cdot \textrm{fib}\left(3 \cdot \left(n + 1\right)\right) + \textrm{fib}\left(3 \cdot n + 2\right) \end{align}

A sum of a odd and an even number is always odd hence fib(3n+2)\textrm{fib}\left(3 \cdot n+2\right) is odd

What is left? fib(3n+1)\textrm{fib}\left(3 \cdot n + 1\right)!

Basis:

n=0 fib(1)=1 n = 0 \\\ \textrm{fib}\left(1\right) = 1

Assumption: Holds to nn, therefore fib(n+1)\textrm{fib}\left(n + 1\right) is odd

Induction: nn+1n \rightarrow n + 1

fib(3(n+1)+1)=fib(3n+4) =fib(3n+2)+fib(3(n+1))  \begin{align} \textrm{fib}\left(3 \cdot \left(n+1\right) + 1\right) &= \textrm{fib}\left(3 \cdot n + 4\right) \\\ &= \textrm{fib}\left(3 \cdot n + 2 \right) + \textrm{fib}\left(3 \cdot \left(n + 1\right)\right) \\\ \end{align}

And again, odd + even = odd!

So the only even numbers in the Fibonacci sequence are the ones with n%3=0n \% 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)

N0123456789 01000000000 10001000000 20000001000 30000000001 \begin{array}{c|ccccccccccccc} N & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 \\\ 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\\ 1 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\\ 2 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\\ 3 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\\ \end{array}

But these could also be written like this: N0123456789 00000000000 10110000000 20000110000 30000000110 \begin{array}{c|ccccccccccccc} N & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 \\\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\\ 1 & 0 & 1 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\\ 2 & 0 & 0 & 0 & 0 & 1 & 1 & 0 & 0 & 0 & 0 \\\ 3 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 0 \\\ \end{array}

This means that _i=0Nf_even=_i=0Nf_odd \sum\_{i=0}^N f\_{\textrm{even}} = \sum\_{i=0}^N f\_{\textrm{odd}} and therefore we can also write it as: _i=0Nf_even=12_i=0Nf_i \sum\_{i=0}^N f\_{\textrm{even}} = \frac{1}{2} \sum\_{i=0}^N f\_i

We now only need to know what the sum of all Fibonacci numbers to some n is: _i=0Nf_i=f_N+21 \sum\_{i=0}^N f\_i = f\_{N+2} - 1

Wherefore, the solution is: _i=0Nf_even=f_n+212with n chosen to be the biggest value fulfilling: f_n4000000 \sum\_{i=0}^N f\_{\textrm{even}} = \frac{f\_{n+2} - 1}{2} \\ \textrm{with n chosen to be the biggest value fulfilling: } f\_n \leq 4000000


Published

Category

projecteuler