I have the following code:
out = {};
f[x_, y_, z_] := f[x, y, z] = x^3 + y^3 + z^3
Do[SeedRandom[];
x = RandomInteger[{10^2, 10^3}];
y = -RandomInteger[{10^2, 10^3}];
z = RandomChoice[{-1, 1}] RandomInteger[{10^2, 10^3}];
sol = f[x, y, z];
If[3 <= sol <= 1000,{AppendTo[out, {sol, {x,y,z}}],Export["new_k.dat", out, "Table"], Continue[]}, Continue[]],10^6];
out
which does a conceptually simple task: it randomly choses a triple {x,y,z}
and computes sol
as the sum of cubes. If sol
is small enough, I want it to AppendTo
to a predefined list out
and Export
that list to a file. The code is supposed to run a given number of iterations regardless of the number of sol
's found (in fact, there will be very few instances that pass my criteria).
The problem is that even with the 10^6
iterations it uses a few Gb of memory. When I wanted to do 10^7
, my computer crashed.
The procedure is rather straightforward: it draws a few numbers, do some algebra and comparison, and IF it fulfills the condition, it gets to be stored in out
. So I expected it will use almost none memory, as I don't need it to remember all the instances when sol
didn't meet the condition. It goes like numbers-condition-store or not-if not then don't remember anything-continue for a given number of iterations.
My intention in writing this in such a way is to run it for e.g. 10^10
iterations, go away and see in the file new_k.txt
after a few hours if maybe some triple {x,y,z,}
was found without interferring with the still running computation. Writing directly to a file is in case of power shortage or something.
(The range of {x,y,z}
is intended to be bit larger, those above are just for the code to give an output sometimes)
f
? (f[x_, y_, z_] := f[x, y, z] = ...
). It's probably eating up your memory. (Note there are only about 6 billion combinations ofx
,y
,z
, so it makes more sense to run through them sequentially instead randomly, if you're contemplating 10^10 iteration.) $\endgroup$