Question

In: Computer Science

why Fibonacci does not have a good performance when it is done recursively? what is a...

why Fibonacci does not have a good performance when it is done recursively? what is a stopping case for recursive functions.?

Solutions

Expert Solution

=> Since Time Complexity of the recursive function is O(2^n) or exponential which is greater than the O(n) Time Complexity in the Iterative approach.

Recursive Code

public static int fibonacciRecursion(int nthNumber) {
        //use recursion
        if (nthNumber == 0) {

            return 0;

        } else if (nthNumber == 1) {

            return 1;
        }   
     return fibonacciRecursion(nthNumber - 1) + fibonacciRecursion(nthNumber - 2);
    }

Space Complexity : The space required is proportional to the maximum depth of the recursion tree, because, that is the maximum number of elements that can be present in the implicit function call stack but for the iterative approach, the amount of space required is the same for fib(6) and fib(100), i.e. as N changes the space/memory used remains the same. Hence its space complexity is O(1) or constant.

Recursion Tree:

=> Stopping Case or Base Case: In the recursive approach, every function call will recursively resolve into n = 1 or n = 0 so we need to return the required value of them to compute the overall function call. Also if the user passes 1 as the function parameter the function should resolve it and return the computed value without getting into the negative value of n in recursive calls.

Drop a comment for any doubt, I would love to resolve it.


Related Solutions

Financial statement analysis- Theory questions What does it mean when a company has good financial performance?...
Financial statement analysis- Theory questions What does it mean when a company has good financial performance? Which financial statement ratios indicate good financial performance and how so? What does it mean when a company has good investment potential? Which financial statement ratios indicate good investment potential and how so? Finally what is the difference between good financial performance and good investment potential?
When sounds of two frequencies sound 'good' together, what does that mean? Why does one note...
When sounds of two frequencies sound 'good' together, what does that mean? Why does one note which is an octave above another note, sound like the same note?
why is it essential to have a good understanding to what dementia patients face when working...
why is it essential to have a good understanding to what dementia patients face when working as a nurse or medical staff member?
What does it mean to have a good life? What does it mean to be a...
What does it mean to have a good life? What does it mean to be a good person?
hi . please comment on pay-for-performance is a good compensation system, why and what types of...
hi . please comment on pay-for-performance is a good compensation system, why and what types of positions and/or companies would be good to use this method with. please use your own worlds I don't want any copy THANK you
Explain what forecasting is. Do you have any good or bad examples of forecasting done by...
Explain what forecasting is. Do you have any good or bad examples of forecasting done by firms with which you are familiar? As a financial manager in developing forecast for the firm, where would you go to in order to start your forecast and refine it with more accurate future projections concerning interest rates, raw material prices and the like to build your estimates?
What does it mean “optimisation of Building Automation Systems (BAS)” , When it should be done?...
What does it mean “optimisation of Building Automation Systems (BAS)” , When it should be done? How we know that the BAS needs to be optimized ? What are the main benefits of BAS optimization? Please answer it using computer
What does the medical assistant do when a patient is having an endoscopy procedure done?.
What does the medical assistant do when a patient is having an endoscopy procedure done?.
What makes a “good diet” a good diet? why do we eat? what does food do...
What makes a “good diet” a good diet? why do we eat? what does food do for us? what are the major nutrients we need to ingest?
question about defibrillator. . Why you apply the shock . What is done when defibrillator ....
question about defibrillator. . Why you apply the shock . What is done when defibrillator . How defibrillator work . Then why we chose Biophasic
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT