欧拉计划第2题:Even Fibonacci numbers(斐波那契数列)

文章讨论了如何计算斐波那契数列中值不超过四百万的项中偶数项的总和,通过编程实现,展示了如何利用递推公式和条件判断来解决这个问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

翻译:斐波那契数列中的每一项都是前两项的和。由 111222 开始生成的斐波那契数列的前 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.”

“欧拉计划的存在,是为了每个对数学感兴趣的人,鼓励他们,挑战他们,并最终培养他们的能力与乐趣。”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值