Skip to content

Instantly share code, notes, and snippets.

@lazywithclass
Created November 28, 2011 22:49
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lazywithclass/1402456 to your computer and use it in GitHub Desktop.
Save lazywithclass/1402456 to your computer and use it in GitHub Desktop.
Fibonacci tail recursion in Java
public static long tailRecursive(long n) {
if (n <= 2) {
return 1;
}
return tailRecursiveAux(0, 1, n);
}
private static long tailRecursiveAux(long a, long b, long count) {
if(count <= 0) {
return a;
}
return tailRecursiveAux(b, a+b, count-1);
}
@PaNaVTEC
Copy link

PaNaVTEC commented May 7, 2016

tailRecursive(0) gives 1 but the correct result is 0 ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment