[Edu-sig] Pythonic Math must include...
Scott David Daniels
Scott.Daniels at Acm.Org
Sun Jan 18 18:35:50 CET 2009
michel paul wrote:
> ... An interesting fact is that, except for 2 and 3, all primes are adjacent
> to a multiple of 6....
Having once been interested in prime pairs, I remember having written
a lot of code based on this "back in the day." Here is my version of
your generator:
def primes():
for x in -1, 2, 3:
yield x
gen = primes().next
for top in iter(gen, 3):
pass # skip useless tests (we skip all multiples of 2 or 3)
factors = []
# get pump ready for a 5
check = -1
limit = 3 * 3 - 2 # How far will 3 work as top prime?
factors = []
while True:
check += 6
if check >= limit: # move if this pair needs another factor
top = gen()
limit = top * top - 2 # limit for both candidates
factors.append(top)
for element in check, check + 2:
for factor in factors:
if element % factor == 0:
break
else:
yield element
--Scott David Daniels
Scott.Daniels at Acm.Org
More information about the Edu-sig
mailing list