algorithm for factorial in python

Solutions on MaxInterview for algorithm for factorial in python by the best coders in the world

showing results for - "algorithm for factorial in python"
Alicia
30 Jan 2020
1def factorial(n):
2	for x in range(n-1,0,-1):
3		n = n*x
4	if n == 0: return 1
5	if n < 0 : return False
6	return n
7  
8factorial(5) # returns 120
9
10####### lambda Factorial ########
11
12factorial = lambda n: n*factorial(n-1) if n > 0 else 1
13#OR
14factorial = lambda n: n>0 and n*factorial(n-1) or 1
15
16######### Recursion ##########
17def factorial(n):
18	if n <= 0: return 1
19    else: return n * factorial(n-1)
20    
21# Do not use big number XD