Created
March 21, 2015 07:26
-
-
Save shigemk2/543f53dc05f45f1cba69 to your computer and use it in GitHub Desktop.
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: Int): Int = { | |
n match { | |
case 0 => 0 | |
case 1 => 1 | |
case n => fib(n - 1) + fib(n - 2) | |
} | |
} | |
println(fib(10)) |
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: 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