Stay up to date – embedded code automagically updates, each snippet and article has a feed
Join Siafoo Now
or
Learn More
Fibonacci Sequence Generator
0
| In Brief | Renders Fibonacci sequences, up to a given maximum value, using a generator function. |
| Language | Python |
# 's
1"""Fibonacci sequences using generators
2
3This program is part of "Dive Into Python", a free Python book for
4experienced programmers. Visit http://diveintopython.org/ for the
5latest version.
6"""
7
8__author__ = "Mark Pilgrim (mark@diveintopython.org)"
9__version__ = "$Revision: 1.2 $"
10__date__ = "$Date: 2004/05/05 21:57:19 $"
11__copyright__ = "Copyright (c) 2004 Mark Pilgrim"
12__license__ = "Python"
13
14def fibonacci(max):
15 a, b = 0, 1
16 while a < max:
17 yield a
18 a, b = b, a+b
19
20for n in fibonacci(1000):
21 print n,
Renders Fibonacci sequences, up to a given maximum value, using a generator function.
Add a Comment