Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5afefc7

Browse filesBrowse files
committed
Update Fibonacci implementation and add recursive version
1 parent 64137af commit 5afefc7
Copy full SHA for 5afefc7

File tree

Expand file treeCollapse file tree

1 file changed

+20
-2
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+20
-2
lines changed
Open diff view settings
Collapse file

‎fibonacci.py‎

Copy file name to clipboard
+20-2Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
11
def fib(n):
22
prev = 0
3-
next = 0
3+
next = 1
44
if n == 1:
55
return 1
66
else:
7-
prev = prev + next
7+
new = prev + next
8+
for i in range(2, n+1):
9+
new = prev + next
10+
prev = next
11+
next = new
12+
return new
13+
14+
15+
def fib_rec(n):
16+
if n == 1:
17+
return 1
18+
elif n == 2:
19+
return 1
20+
else:
21+
return fib_rec(n-1) + fib_rec(n-2)
22+
# Testing the functions
23+
print("Fibonacci number at position 10 is: ", fib(10))
24+
print("Fibonacci number at position 10 is: ", fib_rec(10))
25+
826

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.