Last active
August 29, 2015 14:23
-
-
Save ehsanullahjan/3eaf20df988edea00b1c to your computer and use it in GitHub Desktop.
Fibonacci using tail recursion
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: Long): Long = { | |
@annotation.tailrec | |
def go(n: Long, x: Long, y: Long): Long = | |
n match { | |
case 0 => x | |
case 1 => y | |
case _ => go(n - 1, y, x + y) | |
} | |
go(n, 0, 1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment