Factorial Function in Python
A recursive function (def factorial(i)) to compute factorial of a number.  It can 
be used to compute the value of (EXP)
Euler Number.

       n=∞
 Exp = ∑ 1/n! = 1/0! + 1/1! + 1/2! + ..... ∞ = 2.71828182845904523536 ...
       n=0

Create a file "factExp.py" 
----------------------------------------------------------------
#factExp.py
def factorial(i):
 if i == 0:
   return 1
 elif i == 1:
   return i
 else:
   return i * factorial(i-1)

eval = 0
for n in range(15):
     eval += 1/factorial(n)
     print('Factorial [',n,'] =',factorial(n),' Exp = ', eval)
----------------------------------------------------------------

Euler Number and Factorial


Higher precision of the Euler Number can be attained by iterating to a much higher number 
(greater than 15). 

   Python Reference    Math Reference


Last Revised On: February 20th, 2022

  4931