Last active
August 29, 2015 14:16
-
-
Save orblivion/057fc395e4d87af97fba to your computer and use it in GitHub Desktop.
Recursive lazy fibonacci
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
from itertools import izip, islice | |
def fibg(): | |
"Generate an infinite amount of fibonacci numbers" | |
yield 1 | |
yield 1 | |
offset = fibg() | |
offset.next() | |
for x, y in izip(offset, fibg()): | |
yield x + y | |
def fib(n): | |
"Return a list of the first n elements of the fibonacci sequence" | |
return list(islice(fibg(), 0, n)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment