the-honk/school/a-level/Y12 2022-2024/Algorithms/recursion/factorial.py

15 lines
No EOL
278 B
Python

def recursive_factorial(n):
if n == 1:
return 1
return n * recursive_factorial(n - 1)
def iterative_factorial(n):
out = 1
for i in range(2, n + 1):
out *= i
return out
assert iterative_factorial(5) == recursive_factorial(5)