How would you implement recursion to compute the factorial of a number, and how would you optimize it?
Implement factorial(n) to return n! using recursion. Optimize repeated calls by caching previously computed results. The input is a non-negative integer, and the output is an integer.
def factorial(n):
def factorial(n):