Created
July 31, 2015 14:44
-
-
Save nelanka/37ac355c796c79ea3d7f to your computer and use it in GitHub Desktop.
Tail Recursive Fibonacci
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
def fib(n: Int): Int = { | |
@tailrec | |
def recFib(a: Int, b: Int, i: Int): Int = i match { | |
case 0 => a | |
case _ => recFib(b, a + b, i - 1) | |
} | |
recFib(0, 1, n) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment