I have a sum in Mathematica as follows:

Sum[If[ m^2 + n^2 != 0, 
 1./(m^2 + n^2) ], {m, -1000, 1000}, {n, -1000, 1000}]

and I have to manually change the intervals for $m = n = [-t, t]$ in order to see how the value of the sum changes. This is very inefficient!

What I would like to do is evaluate this sum for a range of $t$'s and plot the results as a graph against $t$. Say if $t$ starts at $t = 2$, the sum gets evaluated, then $t$ gets doubled, the sum gets evaluated again,...and so on,.., and then finally plotting the results. How can I do this in Mathematica? I can do it simply in Matlab with a for loop but I'm trying to learn Mathematica as it seems it could be better for certain computations.


2 Answers 2


Is this not what you want ?

Define a function of t

sum[t_] := Sum[
  If[m^2 + n^2 != 0, 1./(m^2 + n^2), 1], {m, -t, t}, {n, -t, t}]

Create a Table of sums for a range of t values.

sums = Table[{t, sum[t]}, {t, 2, 100, 2}]

Plot the data


enter image description here

Note: Your If statement was incomplete. I edited it.


Just another way:

f[t_] := Total[1/((#.# & /@ Tuples[Range[-t, t], 2]) /. {0 -> 1})]
DiscretePlot[f[t], {t, 2, 100}]

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.