I have read (58520), but the solutions there do not work when using a large maximum value because they rely on Range
. For example, ByteCount[Range[10^9]]
yields 8000000144
(8GB). Plus, I would be repeating that operation several thousand times, so memory efficiency is necessary.
If the set of primes only consists of one prime, I could use the following:
f[max_, prime_] := Flatten[Range[Range[prime - 1], max, prime]]
This solution does not generate all numbers from 1
to max
, thus saving some memory (though not much because the resulting list is only about 1 / prime
shorter than Range[max]
).
I have created the following solution, but it has a problem:
f[max_, primes_List] :=
Block[{y = Range[Min[max, Times @@ primes]]},
Scan[(y[[# ;; ;; #]] = Nothing) &, primes];
If[max > Times @@ primes, Flatten[Range[y, max, Times @@ primes]],
y]];
The problem is that it may attempt to generate Range[max]
, which is undesirable.
What are some memory efficient ways to create a list of integers that are not divisible by a set of primes?
fnHybrid
fails. $\endgroup$f[10^10, Prime[Range[30]]
fails. The output should be a list of length1147700420
. $\endgroup$fnHybrid[1*^9, 30]
(my function, not yours) returns a list of 114,768,524 numbers in about 15 seconds on my system, but it does take about 12GB RAM. I'll see if I can reduce that. $\endgroup$