Trouble with the numerical evaluation of a series

The series is $$\;S=\displaystyle{\sum_{n=0}^\infty 2^{-n+\sin(n\pi/5)}}$$.

Mathematica doesn't find a closed form for Sum[2^(-n + Sin[n Pi/5]), {n, 0, Infinity}], so I tried both:

N[Sum[2^(-n + Sin[n Pi/5]), {n, 0, Infinity}], 100]
NSum[2^(-n + Sin[n Pi/5]), {n, 0, Infinity}, WorkingPrecision -> 100]


They return the same result:

2.621953360503001622580428627210775515701951638633391919349668089988053230383633286891962065806603221


However, as I discovered later, this result is wrong. Indeed, the series is not difficult to evaluate, thanks to the periodicity of $$\sin(n\pi/5)$$:

$$S=\left(\sum_{n=0}^\infty2^{-10n}\right)\left(\sum_{n=0}^92^{-n+\sin(n\pi/5)}\right)=\frac{1}{1-2^{-10}}\sum_{n=0}^92^{-n+\sin(n\pi/5)}$$

And N[1/(1 - 2^-10) Sum[2^(-n + Sin[n Pi/5]), {n, 0, 9}], 100] yields:

2.621953365022156800044269458734842788555304465923212022625632939238430813027293562013936284925892294


So the initial sum only had 9 correct digits. It's so bad that I am almost certain I am missing an important option, but I have no idea which (I tried playing with AccuracyGoal and PrecisionGoal, but it led me nowhere so far).

Any idea to get the numerical answer directly?

The same method as above can be used to evaluate $$\displaystyle{\sum_{n=0}^\infty 2^{-n+(-1)^n}}$$, and the sum is $$3$$, however, Mathematica fails with a message that starts with NIntegrate::ncvb: NIntegrate failed to converge to prescribed accuracy after 9 recursive bisections. So again I must be missing somthing important. The failure occurs with:

N[Sum[2^(-n + (-1)^n), {n, 0, Infinity}], 100]


My guess would be that it fails for this one because the correct answer is an integer, and thus it's impossible to give even a single correct digit since the sum could be 2.99999... or 3, but I'm not sure what really happens here, nor how to deal with this.

I'm not entirely new to Mathematica, but I have always found that numerical computations are difficult to handle with it. If someone can point to a good tutorial on this kind of problem or similar ones with NIntegrate, it would be really helpful!

• Not sure what your version is but running the first two lines in Version 12.1 returns exactly the same value for me. – anon248 Dec 28 '20 at 22:07
• @anon248 Oh, sorry, 12.1.1.0. And indeed, as I wrote just under those two lines, they yield the same result. Which is wrong. – user74736 Dec 28 '20 at 22:07
• Got it, my bad! – anon248 Dec 28 '20 at 22:09
• Does this answer your question? Numerical evaluation of a sum – m_goldberg Dec 29 '20 at 14:48

What seems to help at least with NSum is increasing the number of terms that are computed explicitly via the option NSumTerms. This way one can get agreement for all first $$100$$ digits:

NSum[2^(-n + Sin[n Pi/5]), {n, 0, Infinity}, NSumTerms -> 400, WorkingPrecision -> 100]

2.621953365022156800044269458734842788555304465923212022625632939238430813027293562013936284925892294

N[1/(1 - 2^-10) Sum[2^(-n + Sin[n Pi/5]), {n, 0, 9}], 100]

2.621953365022156800044269458734842788555304465923212022625632939238430813027293562013936284925892294

• Ah, yes, indeed I can find the correct result with larger NSumTerms. I didn't know this option. However, it's a bit disappointing if I have to know the correct answer beforehand to compute a numerical value to the desired accuracy. How am I supposed to know the first answer I found is wrong? What is frustrating with NSum and others is that I often get an answer, with no hint that something went wrong. But maybe I am asking too much. – user74736 Dec 28 '20 at 22:23
• I completely agree that there should be a better option. There are some previous questions on NSum, e.g. here and here. Particularly the second one has some more info on tweaking the internal methods, maybe something can be gained by that. – Hausdorff Dec 28 '20 at 22:52
• Damn. The second one is a question I asked 6 years ago, and I had completely forgotten about this. Shame shame shame... This time it's my bad, I should have known about NSumTerms. Thanks for having unearthed this! (incidentally, in the meantime I confess I have not made much progress on numerical evaluation with Mathematica, which is both embarrassing and annoying). – user74736 Dec 28 '20 at 23:01
Clear["Global*"]


To find the number of terms required, find the minimum value of n for which the last term is less than 10^-100

min = MinValue[{n, 2^(-n + Sin[n Pi/5]) < 10^-100}, n, Integers]

(* 334 *)

sum1 = NSum[2^(-n + Sin[n Pi/5]), {n, 0, Infinity}, NSumTerms -> 400,
WorkingPrecision -> 100]

(* 2.6219533650221568000442694587348427885553044659232120226256329392384308130272\
93562013936284925892294 *)

sum2 = NSum[2^(-n + Sin[n Pi/5]), {n, 0, Infinity}, NSumTerms -> min,
WorkingPrecision -> 100] // Quiet

(* 2.6219533650221568000442694587348427885553044659232120226256329392384308130272\
93562013936284925892294 *)

sum1 === sum2

(* True *)


You can look at the Sin[] term in the exponent as some periodic noise. It would be good to smooth that out before trying numeric methods by summing over a period at a time:

sum = NSum[
Sum[2^(-(10 n + k) + Sin[(10 n + k) Pi/5]), {k, 0, 9}],
{n, 0, Infinity}, WorkingPrecision -> 100]
(*
2.62195336502215680004426945873484278855530446592321202262563293923843\
0813027293562013936284925892294
*)

1/(1 - 2^-10) Sum[2^(-n + Sin[n Pi/5]), {n, 0, 9}] == sum

(*  True  *)


If you compare for high m

m = 10^5; N[ParallelSum[2^(-n + Sin[n*(Pi/5)]), {n, 0, m*10 + 9}], 16]


with

N[Sum[2^(-10*n), {n, 0, m}]*Sum[2^(-n + Sin[n*(Pi/5)]), {n, 0, 9}], 16]


you get the same value. But if you then set m=Infinity the values differ, which is understandable, because any numeric sum up to infinity must include some approximation (or it would take forever...). You may notice that the calculation for m=10^5 takes much longer than the one up to Infinity.

Your second sum you can split into odd and even terms like to get

Sum[2^(-n + (-1)^n), {n, 0, Infinity}] =
Sum[2^(-(2 n) + (-1)^(2 n)), {n, 0, Infinity}] +
Sum[2^(-(2 n + 1) + (-1)^(2 n + 1)), {n, 0, Infinity}] = 3
`