# How do I manage my memory better when using the Map function?

So I'm attempting to write a Mathematica program that prints a range of natural numbers as the sum of four cubes. I wrote the following Mathematica code to try this, but am running into a memory constraint.

SumOfFourCubesSolver[start_, end_, k_] := Block[
{
i = 0,
slimmedlist = {},
slimmedslimmedlist = {}
},
Share[
\$HistoryLength = 0;
ClearSystemCache[];
slimmedlist =
Select[Flatten[
Outer[List, Range[-k, k], Range[-k, k], Range[-k, k],
Range[-k, k]]^3, 3], Total[#] >= start && Total[#] <= end &];
slimmedslimmedlist =
DeleteDuplicates[slimmedlist, Total[#1] == Total[#2] &];
f[x_] := Module[{}, Print[Total[x], ":", CubeRoot[x]]; i++];
Map[f, slimmedslimmedlist];
Print[MemoryInUse[]];
];
Print[i];
];


I have tried replacing Module with Block, Clearing System Cache, and setting history length to 0, but nothing seems to work. Once the data sets gets larger, I usually get an error message saying "The current computation was aborted because there was insufficient memory available to complete the computation". I am not looking for an answer, but just a step in the right direction or some material I can read up on to fix this myself. Below this is the output when the k value is 2, start is 1, and end is 300:

7:{-2,-1,2,2}

1:{-2,0,1,2}

8:{-2,0,2,2}

2:{-2,1,1,2}

9:{-2,1,2,2}

16:{-2,2,2,2}

5:{-1,-1,-1,2}

6:{-1,-1,0,2}

14:{-1,-1,2,2}

15:{-1,0,2,2}

23:{-1,2,2,2}

3:{0,1,1,1}

10:{0,1,1,2}

17:{0,1,2,2}

24:{0,2,2,2}

4:{1,1,1,1}

11:{1,1,1,2}

18:{1,1,2,2}

25:{1,2,2,2}

32:{2,2,2,2}

43664064

20

I have tried using a k value of 50, which crashes my computer, and with a k value of higher, say 150, I get the error message as stated above. Any help is much appreciated. Thanks.

• Perhaps a start would be to use Scan instead of Map. – Chip Hurst May 2 '15 at 22:53
• Welcome to Mathematica.SE! I suggest that: 1) You take the introductory Tour now! 2) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! 3) As you receive help, try to give it too, by answering questions in your area of expertise. – bbgodfrey May 2 '15 at 23:17

This ran fine on a beat-up old loungebook with k=100...

(BTW - this does not directly answer the "How do I fix this problem doing things this way..." aspect - sometimes the answer is just "...don't do it that way..." when there's a more direct path to the desired results.)

k = 100;
size = 4;
start = 1;
end = 300;
howmany = 1;

If[howmany =!= All, Off[IntegerPartitions::take]];

result = Table[{n,
Surd[IntegerPartitions[n, {size}, Range[-k, k]^3, howmany],
3]}, {n, start, end}];

On[IntegerPartitions::take];


Results are in symbol result, a table of the n and corresponding values that when cubed and sum are n.

size is how many (4 in your case) per solution element. start and end are self-evident. howmany determines how many solutions to present (at most) per n - use All for all of them.

The On and Off enable/suppress the message generated when the requested number cannot be found.

n.b. - If you find yourself interested in solutions of this type limited to positive elements, the built-in PowersRepresentations is your friend...