why 'lambda' and 'reduce'?
Anton Vredegoor
anton at vredegoor.doge.nl
Sat Jun 14 10:00:50 EDT 2003
bokr at oz.net (Bengt Richter) wrote:
>I just thought to dig this up and turn it into a generator.
>Something to check against ;-)
<snip pigen.py>
Thanks for posting. After playing a while with it I noticed that the
new itertools for Python23 are nice for this too. The version below
doesn't use "n" and "k" in the generator and computes p and q in a
for-loop. Also d and d1 are dropped for the sake of clarity, at the
cost of computing just one extra a/b.
Anton
from sys import stdout
from itertools import count,islice,imap
def pq(x): return x*x,2*x+1
def pigen():
a,b,c,d = 12,4,76,24
for p,q in imap(pq,count(3)):
while a/b == c/d:
yield str(a/b)
a,c = a%b*10, c%d*10
a,b,c,d = c,d,p*a+q*c,p*b+q*d
def test():
ndigits = 60
map(stdout.write,islice(pigen(),ndigits))
print
if __name__=='__main__':
test()
More information about the Python-list
mailing list