france

https://github.com/francecil

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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}{2n} } ) $$
$$ 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  阅读(433)  评论(0编辑  收藏  举报