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.
Scan
instead ofMap
. $\endgroup$