Skip to content

Instantly share code, notes, and snippets.

@abevoelker
Created June 27, 2011 13:59
Show Gist options
  • Save abevoelker/1048904 to your computer and use it in GitHub Desktop.
Save abevoelker/1048904 to your computer and use it in GitHub Desktop.
Fibonacci in Scala
// 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