recursive factorial function
2 lines of code:
def fac(n):
return 1 if (n < 1) else n * fac(n-1)
Test it:
print fac(4)
Result:
24
We can combine the two functions to this single recursive function:
def factorial(n):
if n < 1: # base case
return 1
else:
returnNumber = n * factorial(n - 1) # recursive call
print(str(n) + '! = ' + str(returnNumber))
return returnNumber