Last active
June 25, 2019 07:42
-
-
Save deepakkumarnd/3e2e37b5b9779b2ac65346c3c737431b to your computer and use it in GitHub Desktop.
factorial in scala
This file contains hidden or 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
// write a program to print factorial of a given integer | |
def factorial(n: Int): Long = { | |
@annotation.tailrec // asks compiler to raise a compile error if unable to do tail call recurssion optimization | |
def loop(num: Int, acc: Long): Long = | |
if (num > 1) loop(num - 1, acc * (num - 1)) | |
else acc | |
loop(n, n) | |
} | |
println(factorial(25)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment