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 syncApr 19, 2026
Problem#0002
LevelLevel 00
Solved By823,769
LanguagesC++, Python
Answer4613732
Length764 words
sequencealgebramodular_arithmetic
Problem statement
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)n≥1 is defined by F1=1, F2=1, and Fn=Fn−1+Fn−2 for n≥3.
Lemma 1 (Fibonacci Addition Identity). For all integers m≥2 and n≥1,
Fm+n=FmFn+1+Fm−1Fn.
Proof. We fix m≥2 and proceed by induction on n.
Base case.n=1: Fm+1=FmF2+Fm−1F1=Fm+Fm−1, which is the defining recurrence. The identity holds.
Inductive step. Assume the identity holds for n and for n−1 (with n≥2). Then
This is the identity with n replaced by n+1, completing the induction. □
Parity Periodicity
Theorem 1 (Pisano Period Modulo 2).Fn is even if and only if 3∣n.
Proof. The Fibonacci recurrence modulo 2, Fn≡Fn−1+Fn−2(mod2), depends only on the pair (Fn−1mod2,Fn−2mod2). Since there are at most 22=4 distinct pairs, the sequence of pairs is eventually periodic. Computing explicitly:
n
1
2
3
4
5
6
7
Fn
1
1
2
3
5
8
13
Fnmod2
1
1
0
1
1
0
1
We observe that (F4mod2,F5mod2)=(1,1)=(F1mod2,F2mod2). Since the recurrence is determined by consecutive pairs, the parity sequence is periodic with period π(2)=3. The pattern is (1,1,0,1,1,0,…), so 2∣Fn if and only if n≡0(mod3).
Uniqueness of the period. Since F1mod2=1=0=F3mod2, the period cannot be 1 or 2. Hence π(2)=3 is the minimal period. □
The Even-Indexed Fibonacci Subsequence
Definition 2. Let Ek=F3k for k≥1. By Theorem 1, (Ek)k≥1 is the subsequence of all even Fibonacci numbers.
Theorem 2 (Even Fibonacci Recurrence). For all k≥3,
Ek=4Ek−1+Ek−2
with initial conditions E1=2 and E2=8.
Proof. We must show F3k=4F3(k−1)+F3(k−2), i.e., F3k=4F3k−3+F3k−6.
Apply Lemma 1 with m=3 and n=3k−3:
F3k=F3F3k−2+F2F3k−3=2F3k−2+F3k−3.(1)
Now express F3k−2 in terms of earlier values. Since F3k−2=F3k−3+F3k−4:
F3k=2(F3k−3+F3k−4)+F3k−3=3F3k−3+2F3k−4.(2)
Apply Lemma 1 with m=3 and n=3k−6 (valid for k≥3):
F3k−3=F3F3k−5+F2F3k−6=2F3k−5+F3k−6.(3)
From (3): F3k−5=(F3k−3−F3k−6)/2. Also, F3k−4=F3k−5+F3k−6, so
So c1+c2=0, hence c2=−c1. Then E1=c1(α−β)=c1⋅25=2, giving c1=1/5 and c2=−1/5. □
Corollary 1 (Exponential Growth).Ek=Θ(αk) where α=2+5≈4.236. Equivalently, Ek=Θ(ϕ3k) where ϕ=(1+5)/2 is the golden ratio.
Proof. Since ∣β∣=∣2−5∣≈0.236<1, we have ∣βk∣→0 as k→∞, so Ek∼αk/5. The equivalence α=ϕ3 follows from direct computation: ϕ3=ϕ⋅ϕ2=ϕ(ϕ+1)=ϕ2+ϕ=2ϕ+1=2+5=α. □
Corollary 2 (Number of Terms Below L). The number of even Fibonacci terms not exceeding L is ⌊logα(5⋅L)⌋+O(1). For L=4×106, this count is 11.
Summation Formula
Theorem 4 (Telescoping Sum for Even Fibonacci Numbers). Define Tk=∑j=1kEj. Then
Tk=4Ek+1+Ek−2.
Proof. By strong induction on k.
Base case.k=1: T1=E1=2. And (E2+E1−2)/4=(8+2−2)/4=8/4=2. ✓
Inductive step. Assume Tk−1=(Ek+Ek−1−2)/4. Then
We need to show this equals (Ek+1+Ek−2)/4, i.e., 5Ek+Ek−1=Ek+1+Ek, i.e., Ek+1=4Ek+Ek−1. This is precisely the recurrence of Theorem 2. □
Numerical Evaluation
Proposition 1. The sum of all even Fibonacci numbers not exceeding 4000000 is 4613732.
Proof. Computing the even Fibonacci sequence:
k
Ek=F3k
Running sum
1
2
2
2
8
10
3
34
44
4
144
188
5
610
798
6
2,584
3,382
7
10,946
14,328
8
46,368
60,696
9
196,418
257,114
10
832,040
1,089,154
11
3,524,578
4,613,732
12
14,930,352
(exceeds limit)
Each Ek is verified via the recurrence Ek=4Ek−1+Ek−2: e.g., E12=4⋅3524578+832040=14930352>4000000, so k=11 is the last term included.
Verification via Theorem 4:T11=(E12+E11−2)/4=(14930352+3524578−2)/4=18454928/4=4613732. ✓□
Editorial
We generate only the even Fibonacci terms, not the entire Fibonacci sequence. Starting from E1=2 and E2=8, we repeatedly use the recurrence Ek=4Ek−1+Ek−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:Ek≤L} for all L≥1.
Proof. We establish two loop invariants. Let ai,bi denote the values of a,b at the start of iteration i (with i=1 being the first iteration).
Invariant 1:ai=Ei and bi=Ei+1.
This holds initially (a1=2=E1, b1=8=E2). After the update (a,b)←(b,4b+a), we have ai+1=bi=Ei+1 and bi+1=4bi+ai=4Ei+1+Ei=Ei+2 by Theorem 2.
Invariant 2: At the start of iteration i, total=∑j=1i−1Ej.
This holds initially (total = 0). After adding ai=Ei, total becomes ∑j=1iEj.
The loop terminates when ai=Ei>L, at which point total =∑j=1i−1Ej=∑{Ek:Ek≤L}. Termination is guaranteed by Corollary 1 (Ek→∞). □
Pseudocode
Algorithm: Sum of Even Fibonacci TermsRequire: 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) time and O(1) space.
Proof.Time: Each iteration performs O(1) arithmetic operations (one comparison, one addition, one multiplication by 4, one addition). The loop executes exactly K iterations where K=max{k:Ek≤L}. By Corollary 2, K=⌊logα(5⋅L)⌋+O(1)=Θ(logL) since α>1 is constant. Hence total time is Θ(logL).
Space: The algorithm maintains exactly three integer variables (a, b, total), which is O(1). □
Answer
4613732
Source code
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;
}
Pythonproject_euler/problem_002/solution.py
"""Problem 2: Even Fibonacci Numbers"""
def solve(limit: int = 4_000_000) -> int:
a, b, total = 2, 8, 0
while a <= limit:
total += a
a, b = b, 4 * b + a
return total
answer = solve()
assert answer == 4613732
print(answer)