eBash

It is not the mountain we conquer but ourselves

Project Euler Problem 2

| Comments

Table of Contents

1 Problem

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, …

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

2 Solution

可以直接遍历Fibonacci数列,找出偶数相加,需要 \(O\) (n)。但我们观察如下数列:

1 1 2 3 5 8 13 21 34 55 89 144

易知每三个Fibonacci为偶数,F(n)和F(n-3),F(n-6)亦可推导:

F(n) = F(n-1) + F(n-2)
= F(n-2)+F(n-3)+F(n-2)=2 F(n-2) + F(n-3)
= 2(F(n-3)+F(n-4))+F(n-3))=3 F(n-3) + 2 F(n-4)
= 3 F(n-3) + F(n-4) + F(n-5) + F(n-6)
= 4 F(n-3) + F(n-6)

3 Answer

4613732

Source:C++

Comments