Chinaunix首页 | 论坛 | 博客
  • 博客访问: 244908
  • 博文数量: 47
  • 博客积分: 1229
  • 博客等级: 中尉
  • 技术积分: 568
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-20 10:06
文章分类

全部博文(47)

文章存档

2014年(1)

2013年(7)

2012年(1)

2011年(38)

分类: C/C++

2011-03-02 23:16:11


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.


  1. #include <stdio.h>

  2. #define NUM    4000000
  3. int main(int argc, const char *argv[])
  4. {
  5.     int fibonacci, prev, pprev;
  6.     int sum;

  7.     pprev = 1, prev = 2, fibonacci = 0;
  8.     sum = 2;
  9.     while (fibonacci< NUM) {
  10.         if (!(fibonacci % 2)) {
  11.             sum += fibonacci;
  12.         }
  13.         fibonacci = prev + pprev;
  14.         pprev = prev;
  15.         prev = fibonacci;
  16.         printf("fibonacci: %d\n", fibonacci);
  17.     }

  18.     printf("sum = %d\n", sum);

  19.     return 0;
  20. }
阅读(1231) | 评论(0) | 转发(0) |
0

上一篇:euler1

下一篇:euler3

给主人留下些什么吧!~~