Skip to content

Commit 5487695

Browse files
authored
Added fibonacci code
0 parents  commit 5487695

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

fibonacci.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def fib(n):
2+
if n < 0:
3+
raise ValueError("Input must be positive")
4+
5+
a = 0
6+
b = 1
7+
8+
# Check if n is equal to 0
9+
if n in (0, 1):
10+
return n
11+
12+
for i in range(1, n):
13+
c = a + b
14+
a = b
15+
b = c
16+
17+
return b

0 commit comments

Comments
 (0)