Created
November 13, 2013 10:56
-
-
Save phalt/7447202 to your computer and use it in GitHub Desktop.
Fibonacci sequence with a generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fib(n): | |
a, b = 1, 0 | |
while a < n: | |
yield a | |
a, b = b, a+b | |
# example: | |
''' | |
The 'yield' key word allows this function to be iterable. | |
Doesn't build up a list in memory, returns the 'next' one along. | |
''' | |
for n in fib(100): | |
print n | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment