Skip to content

Instantly share code, notes, and snippets.

@deepakkumarnd
Last active June 25, 2019 07:42
Show Gist options
  • Save deepakkumarnd/3e2e37b5b9779b2ac65346c3c737431b to your computer and use it in GitHub Desktop.
Save deepakkumarnd/3e2e37b5b9779b2ac65346c3c737431b to your computer and use it in GitHub Desktop.
factorial in scala
// 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