Summing Factorials In Python
I would like to compute sums with factorials using symbolic algebra in python. The simplest version of the problem I can generate is the following one: from sympy.abc import j from
Solution 1:
Using Sympy's own factorial
function (instead of the math
module's factorial function) could perhaps return what you want.
Following your initial setup but omitting from math import factorial
, you could then write:
>>>from sympy import factorial, symbols>>>x = symbols('x')>>>summation(x**(j-1)/factorial(j-1), (j, 1, 3))
x**2/2 + x + 1
This reduces the summation of the factorial series to a simple quadratic equation.
I notice you are calculating the sum of the first few terms of the power series expansion of exp(x)
:
1 + x + x**2/2! + x**3/3! + ...
Solution 2:
The only solution I can think of avoiding loops is a functional version :S:
from math import factorial
reduce(int.__add__,map(factorial,xrange(1,5)))
Post a Comment for "Summing Factorials In Python"