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以下斐波那契数中的偶数求和,累加即可(用递推公式,通项公式可能有舍入误差)
#include"stdio.h" int main() /*problem2-Even Fibonacci numbers*/
{
int a[]={,,},i;
double sum=;
for(i=;;i++)
{
a[i]=a[i-]+a[i-];
if(a[i]>4000000.0||i==)
break;
}
for(i=;a[i]!=;i+=)
sum+=a[i];
printf("sum is %lf\n",sum);
return();
}