Monday, July 19, 2010

Infinite list of primes! Yay!

or until the memory gives up...


# -*- coding: utf-8 -*-

from itertools import count
from collections import defaultdict

def seive():
table = defaultdict(list)
for x in count(2):
facts = table[x]
if facts:
del table[x]
for p in facts:
table[x+p] = table[x+p] + [p]
else:
table[x*x] = [x]
yield x

if __name__ == '__main__':
for k, v in enumerate(seive()):
print k, v


Got the idea from here.