Let the sequence \(\seq{f_n}{n}\) be given by the recurrence relation \(f_{n}=n\cdot f_{n-1}\) and the initial value \(f_0=1\text{.}\)
We compute \(f_5\text{.}\)
\begin{align*}
f_5 \amp =5\cdot f_4 \\
\amp = 5\cdot(4\cdot f_3) \\
\amp = 5\cdot(4\cdot(3\cdot f_2)) \\
\amp = 5\cdot(4\cdot(3\cdot (2\cdot f_1))) \\
\amp = 5\cdot(4\cdot(3\cdot (2\cdot (1\cdot f_0)))) \\
\amp = 5\cdot(4\cdot(3\cdot (2\cdot (1\cdot 1)))) \\
\amp = 5\cdot(4\cdot(3\cdot (2\cdot 1))) \\
\amp = 5\cdot(4\cdot(3\cdot 2)) \\
\amp = 5\cdot(4\cdot6) \\
\amp = 5\cdot24 \\
\amp = 120
\end{align*}
We have computed \(f_5=120\) using 4 multiplications.
We can directly transfer the definition of the recurrence relation into Python by letting a function call itself. Our Python function evaluates \(f(n)\) for a non-negative integer \(n\) recursively following the recurrence relation \(f(n)=n\cdot f(n-1)\) and the initial value \(f(0)=1\text{.}\)
def f(n):
if n==0:
return 1
return n*f(n-1)
def f(n):
f = 1
for n in range(1,n):
f=f*n
return f

