Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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