Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Recursión
Motivación
Ejemplos
Pila de llamados
OBJETIVOS
Entender el concepto de recursividad.
Según la RAE
Volver una cosa al sitio dónde salió, retornar, repetirse, reaparecer.
En términos de programación
Un método es recursivo si directa o indirectamente realiza una llamada a si
mismo.
Diferente a la circularidad.
MOTIVACIÓN
Forma natural, lógica
n! = 1 * 2 * 3 * 4 …* (n-1)*n
1, si n = 0
n! =
n*(n-1)!, si n>=1
RECURSIVO VS. ITERATIVO
[FACTORIAL]
Factorial Iterativo Factorial Recursivo
Definición de función factorial
int factorial (int n){ int factorial (int n){
int f = 1; if n = 0
Fact (0) = 1
for (int i=2; i<=n; i ++) return 1;
Fact (n) = n * Fact (n-1)
{ else
f = f * i; return n*factorial(n-1);
} }
return f;
}
MOTIVACIÓN
Forma natural, lógica
//Pre: n>=0
int Fib (int n){
if (n <= 1)
return n;
fib(5)
fib(4) + fib(3)
(fib(3) + fib(2)) + (fib(2) + fib(1))
((fib(2) + fib(1)) + (fib(1) + fib(0))) + ((fib(1) + fib(0)) + fib(1))
(((fib(1) + fib(0)) + fib(1)) + (fib(1) + fib(0))) + ((fib(1) + fib(0)) + fib(1))
//PRE: n>=0
int Fib2I (int [] T; int n){
if (n == 0)
return 0;
if (n == 1)
return 1;
T[0] = 0;
T[1] = 1;
for (int i = 2; i <= n; i++){
T[i] = T[i-1] + T[i-2];
}
return T[n];
0 10 10 20 30 50 08 13
0 21
0 34
0
}
FIBONACCI – POSIBLES MEJORAS
Almacenar en una tabla los resultados obtenidos. De esta
forma, si queremos calcular fib(n) preguntaremos previamente
si dicho valor ya fue calculado.
Potencia (x, 0) = 1
Potencia (0, y) = 0
y veces
1
Potencia (x, y) con y < 0 =
Potencia( x, y )
EJEMPLO: POTENCIA
int potencia (int x, int y){
if (y = 0)
return 1;
else if (x = 0)
return 0;
else if (y > 0)
return x * potencia(x, y-1);
else // (y < 0)
return 1/potencia(x, -y);
}
PILA DE LLAMADOS – POTENCIA
(NÚMEROS POSITIVOS)
VARIABLES X = 2, Y = 3
VARIABLES X = 2, Y = 2
VARIABLES X = 2, Y = 1
VARIABLES X = 2, Y = 0
VARIABLES X = 2, Y = 1
VARIABLES X = 2, Y = 2
VARIABLES X = 2, Y = 3
2. Cada paso consiste en una nueva división, en la que el dividendo es el número que actuó de divisor en
la división anterior y el divisor es el número que se obtuvo como resto en la división anterior.
3. Cuando en una división se obtiene resto nulo, el máximo común divisor de los números de los que
partimos será el número que ha actuado como divisor en esa última división efectuada y que resultó ser
una división exacta.
public int MCDI(int a, int b){ public int MCDR(int a, int b){
int resto = a%b; int resto = a%b;
while (resto!=0){ if(resto==0){
a=b;
return b;
b=resto;
resto = a % b; }else{
} return MCDR(b, resto);
return b; }
}
}
EJERCICIO: ADOQUINES
Al principio del juego los niños se ubican en el adoquín numerado 0.
Se pide calcular el número de caminos posibles para alcanzar un adoquín objetivo numerado
con n (mayor que cero).
0 1 2 … N N+1
ADOQUINES
1 1 58
= 54 3 =
2 5+ 4
0 3+ 3
2 3
2 2 2
1
1 0 1
0 0
1 =33 6
2 + 1 3
+ 5=
2 8 5
1 4
0 3
2
1
0
EJERCICIO: ADOQUINES
adoquines (1) = 1;
adoquines (2) = 2;
if(n = 1)
return 1;
if(n = 2)
return 2;
CONTADOR DE ISLAS (2.11)
CONTADOR DE ISLAS (2.11)
boolean PerteneceAlMapa(int d1, int d2, int i, int j)
{
return ((i>=0) and (i<d1) and (j>=0) and (i<d2))
}
void Borrar(int m[],int i,int j){
m[i][j]=0;
}
void esTierra (int m[][], int i, int j){
return (m[i][j] == 1)
{
CONTADOR DE ISLAS (2.11)
0 1 2 3 4 5 6 7 8 9
0 0 0 0 1 1 0 0 0 1 0 cantIslas = 0
1 0 0 1 0 1 0 0 0 1 1
2 0 1 0 0 1 1 1 0 0 0
3 0 1 0 0 0 0 0 1 0 0
4 0 0 1 0 1 0 1 0 0 1
5 0 0 1 0 1 0 1 0 0 1
6 0 0 1 0 1 0 1 0 0 1
7 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