# Calculate an infinite series by computer: get sum and number of terms for given precision

I need to find a sum and number of terms in an infinite series if ε ∈ (0;1) and x ∈ (1;5):

$$\sum_{k=0}^∞ \frac{k^2x^k}{(k+1)!}$$

I was able to convert it into a simpler form, but stuck there:

$$\lim_{k→∞} \frac{x^k×k^2}{k!×(k+1)} = e^x \frac{k^2}{k+1}$$

Can you suggest a direction to simplify the $$\frac{k^2}{k+1}$$ portion, please?

• I'm not sure what you did above , but in the limit part that's wrong: it cannot be that the limit when $\;k\to\infty\;$ is something were $\;k\;$ appears... Also that...sign...like epsilon or something at the end of your first line: what is that in $\;(0,1)\;$ ? – DonAntonio Nov 16 '18 at 22:20
• What you need to do is determine how large is the $k$th term for the particular $x$. For example, pick the upper bound of your interval $x=5$. Let's say you measure precision in the number of correct decimal digits. Then you need to pick some $d$ and find $k$ such that $$\frac{k^2 5^k}{(k+1)!}< 10^{-d}$$ Then you can say that you need at least $k$ terms to get $d$ digits. Obviously, we should be using partial sums instead of a single term, but your series converges fast as factorials overtake the powers. So this bound should be a good start – Yuriy S Nov 16 '18 at 22:27
• And whatever you did with the limit is wrong as DonAntonio pointed out. What were you trying to do? – Yuriy S Nov 16 '18 at 22:28
• Thanks a lot for your input, @DonAntonio and Yuriy S! I would clarify the epsilon meaning with my professor because I was assuming that it's just my lack of knowledge about precisions in math (because I only know about $10^{-n}$ type of notations. • • • I've naively tried a limit because the series is convergent, so I was thinking that sum and limit notations are the same. I get that this was wrong assumption. – terales Nov 17 '18 at 9:34
• Just got a confirmation: ε ∈ (0;1) in CS class tasks, in my University at least, means that user can specify any input from 0 to 1. So the resulting epsilon would be in an expected $10^{-d}$ format. – terales Nov 17 '18 at 12:00

\begin{align} \sum_{k=0}^\infty\frac{k^2x^k}{(k+1)!} &=\sum_{k=0}^\infty\frac{((k+1)k-(k+1)+1)\,x^k}{(k+1)!}\tag1\\ &=\sum_{k=1}^\infty\frac{x^k}{(k-1)!}-\sum_{k=0}^\infty\frac{x^k}{k!}+\sum_{k=0}^\infty\frac{x^k}{(k+1)!}\tag2\\ &=x\sum_{k=0}^\infty\frac{x^k}{k!}-\sum_{k=0}^\infty\frac{x^k}{k!}+\frac1x\left(\sum_{k=0}^\infty\frac{x^k}{k!}-1\right)\tag3\\ &=(x-1)e^x+\frac{e^x-1}x\tag4\\[3pt] &=\frac{(x^3+1)e^x-(x+1)}{x(x+1)}\tag5 \end{align} Explanation:
$$(1)$$: $$k^2=(k+1)k-(k+1)+1$$
$$(2)$$: cancel numerators and denominators
$$\phantom{\text{(2):}}$$ since $$(k+1)k=0$$ when $$k=0$$, we can remove the $$k=0$$ term from the first sum
$$(3)$$: substitute $$k\mapsto k+1$$ in the first sum and $$k\mapsto k-1$$ in the last sum
$$(4)$$: recognize the series for $$e^x$$
$$(5)$$: simplify the fraction

• I had the big pleasure to show again your beautiful approximation. Cheers. – Claude Leibovici Nov 18 '18 at 3:58
• So interesting that your final answer is exactly that of mine, yet you are complaining me.......... – Mostafa Ayaz Nov 18 '18 at 20:40
• Oh God, so results are the same? Is it possible to get rid of $x^3+1$? – terales Nov 18 '18 at 20:46
• @MostafaAyaz: your answer was not correct until you edited it. Now that you've added the $-\frac1x$, it matches my answer. – robjohn Nov 18 '18 at 20:57
• @terales: Both $(4)$ and $(5)$ are complete answers. $(4)$ is nice in that the limit as $x\to0$ can be seen more easily. $(5)$ is nice in that it is a bit cleaner. However, my answer is not the same as Mostafa Ayaz's answer before he corrected it. – robjohn Nov 18 '18 at 21:00

$$\sum_{k=0}^∞ \frac{k^2x^k}{(k+1)!}{\\=\sum_{k=0}^∞ \frac{(k^2+k)x^k}{(k+1)!}-\sum_{k=0}^∞ \frac{kx^k}{(k+1)!}\\=\sum_{k=1}^∞ \frac{x^k}{(k-1)!}-\sum_{k=0}^∞ \frac{kx^k}{(k+1)!}\\=xe^x-\sum_{k=0}^∞ \frac{kx^k}{(k+1)!}}$$also $$\sum_{k=0}^∞ \frac{kx^k}{(k+1)!}=\sum_{k=0}^∞ \frac{x^k}{k!}-\sum_{k=0}^∞ \frac{x^k}{(k+1)!}=e^x-\sum_{k=0}^∞ \frac{x^{k+1}}{x(k+1)!}=e^x-{1\over x}(e^x-1)$$therefore $$\sum_{k=0}^∞ \frac{k^2x^k}{(k+1)!}=e^x(x-1+{1\over x})-{1\over x}$$

• Wow, thanks! It took me 30 mins to understand tricks with $k^2=k^2 + k-k$, $x^k=x×x^{k-1}$ and $kx^k=(k+1)x^k-x^k$. Now I see how beautiful your solution is! – terales Nov 17 '18 at 9:38
• I'm happy if it could help you! – Mostafa Ayaz Nov 17 '18 at 14:39
• Notice that the series converges to $0$ at $x=0$, but $e^x\!\left(x-1-\frac1x\right)$ blows up. – robjohn Nov 17 '18 at 15:07

I had very serious mistakes in my answer. Trying to fix them now.

After Yuriy S's comment, if you need to find $$k$$ such that
$$\frac{k^2\, x^k}{(k+1)!}< 10^{-d} \tag 1$$ Since $$\frac{k^2}{(k+1)!}=\frac k{k+1} \frac 1 {(k-1)!}\sim \frac 1 {(k-1)!}$$ we can approximate $$(1)$$ by equation $$(k-1)! = ( x\,{10^d})\,x^{k-1}$$ or, simpler, $$n!= (x\,10^d)\, x^n \qquad \text{where }\qquad n=k-1$$ If you look at this question of mine, you will see a magnificent approximation of the inverse factorial function which was proposed by @robjohn.

Applied to this case, this would give $$\color{blue} { k\sim e\, x\,e^{W(t)}+\frac 12} \qquad \text{where }\qquad \color{blue} {t=\frac {\log \left(\frac{x\, 10^{2 d}}{2 \pi }\right) } {2e x }}\tag 2$$ where appears $$W(.)$$ which is Lambert function.

Let us try for a few values of $$x$$ and $$d$$. The next table reports in the real domain the value given by the approximation $$(2)$$ as well as the exact solution of $$(1)$$. You just need to use the next integer. $$\left( \begin{array}{ccc} x & d & \text{approximation} & \text{exact} \\ 1 & 4 & 8.33367 & 8.28080 \\ 1 & 5 & 9.41743 & 9.37320 \\ 1 & 6 & 10.4438 & 10.4056 \\ 1 & 7 & 11.4258 & 11.3922 \\ 1 & 8 & 12.3721 & 12.3420 \\ 1 & 9 & 13.2888 & 13.2615 \\ & & & \\ 2 & 4 & 12.0127 & 11.9689 \\ 2 & 5 & 13.2892 & 13.2517 \\ 2 & 6 & 14.5002 & 14.4674 \\ 2 & 7 & 15.6595 & 15.6302 \\ 2 & 8 & 16.7765 & 16.7500 \\ 2 & 9 & 17.8579 & 17.8338 \\ & & & \\ 3 & 4 & 15.3123 & 15.2744 \\ 3 & 5 & 16.7140 & 16.6810 \\ 3 & 6 & 18.0471 & 18.0178 \\ 3 & 7 & 19.3250 & 19.2987 \\ 3 & 8 & 20.5572 & 20.5333 \\ 3 & 9 & 21.7508 & 21.7288 \\ & & & \\ 4 & 4 & 18.4409 & 18.4071 \\ 4 & 5 & 19.9348 & 19.9051 \\ 4 & 6 & 21.3592 & 21.3326 \\ 4 & 7 & 22.7268 & 22.7028 \\ 4 & 8 & 24.0470 & 24.0250 \\ 4 & 9 & 25.3267 & 25.3063 \\ & & & \\ 5 & 4 & 21.4717 & 21.4412 \\ 5 & 5 & 23.0379 & 23.0108 \\ 5 & 6 & 24.5347 & 24.5104 \\ 5 & 7 & 25.9744 & 25.9521 \\ 5 & 8 & 27.3656 & 27.3452 \\ 5 & 9 & 28.7152 & 28.6963 \end{array} \right)$$

• Thanks for an answer! I can't get "assume that k is large and reduce the problem to…" part. $k$ seems to be less than 1000 for a precision of 4 digits, isn't it? – terales Nov 17 '18 at 9:57
• @terales. In fact, it is a typo ! $k$ does not need to be large at all. I shall edit now. Cheers. – Claude Leibovici Nov 17 '18 at 10:23
• @terales. $\frac{k^2}{(k+1)!}=\frac k {k+1} \times \frac{1}{(k-1)!}\approx \frac{1}{(k-1)!}$ – Claude Leibovici Nov 18 '18 at 4:01
• Wow, how elegant it is! Thanks a lot for your comment from the real math side! – terales Nov 18 '18 at 18:13