欧拉计划002. 斐波那契数列中的偶数

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.

斐波那契数列中的每一项被定义为前两项之和。要求找出该数列中值为不超过 4 百万的偶数的项之和。

网友题解:发现34是48+2 144=434+2 ···
解:Prelude> sum $ takeWhile (<=4000000) $ map fst $ iterate (\(a,b)->(b,a+4*b)) (2,8)

证明

有没有一种可以得到前n偶数项和的通项公式呢

斐波那契数列的通项公式推导

综合上面两个规律:

有两个公式:

$$ a_n =\displaystyle \frac {\sqrt{5}}{5} * ({\frac {(1+\sqrt{5} )^n - (1-\sqrt{5})^n}{2^n} } ) $$
$$ a_n = 4a_{n-3}+2 $$ -> 转化为 $$ a_n = 4a_{n-1}+2 $$ 对于数列 8 34 144 ....

解法如下:

  1. 由公式1 二分求出 满足条件的最大值 n
  2. 由公式2 构造等比数列 {$${a_{n} + \frac{2}{3}}} 为等比数列
  3. 得到$${a_{n} = \frac{26}{3} * 4^{n-1} - \frac{2}{3}}$$

代码如下(时间复杂度 logN):

... cnblogs的markdown一直乱版...是我的姿势不对吗

posted on 2015-10-03 13:07 france 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/france/p/4853245.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值