A recursive method is said to be tail recursive if there are no pending operatio
ID: 3745048 • Letter: A
Question
A recursive method is said to be tail recursive if there are no pending operations to be performed on return from a recursive call. Here is the fib method from Lecture 1: public static long fib(long index) ( // assume index0 if (index0) //Base case return 0 else if (index1) // Base case return 1; else // Reduction and recursive calls return fib (index 1) + fib(index 2) ) 11 end of method fib(long index) Is this function tail recursive? If not, rewrite this function into an equivalent one which is tail recursive.Explanation / Answer
If you have any doutbs, please give me comment...
The given method is not a tail recursive function.
When the recursive call is the last thing executed by the function then it is called tail recursive function
public static long fib(long index, int a, int b ){
if (index == 0)
return a;
if (index == 1)
return b;
return fib(index - 1, b, a + b);
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.