Jump to content

Factorial: Difference between revisions

Mathematica
(Pascal)
(Mathematica)
Line 262:
output :n * factorial :n-1
end
 
=={{header|Mathematica}}==
Note that Mathematica already comes with a factorial function, which can be used as e.g. <code>5!</code> (gives 120). So the following implementations are only of pedagogical value.
=== Recursive ===
<pre>
factorial[n_Integer] := n*factorial[n-1]
factorial[0] = 1
</pre>
=== Iterative (direct loop) ===
<pre>
factorial[n_Integer] :=
Block[{i, result = 1}, For[i = 1, i <= n, ++i, result *= i]; result]
</pre>
=== Iterative (list) ===
<pre>
factorial[n_Integer] := Block[{i}, Times @@ Table[i, {i, n}]]
</pre>
 
=={{header|OCaml}}==
973

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.