Created
June 27, 2011 13:59
-
-
Save abevoelker/1048904 to your computer and use it in GitHub Desktop.
Fibonacci in Scala
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
// Test directly from shell using (e.g. input 5): | |
// scala -i fibonacci.scala -e 'println(fibonacci(5))' | |
def fibonacci(n: Int) = fibonacci_tail_recursive(n, 1, 0) | |
def fibonacci_tail_recursive(iter: Int, current: BigInt, last: BigInt): BigInt= { | |
if (iter == 0) | |
last | |
else | |
fibonacci_tail_recursive(iter-1, current+last, current) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment