Last active
July 18, 2017 08:19
-
-
Save qqpann/ab473840c6dba3c01e39ee4bdb5962e4 to your computer and use it in GitHub Desktop.
[Python] フィボナッチ数列をジェネレータで生成 ref: http://qiita.com/Qiuqiu/items/d0eddd1a804d3c66d5f3
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 fibonacci(n): | |
a, b = 0, 1 | |
for _ in range(n): | |
yield a | |
a, b = b, b+a |
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
>>> for i in fibonacci(10): print(i, end=' ') | |
... | |
0 1 1 2 3 5 8 13 21 34 | |
>>> [ i for i in fibonacci(15) ] | |
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment