2
$\begingroup$

I have the following code a friend of mine gave me to compute the terms of a sum over a large number of terms. Mathematica can perform the computation in about 2 minutes or less for any $n \leqslant 963$, but as soon as $n \geqslant 964$, the computation stops after a couple of minutes and produces no output at all. Given that it can compute $n = 963$ in a little over 2 minutes, why does it struggle with $n = 964$? Is this a memory issue or an issue with the code? The code is given below (with $n = 964$):

n = 964;
AbsoluteTiming[
Total@(Times @@@ 
 Abs[{a, b, c}^-1 /. 
   FindInstance[{a + b + c == 0, a != 0, b != 0, 
     c != 0, -n <= a <= n, -n <= b <= n, -n <= c <= n}, {a, b, c},
     Integers, 3 n^2 - 3 n]]) // N]

I've tried giving the Wolfram Kernel the highest priority for memory in Task Manager but this has not helped. Is there a setting in Mathematica that I could change to resolve this issue? I am currently running Mathematics 10.3 on Windows, but my friend who is running the latest version of Mathematica on Linux says it can get an output for $n = 1000$ in a little over 2 minutes. Do I need to update my Mathematica or is this an OS issue, or something else altogether?

$\endgroup$
4
  • 1
    $\begingroup$ For n = 963 your code gave {42.3463, 14.3195}. For n = 964 the output is {41.8516, 14.3196}. For n = 1000 it yields {44.6575, 14.3229}. I'm using $Version 10.4.1 for Linux x86 (64-bit). So I guess it might be a hardware issue. You can check either roughly in the System Monitor (Task Manager in Windows? Not sure) how much memory it uses, or play with something like MaxMemoryUsed[] or so. $\endgroup$
    – corey979
    Nov 21 '16 at 19:51
  • 1
    $\begingroup$ Your description sounds like your MMA kernel may have quit. It can silently do that for unexplained reasons. Immediately after you get no output click on Evaluation on the menu bar and slide your mouse down to Quit Kernel and see whether Local is greyed out and does nothing when you click on that. If so then that explains what happened, but not why. After finishing n=1000 MaxMemoryUsed says it used 3 gigs of memory. $\endgroup$
    – Bill
    Nov 21 '16 at 19:59
  • $\begingroup$ Thanks for the suggestions. I think it may indeed be a hardware issue. Local was greyed out immediately after I got no output. Task Manager says that throughout the computation with $n=1000$ the memory doesn't exceed 2 GB. $\endgroup$
    – user363087
    Nov 21 '16 at 20:23
  • 2
    $\begingroup$ The grey means the kernel quit during the calculation. Can you come up with a dozen other compute intensive problems that use up to 3 gig of memory and take about the same amount of time and see if any of those also cause the kernel to quit? $\endgroup$
    – Bill
    Nov 22 '16 at 2:52
1
$\begingroup$

This for the moment is the fastest, at least it is on my machine:

n = 2000;
AbsoluteTiming[
 Sum[Sum[Abs[1/a] Abs[1/b] Abs[1/(b + a)],
    {b, Select[
      Range[Piecewise[{{-n, a > 0}, {-(n + a), a < 0}}], 
       Piecewise[{{n, a < 0}, {n - a, a > 0}}]], # != 
         0 && # != -a & ]}] // N,
  {a, Select[Range[-n, n], # != 0 & ]}]]

{162.751, 14.3696}

$\endgroup$
3
$\begingroup$

give this a try,

n = 1000;
Times @@@ (1/Abs[Append[ #, -Plus @@ #]] & /@ 
  Select[ Tuples[Cases[Range[-n, n], Except[0]], {2}] , 
   1 <= Abs[Plus @@ #] <= n  & ]) // Total // N // AbsoluteTiming

{48.3799, 14.3229}

I'd advise you take a good look and verify its doing what you want. I checked for a few small values of n and it gives the same results.

Edit: a loop version. This will be slower, but does not use big memory.

n = 2000;
Monitor[Times @@@ 
     Reap[Do[ 
        tup = {Floor[(i - 1)/(2 n + 1)] - n, 
          Mod[(i - 1), (2 n + 1)] - n};
        If[Times @@ tup != 0 && 1 <= Abs[Plus @@ tup] <= n, 
         Sow[1/Abs[Append[tup, -Plus @@ tup]]]],
        {i, (2 n + 1)^2 }]][[2, 1]] // Total // N // 
  AbsoluteTiming, {N[i/(2 n + 1)^2]}]
$\endgroup$
2
  • $\begingroup$ Thanks for this -- it gives me the same values as what I wanted for values higher than $n = 1000$. Unfortunately Mathematica can't produce an output for me with $n = 2000$, say, but it does perform better than the original. $\endgroup$
    – user363087
    Nov 21 '16 at 21:29
  • $\begingroup$ added a loop based version -- It never constructs full tuple lists and so should not be memory limited. $\endgroup$
    – george2079
    Nov 21 '16 at 22:18

Your Answer

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

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