You can download this code by clicking the button below.
This code is now available for download.
Calculate the factorial of a given non-negative integer. It uses recursion and caching to optimize the calculation process.
Technology Stack : Built-in functions, recursion, dictionary
Code Type : Mathematical calculation
Code Difficulty : Intermediate
def factorial(n, cache=None):
if cache is None:
cache = {}
if n in cache:
return cache[n]
if n == 0:
return 1
else:
cache[n] = n * factorial(n-1, cache)
return cache[n]