File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed
Roadmap/06 - RECURSIVIDAD/python Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change
1
+
2
+ # RECURSIVIDAD
3
+
4
+ # Imprimir (recursivamente) del 100 al 0
5
+
6
+ def conteo_regresivo (x : int ):
7
+ if (x >= 0 ):
8
+ print (x )
9
+ conteo_regresivo (x - 1 )
10
+
11
+ print ("Imprimiendo los números del 100 al 0 con recursividad..." )
12
+ conteo_regresivo (100 )
13
+
14
+ """
15
+ DIFICULTAD EXTRA (opcional):
16
+ """
17
+
18
+ print ("\n ---- 🌩 DIFICULTAD EXTRA 🌩 ----\n " )
19
+
20
+ # Factorial
21
+
22
+ def factorial (n : int ):
23
+ if (n >= 1 ):
24
+ return n * factorial (n - 1 )
25
+ elif (n == 0 ):
26
+ return 1 ;
27
+ else :
28
+ print ("WARNING! No se puede calcular el factorial de un número negativo." )
29
+
30
+ num = 9
31
+ print (f"{ num } ! = { factorial (num )} " )
32
+
33
+
34
+ # Fibonacci
35
+
36
+ def fibonacci (pos : int ):
37
+ if (pos <= 0 ):
38
+ print ("WARNING! La posición debe ser un entero positivo." )
39
+ return 0
40
+ elif (pos == 1 ):
41
+ return 0
42
+ elif (pos == 2 ):
43
+ return 1
44
+ else :
45
+ return fibonacci (pos - 1 ) + fibonacci (pos - 2 )
46
+
47
+
48
+ num = 30
49
+ print (f"número #{ num } de Fibonacci = { fibonacci (num )} " )
You can’t perform that action at this time.
0 commit comments