I have the following code
def gen_primes():
D = {}
q = 2
while True:
if q not in D:
yield q
D[q * q] = [q]
else:
for p in D[q]:
D.setdefault(p + q, []).append(p)
del D[q]
q += 1
f = open("primes1.txt","w")
filen = 1
ran1 = 1
ran2 = 10000000
k = 1
for i in gen_primes():
if (k >= ran1) and (k <= ran2):
f.write(str(i) + "\n")
if k%1000000 == 0:
print k
k = k + 1
else:
ran1 = ran2 + 1
ran2 = ran2 + 10000000
f.close()
filen = filen + 1;
f = open("primes" + str(filen) + ".txt","w")
if k > 100000000:
break
f.close()
The prime generation algorithm is taken from Simple Prime Generator in Python
This program is giving memory error
Traceback (most recent call last):
File "C:\Python25\Projects\test.py", line 43, in <module>
for i in gen_primes():
File "C:\Python25\Projects\test.py", line 30, in gen_primes
D.setdefault(p + q, []).append(p)
MemoryError
I am trying to store consecutive 10,000,000 primes in one file.