现在的位置: 首页 > 综合 > 正文

欧拉计划 第2题

2013年07月29日 ⁄ 综合 ⁄ 共 363字 ⁄ 字号 评论关闭

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.

 

int a = 1, b = 2;
long result = 2;
while (result < 4000000)
{
int c = a + b;
a
= b;
b
= c;
if (c % 2 == 0) result += c;
}
Console.WriteLine(result);

 

抱歉!评论已关闭.