Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Tail Recursion

Name: Anonymous 2011-10-19 21:09

How do i make this function tail recursive in java


public int fib(int n)
{
  return( n <= 1 ? n : fib(n-1) + fib(n-2) );
}

Name: Anonymous 2011-10-21 3:22

A tail call is just returning another function.


return fn();


Some compilers will optimize it to run in constant space.


This is not doing it right as you are returning an operation on the function result.


return n * fn();


Have a look at this:
http://stackoverflow.com/questions/5453376/tail-call-optimization-for-fibonacci-function-in-java

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List