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:


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)$:


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


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!

  • $\begingroup$ Not sure what your version is but running the first two lines in Version 12.1 returns exactly the same value for me. $\endgroup$ – anon248 Dec 28 '20 at 22:07
  • $\begingroup$ @anon248 Oh, sorry, And indeed, as I wrote just under those two lines, they yield the same result. Which is wrong. $\endgroup$ – user74736 Dec 28 '20 at 22:07
  • $\begingroup$ Got it, my bad! $\endgroup$ – anon248 Dec 28 '20 at 22:09
  • $\begingroup$ Does this answer your question? Numerical evaluation of a sum $\endgroup$ – 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]
N[1/(1 - 2^-10) Sum[2^(-n + Sin[n Pi/5]), {n, 0, 9}], 100]
  • $\begingroup$ 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. $\endgroup$ – user74736 Dec 28 '20 at 22:23
  • $\begingroup$ 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. $\endgroup$ – Hausdorff Dec 28 '20 at 22:52
  • $\begingroup$ 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). $\endgroup$ – user74736 Dec 28 '20 at 23:01

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]

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]


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy