The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+15 votes
883 views

Consider the following recursive definition of $fib$:

 fib(n) :=  if n = 0 then 1
            else if n = 1 then 1
            else fib(n-1) + fib(n-2)

The number of times $fib$  is called (including the first call) for evaluation of $fib(7)$ is___________.

asked in Programming by Veteran (59.6k points)
recategorized by | 883 views

6 Answers

+30 votes
Best answer
The recurrence relation for the no. of calls is $$T(n) = T(n-1) + T(n-2) + 2$$

$T(0) =T(1) = 0$ (for $fib(0)$ and $fib(1)$, there are no recursive calls).

$T(2) = 2$

$T(3) = 4$

$T(4) = 8$

$T(5) = 14$

$T(6) = 24$

$T(7) = 40$.

Counting the initial call we get $40 + 1 = 41$.
answered by Veteran (359k points)
edited by
+2
ok...just confuse between value and call ...

now clear thank you sir ! :)
+1
sir why this extra 2 is include ???

because it have been called twice before
0
sir why we are adding 1, can you please explain.
+2
@Arjun sir,

why have you taken fib(0) and fib(1) as 0,although there are no recursive calls,but there will be one call fib(0) for 0 and fib(1) for 1,the initial function call which should be included.

I think it should be:-

T(n)= T(n-1)+T(n-2) +1 // the 1 is initial function call.

T(0)=T(1)=1, as base condition ,although answer reamins same.

Please check once
0
clear now
+20 votes
fib(0)=1 (because fib(0) is also a function call)

fib(1)=1 (because fib(1) is also a function call)

fib(2)= fib(0) + fib(1) +1= 3 ( 1 is added because fib(2) is also included in function call)

fib(3)= fib(2)+fib(1)+1= 5

fib(4)= fib(2)+fib(3)+1 = 9

fib(5)= fib(4)+fib(3)+1 = 15

fib(6)= fib(5)+fib(4)+1 = 25

fib(7)= fib(5)+fib(6)+1 = 41

HENCE 41 CALLS
answered by Boss (14.6k points)
edited by
0
thank you solving i'm also also solving on this way.
0
nice sushmita mam
+3 votes
Answer: 41

Make the recursion tree.
answered by Boss (34k points)
+3 votes

Answer: 41.

Correct recurrence relation is  -->

f(n) = 1 + f(n - 1) + f(n - 2).

For more details please refer https://stackoverflow.com/questions/1738923/number-of-calls-for-nth-fibonacci-number

Although f(7) is small value. So value could be obtained via direct substitution.

But if value is large then non - homogenous recurrence relation solution could be used.

$f(n) = \frac{1}{2^{(n+1)} * \sqrt{5}} * ( (1- \sqrt{5})^{n+1}- (1+ \sqrt{5})^{n+1} ) + 2$

answered by Boss (11.2k points)
edited by
+2 votes
Formula for no.of calls in f(n) =2f(n)-1 when f(0)=1

only applied  fibonacci series

where f(0)=1

f(1)=1

f(2)=2  i,e (f(0)+f(1))

f(3)=3............f(7)=21

so no.of calls 2(21)-1=41

remember when f(0)=0,f(1)=1 then formula changes as 2f(n+1)-1
answered by Junior (591 points)
+2
how no of call is 2f(n)-1 ??
+1 vote
#include <stdio.h>
int fib(int n);
int count =0 ;
int main()
{
  fib(7);
  printf("%d" , count);
  return 0;
}

int fib(int n)
{   count++;
    if (n==0) return 1;
    else if (n==1) return 1;
    else return (fib(n-1) + fib(n-2));
    
}
answered by Active (2.1k points)


Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

40,880 questions
47,536 answers
146,091 comments
62,300 users