Even Fibonacci numbers


Problem 2


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.



Answer:

4613732


题解:1 1 2 3 5 8 13 21 34 55 89 144 ...规律

推导一下就可以了....

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) 

代码:


#include<bits/stdc++.h>
using namespace std;
const int ex=4e6;
int main()
{
int f[300];int sum=10;
f[1]=2;
f[2]=8;
for(int i=3;;i++)
{
f[i]=4*f[i-1]+f[i-2];
if(f[i]<ex) sum+=f[i];
else break;
}
cout<<sum<<endl;
return 0;
}