-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibonacci.py
More file actions
36 lines (32 loc) · 774 Bytes
/
fibonacci.py
File metadata and controls
36 lines (32 loc) · 774 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
def fibonacci(n):
#make sure n is a positive integer
if isinstance(n, int)!=True or n<0:
raise ValueError("That is not a positive integer.")
#F0=0 and F1=1
elif n <= 1:
return n
#Fn=Fn-1 + Fn-2
second_last = 0
last = 1
index=1
while index<n:
current = second_last + last
second_last = last
last=current
index+=1
return current
# Using recursion
def fibonacci_rec(n):
if isinstance(n, int)!=True or n<0:
raise ValueError("That is not a positive integer.")
elif n <= 1:
return n
else:
return fibonacci_rec(n-1) + fibonacci_rec(n-2)
# Example for n=9, Fn=34
try:
print(fibonacci(9))
pass
except ValueError as ve:
print(ve)
pass