Recursive Factorial Calculation with Accumulator

  • Share this:

Code introduction


Calculates the factorial of a number using recursion, where 'accumulator' is used to accumulate the result


Technology Stack : Recursive calculation of the factorial of a number, using 'accumulator' for result accumulation

Code Type : Recursive function

Code Difficulty : Intermediate


                
                    
def factorial(n, accumulator=1):
    if n == 0:
        return accumulator
    else:
        return factorial(n - 1, n * accumulator)