Posteriormente a buscar en diversos repositorios y foros de internet al terminar hemos hallado la solución que te mostraremos ahora.
Ejemplo 1: función fibonacci recursiva de python
# Python program to display the Fibonacci sequencedefrecur_fibo(n):if n <=1:return n
else:return(recur_fibo(n-1)+ recur_fibo(n-2))
nterms =10# check if the number of terms is validif nterms <=0:print("Plese enter a positive integer")else:print("Fibonacci sequence:")for i inrange(nterms):print(recur_fibo(i))
Ejemplo 2: python fibonacci recursivo
defFibonacci( pos ):#check for the terminating conditionif pos <=1:#Return the value for position 1, here it is 0return0if pos ==2:#return the value for position 2, here it is 1return1#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 numberreturn n
#Here we asking the function to calculate 5th Fibonacci
nth_fibo = Fibonacci(5)print(nth_fibo)
Ejemplo 3: pitón recursiva de fibonacci
#fibonacci sequence with memory to increase the speed.classrecur_fibo:
memory =0:1,1:1deffibonacci(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)
valoraciones y comentarios
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)