Skip to content

Instantly share code, notes, and snippets.

@shigemk2
Created March 21, 2015 07:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shigemk2/543f53dc05f45f1cba69 to your computer and use it in GitHub Desktop.
Save shigemk2/543f53dc05f45f1cba69 to your computer and use it in GitHub Desktop.
def fib(n: Int): Int = {
n match {
case 0 => 0
case 1 => 1
case n => fib(n - 1) + fib(n - 2)
}
}
println(fib(10))
def fib(n: Int): Int = {
@annotation.tailrec
def go(n: Int, a: Int, b: Int): Int =
n match {
case 0 => 0
case 1 => a
case n => go(n - 1, a + b, a)
}
go(n, 1, 0)
}
println(fib(10))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment