Created
March 21, 2015 06:55
-
-
Save shigemk2/44cb18b33ae0b4c2561b 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 factorial(n: Int): Int = { | |
n match { | |
case n if n <= 0 => 1 | |
case n => n * factorial(n - 1) | |
} | |
} | |
println(factorial(100)) |
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 factorial(n: Int): Int = { | |
def go(n: Int, acc: Int): Int = | |
if (n <= 0) acc | |
else go(n - 1, n*acc) | |
go(n, 1) | |
} | |
println(factorial(100)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment