Skip to content

Instantly share code, notes, and snippets.

@shigemk2
Created March 21, 2015 06:55
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/44cb18b33ae0b4c2561b to your computer and use it in GitHub Desktop.
Save shigemk2/44cb18b33ae0b4c2561b to your computer and use it in GitHub Desktop.
def factorial(n: Int): Int = {
n match {
case n if n <= 0 => 1
case n => n * factorial(n - 1)
}
}
println(factorial(100))
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