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