← Previous | ↑ All problems | Go to Project Euler | Go to GitHub | Next →

Project Euler Problem 2

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, \dots$$

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

The naive approach

We want to sum all the even Fibonacci numbers up to some limit.

We'll use a slightly different convention from Project Euler. The recurrence relation for the $n^\text{th}$ Fibonacci number is $F_n = F_{n-1} + F_{n-2}$ with $F_0 = 0$ and $F_1 = 1$, the convention OEIS A000045 uses, so the first 13 terms are

$$0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, \dots$$

We could generate all the Fibonacci numbers up to that limit and just sum the even ones:

function sum_even_fibonacci_naive(limit)
    result = 0

    a, b = 0, 1
    while a ≤ limit
        iseven(a) && (result += a)
        a, b = b, a + b
    end

    return result
end

Benchmarking sum_even_fibonacci_naive(4 * 10^6) we get 17.662 ns, which is already fast enough for the problem. But there's a cooler method!

A recurrence for the even Fibonacci numbers

Notice that $F_3 = 2$, $F_6 = 8$, $F_9 = 34$, and $F_{12} = 144$. So every third number is even, and all the others are odd. This is because $F_0 = 0$ is even and $F_1 = 1$ is odd, so $F_2 = F_1 + F_0$ will be odd because $\text{even} + \text{odd} = \text{odd}$ by parity. Then $F_3 = F_2 + F_1$ will be even because $\text{odd} + \text{odd} = \text{even}$. This continues and so every third Fibonacci will be even, and all others are odd.

Now that we know every third Fibonacci number is even, can we find a recurrence relation for just the even Fibonacci numbers? So we want to compute $F_n$ using $F_{n-3}$, $F_{n-6}$, $F_{n-9}$, etc.

We can start by writing out

$$\begin{align} F_{n+3} &= F_{n+2} + F_{n+1} \\ &= (F_{n+1} + F_n) + F_{n+1} \\ &= 2F_{n+1} + F_n \end{align}$$

and using this we can obtain a recurrence relation for $F_{n+6}$

$$\begin{align} F_{n+6} &= 2F_{n+4} + F_{n+3} \\ &= 2(2F_{n+2} + F_{n+1}) + (2F_{n+1} + F_n) \\ &= 4(F_{n+2} + F_{n+1}) + F_n \\ &= 4F_{n+3} + F_n \end{align}$$

So knowing that $F_3 = 2$ and $F_6 = 8$ we can use the recurrence relation

$$F_n = 4F_{n-3} + F_{n-6}$$

to generate all the even Fibonacci numbers directly, skipping over all the odd ones!

The even Fibonacci numbers $0, 2, 8, 34, 144, 610, 2584, \dots$ are OEIS A014445, and the entry lists this same recurrence $a_n = 4a_{n-1} + a_{n-2}$ along with the generating function $2x / (1 - 4x - x^2)$. The sums of the even Fibonacci numbers, $2, 10, 44, 188, \dots$, are OEIS A099919.

We can implement this in Julia:

function sum_even_fibonacci(limit)
    limit < 2 && return 0
    limit < 8 && return 2

    a, b = 2, 8
    result = a + b

    while (c = 4b + a) ≤ limit
        result += c
        a, b = b, c
    end

    return result
end

Benchmarking sum_even_fibonacci(4 * 10^6) we get 4.537 ns, which is 3.89× faster than generating every Fibonacci number. There are three times fewer terms to generate, and no parity check on each one. Since the Fibonacci numbers grow exponentially, we can increase the limit to a much larger number and still compute the sum quickly. For example, benchmarking sum_even_fibonacci(4 * 10^15) only takes 7.880 ns, which is 4.84× faster than the naive approach.

This solution takes $\mathcal{O}(\log L)$ time because Fibonacci numbers grow exponentially, so only about $\log L$ terms are needed to reach the limit $L$.

Generalizing to every $k^\text{th}$ Fibonacci number

We got lucky twice here. Every third Fibonacci number is even, and those even Fibonacci numbers happen to satisfy a two-term recurrence of their own. Is that second bit of luck special to $3$, or does every $k^\text{th}$ Fibonacci number satisfy a recurrence like this?

The first bit of luck isn't luck at all. The even Fibonacci numbers are $F_3, F_6, F_9, \dots$ and they're even because they are multiples of $F_3 = 2$, which is an instance of a general divisibility property: whenever $m$ divides $n$, $F_m$ divides $F_n$. This follows from the stronger identity $\gcd(F_m, F_n) = F_{\gcd(m, n)}$. So every fourth Fibonacci number is a multiple of $F_4 = 3$, every fifth is a multiple of $F_5 = 5$, every sixth is a multiple of $F_6 = 8$, and so on. "Even" is just the $k = 3$ case of "divisible by $F_k$".

Now for the recurrence. We could grind through the same substitutions we did for $F_{n+6}$, but for a general $k$ it's much less painful to start from Binet's formula

$$F_n = \frac{\varphi^n - \psi^n}{\sqrt{5}}$$

where $\varphi = \frac{1 + \sqrt{5}}{2}$ is the golden ratio and $\psi = \frac{1 - \sqrt{5}}{2}$, the two roots of $x^2 - x - 1 = 0$. The only property of these two numbers we'll need is that their product is

$$\varphi\psi = \frac{(1 + \sqrt{5})(1 - \sqrt{5})}{4} = \frac{1 - 5}{4} = -1$$

so $\psi = -1/\varphi$ and $\varphi = -1/\psi$. Now we want a formula involving $F_{n-k}$ and $F_{n+k}$ so taking $k$ steps backwards or forwards means that

$$\begin{align} \varphi^{n-k} &= \varphi^n \varphi^{-k} = (-1)^k \varphi^n \psi^k \\ \psi^{n-k} &= \psi^n \psi^{-k} = (-1)^k \psi^n \varphi^k \\ \varphi^{n+k} &= \varphi^n \varphi^k \\ \psi^{n+k} &= \psi^n \psi^k \end{align}$$

Putting the first two into Binet's formula we get

$$F_{n-k} = \frac{\varphi^{n-k} - \psi^{n-k}}{\sqrt{5}} = \frac{(-1)^k \varphi^n \psi^k - (-1)^k \psi^n \varphi^k}{\sqrt{5}}$$

which we can rearrange to get

$$(-1)^k F_{n-k} = \frac{\varphi^n \psi^k - \varphi^k \psi^n}{\sqrt{5}}$$

Similarly, the other two give $F_{n+k}$, and this time there's no sign to move around

$$F_{n+k} = \frac{\varphi^{n+k} - \psi^{n+k}}{\sqrt{5}} = \frac{\varphi^n \varphi^k - \psi^n \psi^k}{\sqrt{5}}$$

Now adding the two of them, the $\varphi^n$ and $\psi^n$ terms group together

$$\begin{align} F_{n+k} + (-1)^k F_{n-k} &= \frac{\varphi^n \varphi^k - \psi^n \psi^k}{\sqrt{5}} + \frac{\varphi^n \psi^k - \varphi^k \psi^n}{\sqrt{5}} \\ &= \frac{\varphi^n (\varphi^k + \psi^k) - \psi^n (\psi^k + \varphi^k)}{\sqrt{5}} \\ &= (\varphi^k + \psi^k) \frac{\varphi^n - \psi^n}{\sqrt{5}} \\ &= (\varphi^k + \psi^k) F_n \end{align}$$

That leftover factor of $\varphi^k + \psi^k$ looks like it might be irrational, but it never is! Since $\varphi$ and $\psi$ are both roots of $x^2 - x - 1 = 0$, multiplying that through by $x^{k-2}$ gives $\varphi^k = \varphi^{k-1} + \varphi^{k-2}$ and the same for $\psi$, so the sums $\varphi^k + \psi^k$ obey the exact same recurrence the Fibonacci numbers do. They just start off somewhere else, at $\varphi^0 + \psi^0 = 2$ and $\varphi + \psi = 1$. These are the Lucas numbers $L_k$ (OEIS A000032)

$$2, 1, 3, 4, 7, 11, 18, 29, 47, 76, \dots$$

so what we've derived is that every $k^\text{th}$ Fibonacci number does satisfy a two-term recurrence

$$F_{n+k} = L_k F_n - (-1)^k F_{n-k}$$

with the Lucas number $L_k$ as the coefficient and a sign that flips:

$k$ $F_k$ $L_k$ recurrence
$1$ $1$ $1$ $F_{n+1} = F_n + F_{n-1}$
$2$ $1$ $3$ $F_{n+2} = 3F_n - F_{n-2}$
$3$ $2$ $4$ $F_{n+3} = 4F_n + F_{n-3}$
$4$ $3$ $7$ $F_{n+4} = 7F_n - F_{n-4}$
$5$ $5$ $11$ $F_{n+5} = 11F_n + F_{n-5}$
$6$ $8$ $18$ $F_{n+6} = 18F_n - F_{n-6}$

The $k = 1$ row is just the Fibonacci recurrence we started with, and the $k = 3$ row is the $F_n = 4F_{n-3} + F_{n-6}$ we derived by hand, shifted along by three indices.

Summing every $k^\text{th}$ Fibonacci number

Since the subsequence we're summing has its own two-term recurrence, let's give it its own name. Let's write $U_j = F_{kj}$ so that $U_0 = 0$ and $U_1 = F_k$, and the recurrence above becomes

$$U_{j+1} = L_k U_j - (-1)^k U_{j-1}$$

We want to compute $\displaystyle S_m = \sum_{j=1}^m U_j$ so let's sum the recurrence itself from $j = 1$ to $m$ to get

$$\sum_{j=1}^m U_{j+1} = L_k \sum_{j=1}^m U_j - (-1)^k \sum_{j=1}^m U_{j-1}$$

We can rewrite each sum here as

$$\begin{align} \sum_{j=1}^m U_{j+1} &= S_m + U_{m+1} - U_1 \\ \sum_{j=1}^m U_j &= S_m \\ \sum_{j=1}^m U_{j-1} &= S_m - U_m \end{align}$$

Substituting these in we get

$$S_m + U_{m+1} - U_1 = L_k S_m - (-1)^k (S_m - U_m)$$

which allows us to solve for $S_m$ to get

$$S_m = \frac{U_{m+1} - (-1)^k U_m - U_1}{L_k - (-1)^k - 1}$$

We can now write a closed form for the sum of every $k^\text{th}$ Fibonacci number

$$\sum_{j=1}^m F_{kj} = \frac{F_{k(m+1)} - (-1)^k F_{km} - F_k}{L_k - (-1)^k - 1}$$

This is a classical result (Koshy, 2001, Corollary 5.6, p. 86), and the denominators $L_k - (-1)^k - 1 = 1, 1, 4, 5, 11, 16, \dots$ are the associated Mersenne numbers (OEIS A001350). For $k = 1$ we have $L_1 = 1$ and recover the classic identity for the sum of all the Fibonacci numbers

$$\sum_{j=1}^m F_j = F_{m+1} + F_m - F_1 = F_{m+2} - 1$$

And for $k = 3$ we have $L_3 = 4$ giving a closed form for the sum we actually care about

$$\sum_{j=1}^m F_{3j} = \frac{F_{3m+3} + F_{3m} - 2}{4}$$

The largest even Fibonacci number not exceeding four million is $F_{33}$ and the next one is $F_{36}$, so the answer is

$$\frac{F_{36} + F_{33} - 2}{4}$$

It's not like we needed a closed-form solution to solve the problem in a reasonable amount of time, but it's cool that we can derive something like this!

While writing this post I made a few small contributions to the OEIS. I submitted a short proof there that the sums of every $k^\text{th}$ Fibonacci number satisfy a recurrence of their own, using the same trick of summing a recurrence that we used above. I also submitted the closed form for these sums to A001350, since its terms are the denominators.

References

Koshy, Thomas (2001). Fibonacci and Lucas numbers with applications. Wiley. doi