C Smith schrieb:
Hi Gregor,
I had done the same thing. I also noted that assigning (or inserting) an element into a list is faster than creating a new list: l.insert(0,2) is faster than l = [2]+l.
### def sieve (maximum): if maximum < 2: return [] limit = int(maximum**0.5) nums = range(1,maximum+1,2) nums[0] = None for p in nums: if p: if p > limit: break nums[(p*p)//2::p] = [False]*(1+(maximum//p- p)//2) nums[0] = 2 return filter(None, nums) ### /c
Well done!
Now it would be fine to have an *equally fast* infinite prime number generator.
Has anybody any suggestions?
Gregor
-- Gregor Lingl Reisnerstrasse 3/19 A-1030 Wien
Telefon: +43 1 713 33 98 Mobil: +43 664 140 35 27
Website: python4kids.net _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor