File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
Roadmap/06 - RECURSIVIDAD/python Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 1+ # Recursividad
2+ # Crea una función recursiva que imprima números del 100 al 0
3+
4+
5+ def numbers (number : int = 100 ):
6+ if number >= 0 :
7+ print (number )
8+ numbers (number - 1 )
9+
10+
11+ # Dificultad extra
12+ # Utiliza el concepto de recursividad para:
13+ # Calcular el factorial de un número concreto (la funcion recibe ese número)
14+
15+
16+ def factorial (number : int ):
17+ if number == 1 :
18+ return 1
19+
20+ if number >= 2 :
21+ return number * factorial (number - 1 )
22+
23+
24+ # Calcular el valor de un elemento concreto (según su posición) en la
25+ # sucesión de Fibonacci (la función recibe la posición).
26+
27+
28+ def fibonacci (position : int ):
29+ if position == 0 :
30+ return 0
31+ if position == 1 :
32+ return 1
33+ if position >= 2 :
34+ return fibonacci (position - 2 ) + fibonacci (position - 1 )
35+
36+
37+ if __name__ == "__main__" :
38+ # numbers()
39+ print (factorial (5 ))
40+ print (fibonacci (15 ))
You can’t perform that action at this time.
0 commit comments