ccess Apps MInbox saki... LaGuardia Commun... Apple D Disney (1,405) 1. Write an
ID: 3817022 • Letter: C
Question
ccess Apps MInbox saki... LaGuardia Commun... Apple D Disney (1,405) 1. Write an iterative C++ function that inputs a nonnegative integer n and returns the nth Fibonacci number. 2. Write a recursive C++ function that inputs a nonnegative integer nand returns the nth Fibonacci number. 3. Compare the number of operations and time taken to compute Fibonacci numbers recursively versus that needed to compute them iteratively. 4. Use the above functions to write a C++ program for solving each of the following computational problems l. Find the exact value of foo, tsoo, and fiooo, where fr is the nth Fibonacci number. What are times taken to find out the exact values? ll. Find the smallest Fibonacci number (1)greater than 1,000,000, and (2) greater than 1,000,000,000. Ill. Find as many prime Fibonacci numbers as you can. It is unknown whether there are infinitely many of these. Find out the times taken to find first 10, 20.30, 40...up to 200 and draw a graph and see the pattern. Initial report submission to "Discussions", due on Sunday April 16: 1. Prepare a project report in WORD document to describe how you implement above tasks, including (1) problem analysis and solution plan, (2) source code, (3) discussion on experimental results in tables or graphs, (4) reflection and 2. Your reflection should address all of the below mentioned questions a) Describe the Fibonacci series and write briefly what you have done in the b) What are the different skills, programming techniques have you used in order to run the experiments? c) What did you observe when you did the comparisons in Task 3 and Task 4? Explain the graph that you have drawn from Task 4.lll? d) List at least three different applications of the Fibonacci numbers and describe one them details. Think of situation or a real world problem where you can apply concept of Fibonacci numbers to solve it Explain? e) write a paragraph, explaining what you have done in this assignment. What were the challenges you have faced when solving these problems How can you improve the programs you have written to solve theseExplanation / Answer
Solution:-
(1) The below given C++ code is an iterative method to compute nth Fibonacci number.
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
(2) The below givenC++ code is an recursive method to compute nth Fibonacci number.
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
(3) Time complexity of Iterative method to calculate nth Fibonacci number :-
The operation comparison of iterative and Recursive function for Fibonacci number is given below.
As per operation comparisons we can observe that iterative method is far better than recursive method. Recursive method is a function of itself and Everytime it calculate the itself recursively. So for f(1) it is simple but for f(10) it will call itself 9 times and Everytime it will calculate itself from start, as for f(n) it calculate f(n-1) Everytime. On the other hand iterative function is a loop which does only one Operation at an iteration a single iteration is a single execution of a process.
The time complexity of Recursive method is O (2^n) that is exponential and iterative method has time complexity is O (n) that is linear. So it is very slow comparatively iterative method.
The space complexity is same for Both approaches that is O(n).
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
(4)
This is iterative function to calculate 100 Fibonacci number. It requires a long long type function to store the number. Because it will not fit in 64 bit int.
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
Below given is a recursive method to calculate 100th Fibonacci number.
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
By using above function we can calculate the 500th and 1000th Fibonacci number recursively or by iterative method.
As discussed above the time complexity of iterative method is O (n) that is lenear. So the time taken to calculate the 100, 500 and 1000th Fibonacci number is lenearly equal to n.
The time complexity of Recursive method is O (2^n) that is exponential. So the time taken to calculate the 100, 500 and 1000th Fibonacci number is exponentially equal to n.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.