-
-
Save parzibyte/8456d046fa57f7ae450e2f7e4129e911 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
https://parzibyte.me/blog | |
""" | |
def fibonacci_iterativo(posicion, debe_imprimir): | |
actual = 0 | |
siguiente = 1 | |
for x in range(posicion + 1): | |
if debe_imprimir: | |
print(str(actual) + ",", end="") | |
temporal = actual | |
actual = siguiente | |
siguiente = siguiente + temporal | |
return temporal | |
def fibonacci_recursivo(posicion): | |
if posicion < 2: | |
return posicion | |
return fibonacci_recursivo(posicion - 1) + fibonacci_recursivo(posicion - 2) | |
posicion = 20 | |
# Imprimir sin importar el resultado | |
print(f"Imprimiendo serie hasta posición {posicion}") | |
fibonacci_iterativo(posicion, True) | |
# Obtener valor pero no imprimir | |
valor = fibonacci_iterativo(posicion, False) | |
print(f"\nFibonacci de {posicion} con método iterativo es {valor}") | |
# Lo mismo de arriba pero con el método iterativo | |
valor = fibonacci_recursivo(posicion) | |
print(f"Fibonacci de {posicion} con método recursivo es {valor}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment