Fibonacci Series
Wed May 25 2022 12:54:29 GMT+0000 (Coordinated Universal Time)
Saved by
@redpsych
# Function for nth fibonacci
# number - Space Optimisation
# Taking 1st two fibonacci numbers as 0 and 1
def fibonacci(n):
a = 0
b = 1
# Check is n is less
# than 0
if n < 0:
print("Incorrect input")
# Check is n is equal
# to 0
elif n == 0:
return 0
# Check if n is equal to 1
elif n == 1:
return b
else:
for i in range(1, n):
c = a + b
a = b
b = c
return b
# Driver Program
print(fibonacci(9))
# This code is contributed by Saket Modi
# Then corrected and improved by Himanshu Kanojiya
content_copyCOPY
Comments