- F(n) = F(n-1) + F(n-2)
- F(n) = F(n-1) / F(n-2)
- F(n) = F(n-1) * F(n-2)
Explanation:
- The correct answer is option "b". The numbers that make up the Fibonacci sequence are all the sum of the two numbers that came before it. Each new number in the series is determined by adding the two numbers that came before it, starting with 0 and 1. This is the formal definition of the Fibonacci sequence: F(0) = 0 F(1) = 1 F(n) = F(n-1) + F(n-2) for n ≥ 2
- For the recursive Fibonacci function, which of the following is a base case?
- F(1) = 1
- F(0) = 0
- Both a and b
- None of the above
Explanation:
- The correct answer is option "c". Base cases are required in the recursive definition of the Fibonacci sequence to terminate the recursion and provide beginning values. The base cases are:
- F(0) = 0 denotes the Fibonacci number zero.
- The definition of the first Fibonacci number is F(1) = 1.
- The recursive function would not have a point for terminating the recursion without any of these base cases.
- What is the naïve recursive Fibonacci algorithm's time complexity?
- O(2^n)
- O(log n)
- O(n^2)
- O(n)
Explanation:
- The correct answer is option "c". For any value of n greater than 1, the function calls itself twice in the naïve recursive method, resulting in an exponential increase in the number of calls. As a result, several overlapping subproblems need to be solved repeatedly.
- For example, F(n)=F(n−1) + F (n−2) is the naïve recursive computation for F(n).
- As a result, a binary tree of recursive calls is produced, with n increasing the number of calls exponentially. This recursion tree has a height of n and an O(2^n) time complexity since every level of the tree doubles the number of calls.
- What is the main disadvantage of generating Fibonacci numbers using a naïve recursive approach?
- Code readability
- Lack of modularity
- High time complexity
- High space complexity
Explanation:
- The appropriate choice is option "c". When Fibonacci numbers are generated through the naïve recursive technique, it exhibits a high time complexity. This is due to the excessive redundant computations required. The function is called exponentially many times due to the numerous calculations for each Fibonacci number. Especially for larger values of n, this approach is remarkably ineffective because of its O(2^n) time complexity.
- In the process of calculating fib(5) using the naïve recursive method, the function fib(2) is called multiple times.
Explanation:
- The accurate choice is option "a". To calculate the frequency of fib(2) being invoked within fib(5).
fib(5) calls fib(4) and fib(3)
fib(4) calls fib(3) and fib(2)
fib(3) calls fib(2) and fib(1)
Count the calls to fib(2).
Calculating fib(5) results in a sequence of fib(4) -> fib(3) -> fib(2) in the initial function call.
fib(4) directly calls fib(2) (2nd call)
fib(3) directly calls fib(2) (3rd call)
With the na?ve recursive method, fib(2) is therefore called three times to calculate fib(5).
- When generating Fibonacci numbers, which of the following is a benefit of the iterative method over the recursive approach?
- More function calls
- More intuitive
- Simplicity
- Less memory usage
Explanation:
- The accurate response is choice "d". In contrast to the basic recursive approach, the iterative technique for producing Fibonacci numbers offers several advantages: It necessitates only a small number of variables for storing prior and current Fibonacci numbers, and it operates with constant space, or O(1).
- What is the total of the initial ten Fibonacci numbers?
Explanation:
- The solution is option "b". Each number in the Fibonacci series is generated by adding the two preceding numbers, typically commencing with 0 and 1. The sequence begins with 0; 1, 1, 2, 3, 5, 8, 13, 21, 34,...
- To calculate the sum of the initial ten Fibonacci numbers, use the values F0=0, F1=1, F2=1, F3=2, F4=3, F5=5, F6=8, F7=13, F8=21, F9=34.
Adding these numbers together:
0+1+1+2+3+5+8+13+21+34
The sum of the first 10 Fibonacci numbers is 88.