Fun fact. You can use any programming language for making a factorial function. But do you know, that you can use any programming language to define a factorial function? My understanding is Python has got the power of defining simple recursive functions through lambda functions.


Python is a fun language to learn, and really easy to pick up even if you are new to programming.

Lets write a simple factorial function in python. We will use lambda functions and recursion to do it.

def factorial(n):

return 1 if (n==1 or n==0) else n*factorial(n-1);

num = 5;

print(“Factorial of”,num,”is”,factorial(num))

def factorial(n):

if n == 0:

return 1

else:

return n * factorial(n-1)

def factorial(n):

return n>1 and n*factorial(n-1) or 1

def factorial(n):

return reduce(lambda a,b:a*b, range(1,n+1))

factorial = lambda n: n * factorial(n – 1) if n > 1 else 1

print(factorial(5))

def factorial(n):

if n <= 1: return 1; else: return n * factorial(n - 1); def factorial(x): if x == 1: return 1 else: return (x * factorial(x-1)) print factorial(5)


Leave a Reply

Your email address will not be published.