Skip to content

Instantly share code, notes, and snippets.

@cgopalan
Created September 13, 2011 04:27
Show Gist options
  • Save cgopalan/1213115 to your computer and use it in GitHub Desktop.
Save cgopalan/1213115 to your computer and use it in GitHub Desktop.
Scala Factorial in terms of a product function
object Factorial {
def product(f: Int => Int)(a: Int, b:Int): Int = {
if (a > b) 1 else f(a) * product(f)(a + 1, b)
}
def factorial(n: Int): Int = {
product(x => x)(1, n)
}
def main(args: Array[String]) {
println("Factorial of 6 is: " + factorial(6))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment