Skip to content

Instantly share code, notes, and snippets.

@qqpann
Last active July 18, 2017 08:19
Show Gist options
  • Save qqpann/ab473840c6dba3c01e39ee4bdb5962e4 to your computer and use it in GitHub Desktop.
Save qqpann/ab473840c6dba3c01e39ee4bdb5962e4 to your computer and use it in GitHub Desktop.
[Python] フィボナッチ数列をジェネレータで生成 ref: http://qiita.com/Qiuqiu/items/d0eddd1a804d3c66d5f3
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
yield a
a, b = b, b+a
>>> 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