您的位置:首页 > 其它

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

2015-10-03 13:07 441 查看

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 二分求出 满足条件的最大值 n

由公式2 构造等比数列 {$${a_{n} + \frac{2}{3}}} 为等比数列

得到$${a_{n} = \frac{26}{3} * 4^{n-1} - \frac{2}{3}}$$

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

... cnblogs的markdown一直乱版...是我的姿势不对吗
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: