[Tutor] Geometric sequence

Danny Yoo dyoo at hashcollision.org
Thu Oct 31 18:29:29 CET 2013


As an aside: It shouldn't be too bad to write a "generator" for the
geometric series, so that we can pick out the terms on-demand.

#################################
>>> def geometric(base):
...     x = 1
...     while True:
...         yield x
...         x *= base
...
>>> twos = geometric(2)
#################################


'twos' is a representation of the geometric series; we can then pick out
the elements one at a time:


#################################
>>> twos.next()
1
>>> twos.next()
2
>>> twos.next()
4
>>> twos.next()
8
>>> twos.next()
16
>>> twos.next()
32
>>> twos.next()
64
#################################
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131031/9c014bb6/attachment.html>


More information about the Tutor mailing list