Skip to content

Instantly share code, notes, and snippets.

@Hoobie
Created March 16, 2020 18:59
Show Gist options
  • Save Hoobie/f162a00aeb4dcbdf5195c2c3b81c6fc8 to your computer and use it in GitHub Desktop.
Save Hoobie/f162a00aeb4dcbdf5195c2c3b81c6fc8 to your computer and use it in GitHub Desktop.
def factorial(n: Int): Int = if (n > 1) n * factorial(n - 1) else 1
@scala.annotation.tailrec
def tailFactorial(n: Int, acc: Int = 1): Int =
if (n == 1) acc
else tailFactorial(n - 1, n * acc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment