I’m trying to implement the sieve of eratosthenes in python, however when trying to find all primes up to the sqare root of for instance 779695003923747564589111193840021 I get an error saying result of range() has too many items. My question is, how do I avoid this problem, if I instantiate the list with a while loop I will get an error saying I’m using too much memory (before it even starts to use the pagefile), the two are listed below:
Using range()
maxnum = 39312312323123123
primes = []
seq = []
i = 0
seq = range(2,maxnum)
for i in seq:
mul = i * seq
for j in mul:
try:
seq.remove(j)
except:
pass
primes.append(i)
print primes
Using while:
maxnum = 39312312323123123
primes = []
seq = []
i = 0
while i < maxnum:
seq.append(i)
i+=1
for i in seq:
mul = i * seq
for j in mul:
try:
seq.remove(j)
except:
pass
primes.append(i)
print primes
It’s a more complex algorithm, perhaps technically not counting as the sieve, but one approach is to not remove all multiples of a given prime at once, but queue the next multiple (along with the prime). This could be used in a generator implementation. The queue will still end up containing a lot of (multiples of) primes, but not as many as by building then filtering a list.
First few steps done manually, to show the principle…
Note – the queue isn’t a FIFO. You will always be extracting the tuples with the lowest first item, but new/replacement tuples don’t (usually) have the highest first item and (as with 6 above) there will be duplicates.
To handle the queue efficiently in Python, I suggest a dictionary (ie hashtable) keyed by the first item of the tuple. The data is a set of second item values (original primes).
As suggested elsewhere, test with small targets before trying for the big one. And don’t be too surprised if you fail. It may still be that you need too many heap-allocated large integers at one time (in the queue) to complete the solution.