Back
Simplest way is to use math.factorial (Which is available from python 2.6 and above)
import mathmath.factorial(1000)
import math
math.factorial(1000)
You can also use an iterative approach:
def factorial(n): fact = 1 for num in range(2, n + 1): fact *= num return fact
def factorial(n):
fact = 1
for num in range(2, n + 1):
fact *= num
return fact
or recursive approach:
def factorial(n): if n < 2: return 1 else: return n * factorial(n-1)
if n < 2:
return 1
else:
return n * factorial(n-1)
Want to learn the concepts of python in detail? Join python certification course and get certifed
30.9k questions
32.9k answers
500 comments
665 users