Question

The recursive definition of a Fibonacci Number is F(n) = F(n - 1) + F(n -...

The recursive definition of a Fibonacci Number is F(n) = F(n - 1) + F(n - 2), where F(0) = 1 and F(1) = 1. What is the value of Fib(3)?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

We have F (n) = F (n - 1) + F (n - 2), where F (0) = 1 and F (1) = 1

Now, we need to find Fib (3) as per the above, F (3) can be written as

   F (3) = F (3 – 1) + F (3 – 2)

  • F (2) + F (1)
  • F (2) + 1 (since F (1) is 1)
  • F (2 – 1) + F (2 – 2) + 1
  • F (1) + F (0) + 1
  • 1 + 0 + 1 (since F (0) is 0)
  • 2

Hence Fib (3) = 2

Program to test:

#include<iostream>
using namespace std;

int RecursiveFib(int);

int main()
{
   int n, i = 0, count;
   int fib=0;

    //read number of terms to be printed in fibonacci series
    cout<<"\nEnter number: ";
    cin>>n;
   
    //call the recursive fibonacci function
    for ( count = 1 ; count <= n ; count++ )
    {
        fib=fib+RecursiveFib(i);
    //cout<<fib<<" ";
    i++;
}
cout<<"\nFib("<<n<<") = "<<fib;

return 0;
}

//this is function that prints fibonacci series using recursive method
int RecursiveFib(int n)
{
   //here n is the number of terms
   //if n is 0 return 0 and if n is 1 return 1
if (n == 0)
return 0;
else if ( n == 1 )
return 1;
else
//else call the function recursively by passing n-1 and n-2 as parameters
return ( RecursiveFib(n-1) + RecursiveFib(n-2) );
}

Output:

C.Users Vicky Desktop\DevC++Programs Fibsum.exe Enter number: 3 Process exited after 1.339 seconds with return value 0 Press

Add a comment
Know the answer?
Add Answer to:
The recursive definition of a Fibonacci Number is F(n) = F(n - 1) + F(n -...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program in MIPs Assembly Language to compute nth number of a fibonacci number sequence....

    Write a program in MIPs Assembly Language to compute nth number of a fibonacci number sequence. Your program should prompt for an integer input n from the user. The program should call a recursive function to compute the nth fibonacci number. Your program must follow programming convention. You should submit program and screenshot of output in a single word/pdf file. You should use following recursive definition of fibonacci function: fib(0) = 0 fib(1) = 1 fib(n) = fib(n-1) +fib(n-2)

  • Consider Fibonacci number F(N), where N is a positive integer, defined as follows. F(1) = 1...

    Consider Fibonacci number F(N), where N is a positive integer, defined as follows. F(1) = 1 F(2) = 1 F(N) = F(N-1) + F(N-2) for N > 2 a) Write a recursive function that computes Fibonacci number for a given integer N≥ 1. b) Prove the following theorem using induction: F(N) < ΦN for integer N≥ 1, where Φ = (1+√5)/2.

  • in C++ 6. (20)The Fibonacci sequence is the series of integers 0, 1, 1,2, 3, 5,...

    in C++ 6. (20)The Fibonacci sequence is the series of integers 0, 1, 1,2, 3, 5, 8, 13, 21, 34, 55, 89.. 1 See the pattern? Each element in the series is the sum of the preceding two items. There is a recursive formula for calculating the nth number of the sequence (the oth number if Fib(0)-0): 8 Fib(N)-/N, if N 0 or 1 ifN> 1 Fib(N-2) Fib(N-1), a. b. c. Write a recursive version of the function Fibonacci. Write...

  • 1.Take this recursive Fibonacci implementation and convert it into the caching based version discussed in class....

    1.Take this recursive Fibonacci implementation and convert it into the caching based version discussed in class. Implement your caching to store a maximum of 5 values. Create 2 variations: one that stores all values and one that only stores even values. Make sure that you don't leave any gaps in your cache — if you have 5 cache entries you must cache 5 unique and valid values. Compare the caching implementations to the recursive implementation using the time utility. How...

  • The ­following Implementation of the Fibonacci function is a correct, but inefficient, def fibonacci(n): if n...

    The ­following Implementation of the Fibonacci function is a correct, but inefficient, def fibonacci(n): if n <= 2: return 1 else: return fib(n - 1) + fib(n - 2) In more details, the code shown runs very slowly for even relatively small values of n; it can take minutes or hours to compute even the 40th or 50th Fibonacci number. The code is inefficient because it makes too many recursive calls. It ends up recomputing each Fibonacci number many times....

  • use Java please. The Fibonacci Sequence Given the initial Fibonacci numbers 0 and 1, we can...

    use Java please. The Fibonacci Sequence Given the initial Fibonacci numbers 0 and 1, we can generate the next number by adding the two previous Fibonacci numbers together. For this sequence, you will be asked to take an input, denoting how many Fibonacci numbers you want to generate. Call this input upperFibLimit. The longest Fib sequence you should generate is 40 and the shortest you should generate is 1. So,1<upperFibLimit<40 The rule is simple given f(0) 0, f(1) 1 ....

  • Fibonacci Sequence The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5,...

    Fibonacci Sequence The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... The next number is found by adding up the two numbers before it. The 2 is found by adding the two numbers before it (1+1) The 3 is found by adding the two numbers before it (1+2), And the 5 is (2+3), and so on!         Example: the next number in the sequence above is 21+34 = 55 Source:...

  • Let f:N + N be defined by the recursive definition: Base case: f(0) = 7 Recursive...

    Let f:N + N be defined by the recursive definition: Base case: f(0) = 7 Recursive step: 3nf(n-1) and g:N + N be defined by the recursive definition: Base case: g(0) = 1 Recursive setep: g(n) = 8 * g(n-1) +4n Find a closed-form definition for fog(n)

  • In Haskell: Write a recursive function fibonacci that computes the n-th Fibonacci number. fibonacci :: Int...

    In Haskell: Write a recursive function fibonacci that computes the n-th Fibonacci number. fibonacci :: Int -> Int Write a recursive function myProduct that multiplies all the numbers in a list. myProduct :: [Integer] -> Integer Using the technique of List Comprehension write a function that would remove even numbers from a list of lists. For Example: given input: [[1,2,3,4], [6,3,45,8], [4,9,23,8]] expected output: [[1,3], [3,45],[9,23]]. Show how the library function replicate :: Int -> a-> [a] that produces a...

  • 3. The sequence (Fn) of Fibonacci numbers is defined by the recursive relation Fn+2 Fn+1+ F...

    3. The sequence (Fn) of Fibonacci numbers is defined by the recursive relation Fn+2 Fn+1+ F for all n E N and with Fi = F2= 1. to find a recursive relation for the sequence of ratios (a) Use the recursive relation for (F) Fn+ Fn an Hint: Divide by Fn+1 N (b) Show by induction that an 1 for all n (c) Given that the limit l = lim,0 an exists (so you do not need to prove that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT