Factorial in Scala
Example for versions
Scala 2.7.7-final
This example uses recursive factorial definition.
object Factorial {
def factorial(n: Int): Long =
if (n == 0) 1
else n * factorial(n - 1)
def main(args: Array[String]) {
for {i <- List.range(0, 17)}
yield { println(i + "! = " + factorial(i)) }
}
}
Comments
]]>blog comments powered by Disqus
]]>