Shortest prime number program

Tim Hochberg tim.hochberg at ieee.org
Mon Feb 13 12:49:32 EST 2006


In the spirit of pointless pessimization and obfuscation I have crushed 
something very similar to Alex Martelli's eratosthenes function onto a 
single line. It's truly monstrous, but somewhat entertaining [Some 
preemptive linebreaks added]:

def short():import itertools as it;D={};g=D.get;return (
q for q in it.count(2) if
D.__setitem__(it.dropwhile(D.__contains__, 
xrange(g(q,q)+q,2**30,g(q,q))).next(),g(q,q))
or q not in D and D.pop(q,1))


I'm sure there's a lesson to this somewhere. Something like I need to 
find something better to do with my spare time.

-tim




More information about the Python-list mailing list