How can I convert this python code into Mathematical notation?

1.6k Views Asked by At

$\sum_{i=0}^n \frac{\sum_{i=0}^n n}{n!} $

Is it possible to convert this series to the form $ \frac{ n(n+1)}{2} $

I've tried my best to convert the code below to math notation but can't seem to get the same answer.

Python version:

vars1=1
vars2=0
for x in (range(1,n+1)):
  vars1*=x
  vars2+=x
  ans+=vars2/vars1
1

There are 1 best solutions below

7
On

You have the right idea thinking that var1 is representing the factorial. And it goes ${1!,2!,3!,...,n!}$. And indeed var2 is the sum of the first ${x}$ integers, and we are taking their ratio. So

$${S(n) = \sum_{x=1}^{n}\frac{\sum_{i=0}^{x}i}{x!}}$$

Now using the fact that

$${\sum_{i=0}^{x}i = \frac{x(x+1)}{2}}$$

We can get

$${S(n) = \sum_{x=1}^{n}\frac{x(x+1)}{2x!}}$$

I don't think a nice partial sum formula will exist. If you take a look at WolframAlpha, it's a long expression involving the Gamma function and all sorts: https://www.wolframalpha.com/input/?i=sum+from+k%3D1+to+n+%28k*%28%28k%2B1%29%2F2%29%29%2F%28k%21%29

What you can do, however, is take a limit as ${n\rightarrow\infty}$. This will tell you, as $n$ get's bigger, what the sum is approaching. I imagine that it converges quite quickly, so for large $n$ this will be a good approximation anyways. I am going to change the variable ${x}$ into the variable ${i}$, since otherwise things are going to get very confusing. Notice

$${S(n) = \frac{1}{2}\sum_{i=1}^{n}\left[\frac{i^2}{i!} + \frac{i}{i!}\right]=\frac{1}{2}\left(\sum_{i=1}^{n}\frac{i}{(i-1)!} + \sum_{i=1}^{n}\frac{1}{(i-1)!}\right)}$$

So we have two sums to calculate. Using

$${e^{x} = \sum_{i=0}^{\infty}\frac{x^i}{i!}}$$

you see the right sum

$${\sum_{i=1}^{n}\frac{1}{(i-1)!}\rightarrow \sum_{i=0}^{\infty}\frac{1}{i!}=e}$$

as ${n\rightarrow \infty}$. Differentiating the Taylor Series for ${e^{x}}$, multiplying by $x$ and differentiating once again and setting ${x=1}$ also yields that

$${\sum_{i=1}^{n}\frac{i}{(i-1)!}\rightarrow 2e}$$

as ${n\rightarrow\infty}$. And hence we get overall that

$${\lim_{n\rightarrow\infty}S(n)=\frac{1}{2}\left(2e+e\right)=\frac{3e}{2}\approx 4.07742274}$$

This is what your sum "approaches" as ${n\rightarrow\infty}$.