Project Euler Problem 2 (C++和Python代码实现和解析)

100 篇文章 3 订阅
87 篇文章 1 订阅

Problem 2: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.

1. 第2个问题 : 偶数的斐波那契数

斐波那契序列的每一项都是它前两个项相加之和。从前两项1和2开始,前10个项是:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …

通过考虑斐波那契序列中值不超过400万的项,找出值为偶数的项的和。

2. 求解分析

从题目中,我们知道:斐波那契序列的计算公式是: Fn = Fn-1 + Fn-2 (n>=3),其中F1 =1, F2 = 2

知道了Fn的计算公式,我们判断每一个值是偶数的Fn项,并求和。

3. C++ 代码实现

我们定义了一个类PE0002,包含一个成员函数getSumOfEvenValuedTerms(),这个函数有一个输入参数max,返回值小于max的项的和。

C++ 代码

#include <iostream>
#include <cassert>

using namespace std;

class PE0002
{
public:
    int getSumOfEvenValuedTerms(int max);
};
    
int PE0002::getSumOfEvenValuedTerms(int max)
{
    int Fn_2 = 1;    // first term
    int Fn_1 = 2;    // second term
    int Fn;          // next term
    int sum = Fn_1;  // 2 is the first even-valued term

    while (Fn_2 + Fn_1 <= max)
    {
        Fn = Fn_2 + Fn_1;

        if (0 == Fn % 2)  // Fn is an even-valued term
        {
            sum += Fn;
        }

        Fn_2  = Fn_1;
        Fn_1  = Fn;
    }
    
    return sum;
}

int main()
{
    PE0002 pe0002;

    assert(44 == pe0002.getSumOfEvenValuedTerms(89));

    cout << "The sum of the even-valued terms in Fibonacci sequence " << endl; 
    cout << "whose values do not exceed four million is " ;
    cout << pe0002.getSumOfEvenValuedTerms(4000000) << "." << endl;

    return 0;
}

4. Python 代码实现

Python采用了和C++类似的方法,我们也定义了一个函数FibonacciEvenValuedTermsSum()。

Python 代码

def FibonacciEvenValuedTermsSum(max):
    Fn_2, Fn_1 = 1, 2
    sumOfTerms = 2
    while Fn_1 + Fn_2 <= max:
        Fn = Fn_1 + Fn_2
        if Fn % 2 == 0:
            sumOfTerms += Fn
        Fn_2, Fn_1 = Fn_1, Fn
    return sumOfTerms

def main():
    assert 44 == FibonacciEvenValuedTermsSum(89)
    print("The sum of the even-valued terms is", 
          FibonacciEvenValuedTermsSum(4000000))
    
if  __name__ == '__main__':
    main()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
欧拉方法和改进欧拉方法都是常见的数值解法之一,可以用来求解常微分方程初值问题。下面是Python实现欧拉方法和改进欧拉方法的代码欧拉方法: ```python def euler(f, x0, y0, h, n): x = [x0] y = [y0] for i in range(n): y.append(y[i] + h*f(x[i], y[i])) x.append(x[i] + h) return x, y ``` 其中,`f`是常微分方程右侧的函数,`x0`和`y0`是初值,`h`是步长,`n`是迭代次数。 改进欧拉方法: ```python def improved_euler(f, x0, y0, h, n): x = [x0] y = [y0] for i in range(n): k1 = h*f(x[i], y[i]) k2 = h*f(x[i]+h, y[i]+k1) y.append(y[i] + 0.5*(k1+k2)) x.append(x[i] + h) return x, y ``` 在改进欧拉方法中,我们首先计算出当前点的斜率k1,然后用k1来计算下一个点的近似值,得到k2。最终,我们将k1和k2的平均值作为斜率来更新y的值。 使用示例: ```python # 求解 y' = y+x, y(0)=1 def f(x, y): return y+x # 欧拉方法 x1, y1 = euler(f, 0, 1, 0.1, 10) print(x1) print(y1) # 改进欧拉方法 x2, y2 = improved_euler(f, 0, 1, 0.1, 10) print(x2) print(y2) ``` 输出结果: ``` 欧拉方法 [0, 0.1, 0.2, 0.30000000000000004, 0.4, 0.5, 0.6, 0.7, 0.7999999999999999, 0.8999999999999999, 0.9999999999999999] [1, 1.1, 1.21, 1.331, 1.4641000000000002, 1.61051, 1.771561, 1.9487171, 2.14358881, 2.357947691, 2.5937424601] 改进欧拉方法 [0, 0.1, 0.2, 0.30000000000000004, 0.4, 0.5, 0.6, 0.7, 0.7999999999999999, 0.8999999999999999, 0.9999999999999999] [1, 1.105, 1.233025, 1.38622150625, 1.5672100128906252, 1.778877619038672, 2.024485399686133, 2.307644815319196, 2.6324689879603275, 3.003497460080982, 3.4267290986342885] ``` 可以看到,使用欧拉方法和改进欧拉方法得到的数值解略有不同。在相同的步长下,改进欧拉法的结果更接近精确解。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值