Search code examples
pythonrecursion

How to implement a recursive factorial function without using the multiplication operator?


this is my example of recursive using '*' operation!

def rec_fac(n):
   if n == 1:
       return n
   else:
       return n*rec_fac(n-1)

Solution

  • If you'd like a technicality, try this:

    def rec_fac(n):
        if n == 1:
            return n
        else:
            return eval('n*rec_fac(n-1)')
    

    Otherwise, you should try addition:

    def rec_fac(n):
        if n == 1:
            return 1
        else:
            return sum([n for i in range(rec_fac(n-1))])