Factorial: Difference between revisions

Line 9,154:
===Imperative===
An imperative style using a mutable variable:
<syntaxhighlight lang="scala">def factorial(n: Int) = {
var res = 1
for (i <- 1 to n)
res *= i
res
Line 9,164:
Using naive recursion:
<syntaxhighlight lang="scala">def factorial(n: Int): Int =
if (n ==< 0) 1
else n * factorial(n - 1)</syntaxhighlight>
 
Using tail recursion with a helper function:
92

edits