Even Fibonacci numbers
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.
找出400 0000以下斐波那契数中的偶数求和,累加即可(用递推公式,通项公式可能有舍入误差)
1 #include"stdio.h" 2 3 int main() /*problem2-Even Fibonacci numbers*/ 4 { 5 int a[100]={1,1,2},i; 6 double sum=0; 7 for(i=3;;i++) 8 { 9 a[i]=a[i-2]+a[i-1]; 10 if(a[i]>4000000.0||i==100) 11 break; 12 } 13 for(i=2;a[i]!=0;i+=3) 14 sum+=a[i]; 15 printf("sum is %lf ",sum); 16 return(0); 17 }