Skip to content

Commit ac53431

Browse files
authored
Create fibonacci.py
using iterators to make it efficient
1 parent 51c4110 commit ac53431

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Diff for: fibonacci.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import Iterable
2+
3+
# 1, 1, 2, 3, 5, 8, 13, 21
4+
5+
def fibonacci(value: int) -> Iterable[int]:
6+
count: int = 1
7+
acc: int = 1
8+
prev: int = 0
9+
10+
yield 1
11+
12+
while count < value:
13+
new_value = prev + acc
14+
yield new_value
15+
prev = acc
16+
acc = new_value
17+
count += 1
18+
19+
20+
print(list(fibonacci(8)))
21+

0 commit comments

Comments
 (0)