print n fibonacci numbers in python code example
Example 1: python fibonacci get nth element
# Dynamic approach to get nth fibonacci number
arr = [0,1]
def fibonacci(n):
if n<0:
print("Fibbonacci can't be computed")
elif n<=len(arr):
return arr[n-1]
else:
temp = fibonacci(n-1)+fibonacci(n-2)
arr.append(temp)
return temp
print(fibonacci(9))
Example 2: fibonacci sequence python
num = 1
num1 = 0
num2 = 1
import time
for i in range(0, 10):
print(num)
num = num1 + num2
num1 = num2
num2 = num
time.sleep(1)
Example 3: fibonacci series in python
#Recursive Solutions
def fibo(n):
if n<=1: return 1
return fibo(n-1)+fibo(n-2)
fibo(5)
#OUTPUT:
#120