All Euler problems
Project Euler

Even Fibonacci Numbers

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms. That is, compute S = sum_(F_n <= 4,000,000, 2 | F_n) F_n where F_1...

Source sync Apr 19, 2026
Problem #0002
Level Level 00
Solved By 823,769
Languages C++, Python
Answer 4613732
Length 764 words
sequencealgebramodular_arithmetic

Problem Statement

This archive keeps the full statement, math, and original media on the page.

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,\ldots \] By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

Problem 2: Even Fibonacci Numbers

Mathematical Development

Preliminaries

Definition 1 (Fibonacci Sequence). The Fibonacci sequence (Fn)n1(F_n)_{n \ge 1} is defined by F1=1F_1 = 1, F2=1F_2 = 1, and Fn=Fn1+Fn2F_n = F_{n-1} + F_{n-2} for n3n \ge 3.

Lemma 1 (Fibonacci Addition Identity). For all integers m2m \ge 2 and n1n \ge 1,

Fm+n=FmFn+1+Fm1Fn.F_{m+n} = F_m F_{n+1} + F_{m-1} F_n.

Proof. We fix m2m \ge 2 and proceed by induction on nn.

Base case. n=1n = 1: Fm+1=FmF2+Fm1F1=Fm+Fm1F_{m+1} = F_m F_2 + F_{m-1} F_1 = F_m + F_{m-1}, which is the defining recurrence. The identity holds.

Inductive step. Assume the identity holds for nn and for n1n - 1 (with n2n \ge 2). Then

Fm+n+1=Fm+n+Fm+n1=(FmFn+1+Fm1Fn)+(FmFn+Fm1Fn1)F_{m+n+1} = F_{m+n} + F_{m+n-1} = (F_m F_{n+1} + F_{m-1} F_n) + (F_m F_n + F_{m-1} F_{n-1}) =Fm(Fn+1+Fn)+Fm1(Fn+Fn1)=FmFn+2+Fm1Fn+1.= F_m(F_{n+1} + F_n) + F_{m-1}(F_n + F_{n-1}) = F_m F_{n+2} + F_{m-1} F_{n+1}.

This is the identity with nn replaced by n+1n+1, completing the induction. \square

Parity Periodicity

Theorem 1 (Pisano Period Modulo 2). FnF_n is even if and only if 3n3 \mid n.

Proof. The Fibonacci recurrence modulo 2, FnFn1+Fn2(mod2)F_n \equiv F_{n-1} + F_{n-2} \pmod{2}, depends only on the pair (Fn1mod2,Fn2mod2)(F_{n-1} \bmod 2, F_{n-2} \bmod 2). Since there are at most 22=42^2 = 4 distinct pairs, the sequence of pairs is eventually periodic. Computing explicitly:

nn1234567
FnF_n11235813
Fnmod2F_n \bmod 21101101

We observe that (F4mod2,F5mod2)=(1,1)=(F1mod2,F2mod2)(F_4 \bmod 2, F_5 \bmod 2) = (1, 1) = (F_1 \bmod 2, F_2 \bmod 2). Since the recurrence is determined by consecutive pairs, the parity sequence is periodic with period π(2)=3\pi(2) = 3. The pattern is (1,1,0,1,1,0,)(1, 1, 0, 1, 1, 0, \ldots), so 2Fn2 \mid F_n if and only if n0(mod3)n \equiv 0 \pmod{3}.

Uniqueness of the period. Since F1mod2=10=F3mod2F_1 \bmod 2 = 1 \ne 0 = F_3 \bmod 2, the period cannot be 1 or 2. Hence π(2)=3\pi(2) = 3 is the minimal period. \square

The Even-Indexed Fibonacci Subsequence

Definition 2. Let Ek=F3kE_k = F_{3k} for k1k \ge 1. By Theorem 1, (Ek)k1(E_k)_{k \ge 1} is the subsequence of all even Fibonacci numbers.

Theorem 2 (Even Fibonacci Recurrence). For all k3k \ge 3,

Ek=4Ek1+Ek2E_k = 4E_{k-1} + E_{k-2}

with initial conditions E1=2E_1 = 2 and E2=8E_2 = 8.

Proof. We must show F3k=4F3(k1)+F3(k2)F_{3k} = 4F_{3(k-1)} + F_{3(k-2)}, i.e., F3k=4F3k3+F3k6F_{3k} = 4F_{3k-3} + F_{3k-6}.

Apply Lemma 1 with m=3m = 3 and n=3k3n = 3k - 3:

F3k=F3F3k2+F2F3k3=2F3k2+F3k3.(1)F_{3k} = F_3 F_{3k-2} + F_2 F_{3k-3} = 2F_{3k-2} + F_{3k-3}. \tag{1}

Now express F3k2F_{3k-2} in terms of earlier values. Since F3k2=F3k3+F3k4F_{3k-2} = F_{3k-3} + F_{3k-4}:

F3k=2(F3k3+F3k4)+F3k3=3F3k3+2F3k4.(2)F_{3k} = 2(F_{3k-3} + F_{3k-4}) + F_{3k-3} = 3F_{3k-3} + 2F_{3k-4}. \tag{2}

Apply Lemma 1 with m=3m = 3 and n=3k6n = 3k - 6 (valid for k3k \ge 3):

F3k3=F3F3k5+F2F3k6=2F3k5+F3k6.(3)F_{3k-3} = F_3 F_{3k-5} + F_2 F_{3k-6} = 2F_{3k-5} + F_{3k-6}. \tag{3}

From (3): F3k5=(F3k3F3k6)/2F_{3k-5} = (F_{3k-3} - F_{3k-6})/2. Also, F3k4=F3k5+F3k6F_{3k-4} = F_{3k-5} + F_{3k-6}, so

F3k4=F3k3F3k62+F3k6=F3k3+F3k62.(4)F_{3k-4} = \frac{F_{3k-3} - F_{3k-6}}{2} + F_{3k-6} = \frac{F_{3k-3} + F_{3k-6}}{2}. \tag{4}

Substituting (4) into (2):

F3k=3F3k3+2F3k3+F3k62=3F3k3+F3k3+F3k6=4F3k3+F3k6.F_{3k} = 3F_{3k-3} + 2 \cdot \frac{F_{3k-3} + F_{3k-6}}{2} = 3F_{3k-3} + F_{3k-3} + F_{3k-6} = 4F_{3k-3} + F_{3k-6}.

For the initial conditions: E1=F3=2E_1 = F_3 = 2 and E2=F6=8E_2 = F_6 = 8.

Verification: E3=F9=34=48+2=4E2+E1E_3 = F_9 = 34 = 4 \cdot 8 + 2 = 4E_2 + E_1. \checkmark \square

Growth Rate and Termination

Theorem 3 (Closed Form and Growth Rate). The sequence (Ek)(E_k) satisfies

Ek=αkβk5E_k = \frac{\alpha^k - \beta^k}{\sqrt{5}}

where α=2+5\alpha = 2 + \sqrt{5} and β=25\beta = 2 - \sqrt{5} are the roots of the characteristic equation x24x1=0x^2 - 4x - 1 = 0.

Proof. The characteristic polynomial of Ek=4Ek1+Ek2E_k = 4E_{k-1} + E_{k-2} is x24x1=0x^2 - 4x - 1 = 0, with roots

α=4+16+42=2+5,β=4202=25.\alpha = \frac{4 + \sqrt{16 + 4}}{2} = 2 + \sqrt{5}, \qquad \beta = \frac{4 - \sqrt{20}}{2} = 2 - \sqrt{5}.

The general solution is Ek=c1αk+c2βkE_k = c_1 \alpha^k + c_2 \beta^k. Applying initial conditions:

E1=c1α+c2β=2,E2=c1α2+c2β2=8.E_1 = c_1 \alpha + c_2 \beta = 2, \qquad E_2 = c_1 \alpha^2 + c_2 \beta^2 = 8.

From E1E_1: c2=(2c1α)/βc_2 = (2 - c_1 \alpha)/\beta. Substituting into E2E_2 and using α+β=4\alpha + \beta = 4, αβ=1\alpha \beta = -1, αβ=25\alpha - \beta = 2\sqrt{5}:

Since α2=4α+1\alpha^2 = 4\alpha + 1 and β2=4β+1\beta^2 = 4\beta + 1 (as roots of x2=4x+1x^2 = 4x + 1):

E2=c1(4α+1)+c2(4β+1)=4(c1α+c2β)+(c1+c2)=42+(c1+c2)=8+(c1+c2).E_2 = c_1(4\alpha + 1) + c_2(4\beta + 1) = 4(c_1 \alpha + c_2 \beta) + (c_1 + c_2) = 4 \cdot 2 + (c_1 + c_2) = 8 + (c_1 + c_2).

So c1+c2=0c_1 + c_2 = 0, hence c2=c1c_2 = -c_1. Then E1=c1(αβ)=c125=2E_1 = c_1(\alpha - \beta) = c_1 \cdot 2\sqrt{5} = 2, giving c1=1/5c_1 = 1/\sqrt{5} and c2=1/5c_2 = -1/\sqrt{5}. \square

Corollary 1 (Exponential Growth). Ek=Θ(αk)E_k = \Theta(\alpha^k) where α=2+54.236\alpha = 2 + \sqrt{5} \approx 4.236. Equivalently, Ek=Θ(ϕ3k)E_k = \Theta(\phi^{3k}) where ϕ=(1+5)/2\phi = (1+\sqrt{5})/2 is the golden ratio.

Proof. Since β=250.236<1|\beta| = |2 - \sqrt{5}| \approx 0.236 < 1, we have βk0|\beta^k| \to 0 as kk \to \infty, so Ekαk/5E_k \sim \alpha^k / \sqrt{5}. The equivalence α=ϕ3\alpha = \phi^3 follows from direct computation: ϕ3=ϕϕ2=ϕ(ϕ+1)=ϕ2+ϕ=2ϕ+1=2+5=α\phi^3 = \phi \cdot \phi^2 = \phi(\phi + 1) = \phi^2 + \phi = 2\phi + 1 = 2 + \sqrt{5} = \alpha. \square

Corollary 2 (Number of Terms Below LL). The number of even Fibonacci terms not exceeding LL is logα(5L)+O(1)\lfloor \log_\alpha(\sqrt{5} \cdot L) \rfloor + O(1). For L=4×106L = 4 \times 10^6, this count is 11.

Summation Formula

Theorem 4 (Telescoping Sum for Even Fibonacci Numbers). Define Tk=j=1kEjT_k = \sum_{j=1}^{k} E_j. Then

Tk=Ek+1+Ek24.T_k = \frac{E_{k+1} + E_k - 2}{4}.

Proof. By strong induction on kk.

Base case. k=1k = 1: T1=E1=2T_1 = E_1 = 2. And (E2+E12)/4=(8+22)/4=8/4=2(E_2 + E_1 - 2)/4 = (8 + 2 - 2)/4 = 8/4 = 2. \checkmark

Inductive step. Assume Tk1=(Ek+Ek12)/4T_{k-1} = (E_k + E_{k-1} - 2)/4. Then

Tk=Tk1+Ek=Ek+Ek124+Ek=Ek+Ek12+4Ek4=5Ek+Ek124.T_k = T_{k-1} + E_k = \frac{E_k + E_{k-1} - 2}{4} + E_k = \frac{E_k + E_{k-1} - 2 + 4E_k}{4} = \frac{5E_k + E_{k-1} - 2}{4}.

We need to show this equals (Ek+1+Ek2)/4(E_{k+1} + E_k - 2)/4, i.e., 5Ek+Ek1=Ek+1+Ek5E_k + E_{k-1} = E_{k+1} + E_k, i.e., Ek+1=4Ek+Ek1E_{k+1} = 4E_k + E_{k-1}. This is precisely the recurrence of Theorem 2. \square

Numerical Evaluation

Proposition 1. The sum of all even Fibonacci numbers not exceeding 40000004\,000\,000 is 46137324\,613\,732.

Proof. Computing the even Fibonacci sequence:

kkEk=F3kE_k = F_{3k}Running sum
122
2810
33444
4144188
5610798
62,5843,382
710,94614,328
846,36860,696
9196,418257,114
10832,0401,089,154
113,524,5784,613,732
1214,930,352(exceeds limit)

Each EkE_k is verified via the recurrence Ek=4Ek1+Ek2E_k = 4E_{k-1} + E_{k-2}: e.g., E12=43524578+832040=14930352>4000000E_{12} = 4 \cdot 3\,524\,578 + 832\,040 = 14\,930\,352 > 4\,000\,000, so k=11k = 11 is the last term included.

Verification via Theorem 4: T11=(E12+E112)/4=(14930352+35245782)/4=18454928/4=4613732T_{11} = (E_{12} + E_{11} - 2)/4 = (14\,930\,352 + 3\,524\,578 - 2)/4 = 18\,454\,928/4 = 4\,613\,732. \checkmark \square

Editorial

We generate only the even Fibonacci terms, not the entire Fibonacci sequence. Starting from E1=2E_1 = 2 and E2=8E_2 = 8, we repeatedly use the recurrence Ek=4Ek1+Ek2E_k = 4E_{k-1} + E_{k-2}, add each term to a running total, and stop once the next even term exceeds the limit. This is sufficient because Theorem 1 shows that every third Fibonacci number is even, and Theorem 2 gives a recurrence that enumerates exactly those terms.

Theorem 5 (Algorithm Correctness). SumEvenFibonacci(L) returns {Ek:EkL}\sum \{E_k : E_k \le L\} for all L1L \ge 1.

Proof. We establish two loop invariants. Let ai,bia_i, b_i denote the values of a,ba, b at the start of iteration ii (with i=1i = 1 being the first iteration).

Invariant 1: ai=Eia_i = E_i and bi=Ei+1b_i = E_{i+1}.

This holds initially (a1=2=E1a_1 = 2 = E_1, b1=8=E2b_1 = 8 = E_2). After the update (a,b)(b,4b+a)(a, b) \leftarrow (b, 4b + a), we have ai+1=bi=Ei+1a_{i+1} = b_i = E_{i+1} and bi+1=4bi+ai=4Ei+1+Ei=Ei+2b_{i+1} = 4b_i + a_i = 4E_{i+1} + E_i = E_{i+2} by Theorem 2.

Invariant 2: At the start of iteration ii, total =j=1i1Ej= \sum_{j=1}^{i-1} E_j.

This holds initially (total = 0). After adding ai=Eia_i = E_i, total becomes j=1iEj\sum_{j=1}^{i} E_j.

The loop terminates when ai=Ei>La_i = E_i > L, at which point total =j=1i1Ej={Ek:EkL}= \sum_{j=1}^{i-1} E_j = \sum\{E_k : E_k \le L\}. Termination is guaranteed by Corollary 1 (EkE_k \to \infty). \square

Pseudocode

Algorithm: Sum of Even Fibonacci Terms
Require: A bound L ≥ 1.
Ensure: The sum T of the even Fibonacci terms not exceeding L.
1: Initialize e_prev ← 2, e_curr ← 8, and T ← 0.
2: While e_prev ≤ L do:
3:     Update T ← T + e_prev and (e_prev, e_curr) ← (e_curr, 4 · e_curr + e_prev).
4: Return T.

Complexity Analysis

Theorem 6. SumEvenFibonacci(L) runs in Θ(logL)\Theta(\log L) time and O(1)O(1) space.

Proof. Time: Each iteration performs O(1)O(1) arithmetic operations (one comparison, one addition, one multiplication by 4, one addition). The loop executes exactly KK iterations where K=max{k:EkL}K = \max\{k : E_k \le L\}. By Corollary 2, K=logα(5L)+O(1)=Θ(logL)K = \lfloor \log_\alpha(\sqrt{5} \cdot L) \rfloor + O(1) = \Theta(\log L) since α>1\alpha > 1 is constant. Hence total time is Θ(logL)\Theta(\log L).

Space: The algorithm maintains exactly three integer variables (a, b, total), which is O(1)O(1). \square

Answer

4613732\boxed{4613732}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_002/solution.cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
    long long a = 2, b = 8, s = 0;
    while (a <= 4000000) {
        s += a;
        long long c = 4 * b + a;
        a = b;
        b = c;
    }
    cout << s << endl;
    return 0;
}