Recursive Factorial Calculator in Python

  • Share this:

Code introduction


Calculates the factorial of a given positive integer, i.e., n! = n * (n-1) * ... * 1. This uses the recursive method to calculate the factorial.


Technology Stack : Recursive

Code Type : Function

Code Difficulty : Intermediate


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