Factorial: Difference between revisions

Content added Content deleted
(Pascal)
(Mathematica)
Line 262: Line 262:
output :n * factorial :n-1
output :n * factorial :n-1
end
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}}==
=={{header|OCaml}}==