Skip to content

Instantly share code, notes, and snippets.

@klgraham
Created September 9, 2016 08:36
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 klgraham/6935e3560058ed0f707752ec0638770e to your computer and use it in GitHub Desktop.
Save klgraham/6935e3560058ed0f707752ec0638770e to your computer and use it in GitHub Desktop.
Tail-recursive factorial in Scala
def factorial(n: BigInt): BigInt = {
def fact(n: BigInt, result: BigInt): BigInt = {
if (n == 0) return result
else return fact(n - 1, result * n)
}
return fact(n, 1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment