In [50]:
# Generate e using infinite series

import sympy
from sympy.mpmath import mp
mp.dps = 50
mp.pretty = True

n = sympy.symbols('n', integer=True)

result = sympy.summation(1/sympy.factorial(n), (n, 0, sympy.oo))

print("The sum is: {}".format(result))

str(mp.e)


The sum is: E
Out[50]:
'2.7182818284590452353602874713526624977572470937'

In [59]:
import sympy as sy

# Custom (mathematic) function: 
# Compute the reciprocal of the factorial of an integer
class factorial_reciprocal(sy.Function):
    
    nargs = 1
    
    @classmethod
    def eval(cls, arg):
        #if arg < 0:
         #   raise ValueError('The factorial function takes only arguments greater than or equal to zero (0).')
        return 1 / sy.factorial(arg)
    
x = sy.var('x')

print("Sum: {}".format( sy.summation(factorial_reciprocal(x), (x, 0, sy.oo)) ))

print( factorial_reciprocal(x).series(x, x0=0, n=3) )


Sum: E
1 + EulerGamma*x + x**2*(-pi**2/12 + EulerGamma**2/2) + O(x**3)

In [ ]: