# [portland] sheepish primes on PPUG

kirby urner kirby.urner at gmail.com
Sun Apr 5 18:40:52 CEST 2009

```Yo, Pythonistas, Pythonoodlers et al --

here's one from over the cuckoo's nest (with some serious admiration
from my side, though I don't recommend doing it this way if you wanna
have friends read your Python, easier versions available):

>>> g = (lambda primes = []: \
(n for n in count(2) if \
(lambda n, primes: \
(n in primes if (primes and n<=primes[-1]) \
else (primes.append(n) or True if all(n%p
for p in primes if p <= sqrt(n)) \
else False) \
) \
)(n, primes) \
) \
)()
>>> next(g)
2
>>> next(g)
3
>>> next(g)
5
>>> next(g)
7
>>> next(g)
11
>>> [next(g) for i in range(30)]
[13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79,
83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149]

OMG right?  This isn't by me BTW, it's a thread we're enjoying on
edu-sig, python.org's e-list for weirdo esoterica (OK OK, python-dev
is weirder).

My thanks to Kay Schluehr , John Posner, Ed Cherlin, Gregor Lingl and
others (it's a long thread about "must have" generators, including
Fibonacci's, Mandelbrot's, Pascal's and of course Primes).

http://mail.python.org/pipermail/edu-sig/2009-April/009222.html

I handed this page off to Andre Roberge at Pycon 2009, consensus
easily reached in our BOF:

http://www.python.org/community/sigs/current/edu-sig/  (I'd been
maintaining, then froze it for svn chapter -- anyone know if they
decided between hg and bzr yet, been navel gazing).

Andre is a university president and physics teacher, more relevantly
author of Crunchy, a client-side thing that eats VLAM (very little
additional markup) to give Python students both a shell and editor
right in the browser, where what it "eats" are doctest enabled code
snippets embedded in VLAM-enabled Wikis (there's a MoinMoin spin-off
that does this).  Not to be confused with PyWhip, a Google App Engine
out of New Mexico I think it is (or Arizona, one of those).