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