Skip to content

Instantly share code, notes, and snippets.

@klgraham
Created September 9, 2016 08:36
Show Gist options
  • 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