Recursive Factorial Calculation in Python

  • Share this:

Code introduction


Calculates the factorial of a non-negative integer. Factorial is a recursive concept, where n factorial is n times (n-1) factorial, and so on, until 1 factorial is 1.


Technology Stack : Recursive calculation

Code Type : Mathematical calculation

Code Difficulty : Intermediate


                
                    
def factorial(n, accumulator=1):
    if n < 0:
        return "Factorial not defined for negative numbers"
    if n == 0:
        return accumulator
    else:
        return factorial(n-1, n*accumulator)