Question
What are the big oh's for both of these methods(iterative and recursive fibonacci)? //Recursive public static long fib(long index) { if (index == 0) //
What are the big oh's for both of these methods(iterative and recursive fibonacci)?
//Recursive
public static long fib(long index) {
if (index == 0) // Base case
return 0;
else if (index == 1) // Base case
return 1;
else // Reduction and recursive calls
return fib(index - 1) + fib(index - 2);
}
//Iterative
public static long fib(long n) {
long f0 = 0; // For fib(0)
long f1 = 1; // For fib(1)
long f2 = 1; // For fib(2)
if (n == 0)
return f0;
else if (n == 1)
return f1;
else if (n == 2)
return f2;
for (int i = 3; i <= n; i++) {
f0 = f1;
f1 = f2;
f2 = f0 + f1;
}
return f2;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started