Created
November 28, 2011 22:49
-
-
Save lazywithclass/1402456 to your computer and use it in GitHub Desktop.
Fibonacci tail recursion in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
tailRecursive(0)
gives1
but the correct result is0
;)