Problem 2:Even Fibonacci numbers
标签:斐波那契数列
原文:Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 111 and 222, the first 101010 terms will be:
1,2,3,5,8,13,21,34,55,89,…1, 2, 3, 5, 8, 13, 21, 34, 55, 89, \ldots1,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.
翻译:斐波那契数列中的每一项都是前两项的和。由 111 和 222 开始生成的斐波那契数列的前 101010 项为:
1,2,3,5,8,13,21,34,55,89,…1, 2, 3, 5, 8, 13, 21, 34, 55, 89, \ldots1,2,3,5,8,13,21,34,55,89,…
考虑该斐波那契数列中不超过四百万的项,求其中为偶数的项之和。
题解:简单打表 可以得到第 333333 个斐波那契数列就超过四百万了,求解一下斐波那契数列,把前 323232 项中偶数值的相加一下求和。
代码:
#include <bits/stdc++.h>
using namespace std;
int f[50];
int main() {
int sum = 0;
f[0] = f[1] = 1;
for (int i = 1; i <= 40; i++) {
f[i] = f[i - 1] + f[i - 2];
if (f[i] > 4000000) break;
if (f[i] % 2 == 0) sum += f[i];
}
cout << sum << endl;
return 0;
}
“Project Euler exists to encourage, challenge, and develop the skills and enjoyment of anyone with an interest in the fascinating world of mathematics.”
“欧拉计划的存在,是为了每个对数学感兴趣的人,鼓励他们,挑战他们,并最终培养他们的能力与乐趣。”