Created
September 9, 2016 08:36
-
-
Save klgraham/6935e3560058ed0f707752ec0638770e to your computer and use it in GitHub Desktop.
Tail-recursive factorial in Scala
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: 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