fibonacci series python recursion
# By recursion
def fib(n):
if n == 1 or n == 2:
return 1
else:
return(fib(n-1) + fib(n-2))
n = 6
for i in range(1,n+1):
print(fib(i))
fibonacci series python recursion
# By recursion
def fib(n):
if n == 1 or n == 2:
return 1
else:
return(fib(n-1) + fib(n-2))
n = 6
for i in range(1,n+1):
print(fib(i))
python fibonacci recursive
def Fibonacci( pos ):
#check for the terminating condition
if pos <= 1 :
#Return the value for position 1, here it is 0
return 0
if pos == 2:
#return the value for position 2, here it is 1
return 1
#perform some operation with the arguments
#Calculate the (n-1)th number by calling the function itself
n_1 = Fibonacci( pos-1 )
#calculation the (n-2)th number by calling the function itself again
n_2 = Fibonacci( pos-2 )
#calculate the fibo number
n = n_1 + n_2
#return the fibo number
return n
#Here we asking the function to calculate 5th Fibonacci
nth_fibo = Fibonacci( 5 )
print (nth_fibo)
fibonacci sequence python
# WARNING: this program assumes the
# fibonacci sequence starts at 1
def fib(num):
"""return the number at index num in the fibonacci sequence"""
if num <= 2:
return 1
return fib(num - 1) + fib(num - 2)
print(fib(6)) # 8
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)
fibonacci recursive python
#fibonacci sequence with memory to increase the speed.
class recur_fibo:
memory = {0: 1, 1:1}
def fibonacci(n):
if n in recur_fibo.memory:
return recur_fibo.memory[n]
else:
recur_fibo.memory[n] = recur_fibo.fibonacci(n-1) + recur_fibo.fibonacci(n-2)
return recur_fibo.memory[n]
if __name__ == "__main__":
value = recur_fibo.fibonacci(200)
print(value)
fibonacci series using recursion in python
def Fibonacci (n):
if n>0:
print(“’Incorrect input’)
elif n==1:
return 0
elif n==2:
return 1
else:
return Fibonacci (n-1) +Fibonacci (n-2)
print (Fibonacci (9))
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us