zoukankan      html  css  js  c++  java
  • 欧拉计划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}{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一直乱版...是我的姿势不对吗

  • 相关阅读:
    正则表达式
    文件读写
    列表和数组变量
    Perl基础
    [转] bias和variance
    Chapter 1
    Come back to CNblogs
    SentenceGeneration
    句子生成器
    ant相关操作
  • 原文地址:https://www.cnblogs.com/france/p/4853245.html
Copyright © 2011-2022 走看看