You can download this code by clicking the button below.
This code is now available for download.
This function calculates the factorial of an integer using recursion. It also uses a cache to optimize repeated calculations.
Technology Stack : Recursion, caching (dictionary)
Code Type : Recursive function
Code Difficulty : Intermediate
def factorial(n, _cache={}):
if n in _cache:
return _cache[n]
elif n == 0:
return 1
else:
_cache[n] = n * factorial(n - 1, _cache)
return _cache[n]