首页 > 代码库 > Even Fibonacci numbers

Even Fibonacci numbers

problem 2:Even Fibonacci numbers

题意:求斐波那契数列不大于4百万的值为偶数的和

代码如下:

 1 #ifndef PRO2_H_INCLUDED
 2 #define PRO2_H_INCLUDED
 3 #include<iostream>
 4 using namespace std;
 5 
 6 namespace pro2{
 7     int solve(){
 8         int p1=1,p2=2,p,sum=0;
 9         for(;p1<=4000000;){
10             if(p1%2==0)sum+=p1;
11             p=p1+p2;
12             p1=p2;
13             p2=p;
14         }
15         return sum;
16     }
17 }
18 
19 #endif // PRO2_H_INCLUDED

 

Even Fibonacci numbers