In: Computer Science
if this is considered more than one question then please explain part 'I' (Find the exact value of f100, f500, and f1000, where fn is the nth Fibonacci number. What are times taken to find out the exact values?). I am having trouble writing the code for both recursive and iterative functions without having the program crash. I assume its because the numbers are so high.
Goal: The main goal of the project is to let students use their prior knowledge, try to use the skills they have learnt to solve real world problems. Using this assignment students are required to learn to use the skills they have learned in their previous classes, solve the problems given and reflect on how they can apply it to solve real world issues.
Deliverables: The students are required to submit a written report along with the programs they have written. The report has to address all the prompts mentioned under the Report section. The report should be properly presented by using appropriate font (Time new roman, preferably font size 12 till 16) and grammar.
Tasks:
Report: Your report should address all of the below mentioned questions
Hi... Greetings for the day!
Below is the code for Fibonacci numbers with both using recursion and iteration.
Don't use online compilers for executing the program as those will give timeout errors as they are designed for some load. Use the compiler installed on your system.
Note: Execute only one function at a time with only one input for capturing time taken for each function execution cycle.
To avoid crashing the program, use eclipse or intellij IDEs for better coding and measuring execution times.
Please understand the note mentioned below the program.
#include<bits/stdc++.h>
using namespace std::chrono;
using namespace std;
long long fibRec(int n)
{
if (n <= 1)
return n;
return fibRec(n-1) + fibRec(n-2);
}
long long fibItr(int n)
{
long long previous = 1;
long long current = 1;
long long next = 1;
for (int i = 3; i <= n; ++i)
{
next = current + previous;
previous = current;
current = next;
}
return next;
}
int main ()
{
int n = 10; //100, 500, 1000
auto start = high_resolution_clock::now();
cout << fibRec(n) << " ";
//cout << fibItr(n) << " ";
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop -
start);
cout << duration.count() << " milliseconds"
<< endl;
getchar();
return 0;
}
Outputs:
For n=10:
Iterative function:
55 19 milliseconds
Recursive function:
55 17 milliseconds
For n=20:
Iterative function:
6765 25 milliseconds
Recursive function:
6765 99 milliseconds
For n=30:
Iterative function:
832040 30 milliseconds
Recursive function:
832040 50424 milliseconds
Important Note:
If you observe the time execution difference recursive function call will take more time and increased drastically with input number. If you want to calculate large numbers it will take years with recursive function. The reason behind this is internally it will maintain call stack for accessing previously calculated function value. It will store all the function calls and their corresponding results till it reaches base condition then it will pop the stack values and then result will be computed. But for iterative method we are updating the values on the go and there is no memory usage for stack and calculation will be fast.
Time complexity for iterative function is O(n) as at max it will go n iterations.
Time complexity for recursive function is exponential approximately 2^n function calls. T(n) = T(n-1)+T(n-2).
See the below diagram for better understanding.
For every recursion call function is initiating 2 times till it reaches base condition 1 or 0.
I hope this is helpful.