Problem 2 : Even Fibonacci numbers

原创 2018年04月16日 16:45:50

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开始生成的斐波那契数列前10项为:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …

考虑该斐波那契数列中不超过四百万的项,求其中为偶数的项之和。

题目解答
    朴素解法:暴力枚举(遍历所有斐波那契额数,判断到偶数时加到计数器中即可)
      优化算法:我们先将所有斐波那契额数列出
                11, 2, 3, 5, 8, 13, 21, 34, 55, 89, …
    我们发现每个偶数之前的下标距离是相同的,斐波那契额数列递归式如下:
                
       既然一个数字与前两个数字相关,前两个数字又和各自的前两个数字相关,那么我们可以推断出一个数字一定和之前出现过的数字相关,我们可以推导出以下等式:
 
                   
题目代码  
#include <stdio.h>
#include <inttypes.h>
#define N 4e6
int32_t main() {
    int32_t num[3] = {0};
    num[0] = 2;
    num[1] = 8;
    int64_t sum = 10;
    for(int i=2;;i++){
        num[i%3] = num[(i+1)%3] + 4*num[(i+2)%3];
        if(num[i%3]>N)
            break;
        sum += num[i%3];
    }
    printf("%ld\n",sum);
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Ruger008/article/details/79962317

Problem 2 Even Fibonacci numbers (fibonacci 数学规律)

Even Fibonacci numbers Problem 2 Each new term in the Fibonacci sequence is generated by a...
  • liangzhaoyang1
  • liangzhaoyang1
  • 2016-10-25 19:23:55
  • 331

Geeks 面试题:Fibonacci numbers 优化为lgn效率

通过这个矩阵相乘的方法,得到的时间复杂度依然是O(n),但是可以进一步使用二分法,把复杂度降低到O(lgn)。...
  • kenden23
  • kenden23
  • 2014-03-10 08:42:36
  • 1936

Fibonacci numbers

Program for Fibonacci numbers The Fibonacci numbers are the numbers in the following integer se...
  • screaming
  • screaming
  • 2016-04-06 21:12:06
  • 347

Fibonacci Number Established the Environment of Python

Ⅰ.Some basic Python Language Syntax1.List>>> a=[1,2,3,4] >>> print a [1, 2, 3, 4] >>> a[1]="Don have...
  • PrincessKenny
  • PrincessKenny
  • 2015-06-15 14:45:19
  • 460

acm hnu 10072 Fibonacci Number

Fibonacci Number Time Limit: 1000ms, Special Tim...
  • manio
  • manio
  • 2006-10-25 12:21:00
  • 1212

Fibonacci Numbers

Fibonacci Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other...
  • fastkeeper
  • fastkeeper
  • 2015-02-15 23:02:30
  • 353

HDU 1021 Fibonacci Again

Problem DescriptionThere are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) +...
  • ChinaCzy
  • ChinaCzy
  • 2009-11-26 22:13:00
  • 1828

(Problem 2)Even Fibonacci numbers

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting w...
  • u010993034
  • u010993034
  • 2013-07-26 10:57:25
  • 368

Problem 2 Even Fibonacci numbers

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting w...
  • junxinsiwo
  • junxinsiwo
  • 2017-07-13 18:23:14
  • 87

Problem 2:Even Fibonacci numbers

原文地址:http://projecteuler.net/problem=2 Even Fibonacci numbers Problem 2 Each new term in the F...
  • cfeibiao
  • cfeibiao
  • 2013-08-01 23:31:18
  • 641
收藏助手
不良信息举报
您举报文章:Problem 2 : Even Fibonacci numbers
举报原因:
原因补充:

(最多只允许输入30个字)