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. The factorial is a recursive concept, where n! is defined as n times the factorial of n-1, and so on, until it reaches 1.
Technology Stack : Recursion
Code Type : Recursive function
Code Difficulty : Intermediate
def factorial(n, accumulator=1):
if n < 0:
raise ValueError("n must be a non-negative integer")
if n == 0:
return accumulator
return factorial(n - 1, n * accumulator)