呼吁第n个斐波纳契数数 [英] Number of calls for nth Fibonacci number

查看:130
本文介绍了呼吁第n个斐波纳契数数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下code片断:

Consider the following code snippet:

int fib(int N)
{
   if(N<2) return 1;
   return (fib(N-1) + fib(N-2));
}

由于 FIB 从主被称为以N为10,35,67,......(说),一共有多少电话 都是为了 FIB

Given that fib is called from main with N as 10,35,67,... (say), how many total calls are made to fib?

有没有针对此问题的任何关系?

Is there any relation for this problem?

PS:这是一个理论问题和不应该被执行

PS: This is a theoretical question and not supposed to be executed.

编辑:

我所知道的其他方法来斐波那契数列的速度更快的计算。

I am aware of other methods for the faster computation of Fibonacci series.

我想计算的时候FIB数量的解决方案被调用FIB(40),FIB(50),...没有编译器的帮助和考试情况,你都应该回答40问题与此类似的在规定的时间(约30薄荷糖)。

I want a solution for computing number of times fib is invoked for fib(40),fib(50) ,.. without the aid of compiler and in exam condition where you are supposed to answer 40 question similar to this one in a stipulated of time ( about 30 mints).

谢谢

推荐答案

F(N)的是做计算 FIB(N)的呼叫数量

  • 如果的 N'LT; 2 的那么的 F(N)= 1
  • 另外, F(N)= 1 + F(N - 1)+ F(N - 2)。
  • If n < 2 then f(n) = 1.
  • Otherwise, f(n) = 1 + f(n - 1) + f(n - 2).

因此​​, F 的是至少的 O(FIB(N))的。事实上, F(N)的是 2 * FIB(N) - 1 的。我们表明,该通过感应:

So, f is at least O(fib(n)). In fact, f(n) is 2 * fib(n) - 1. We show this by induction:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆