Académique Documents
Professionnel Documents
Culture Documents
UNIVERSIDAD
DE CANTABRIA
1. Introduccin
2. Datos y expresiones. Especificacin de algoritmos
3. Estructuras algortmicas bsicas
4. Iteracin y recursin
5. Iteracin y recursin sobre secuencias
6. Iteracin y recursin sobre tablas
DEPARTAMENTO DE MATEMTICAS,
ESTADSTICA
Y COMPUTACIN
4
Notas:
UNIVERSIDAD
DE CANTABRIA
1. Introduccin
2. Datos y expresiones. Especificacin de algoritmos
3. Estructuras algortmicas bsicas
4. Iteracin y recursin
Diseo iterativo. Instrucciones de bucle. Invariantes y cotas. Fases de un diseo iterativo. Ejemplos.
Recursin. Ejemplos. Correccin de la implementacin recursiva. Fases del diseo recursivo.
5. Iteracin y recursin sobre secuencias
6. Iteracin y recursin sobre tablas
DEPARTAMENTO DE MATEMTICAS,
ESTADSTICA Y COMPUTACIN
Diseo iterativo
UNIVERSIDAD
DE CANTABRIA
Ejemplo
UNIVERSIDAD
DE CANTABRIA
x x x
n 1x
----,
ln ( 1 + x ) = x ---- + ---- ---- + + ( 1 )
2 3 4
n
DEPARTAMENTO DE MATEMTICAS,
ESTADSTICA Y COMPUTACIN
1 < x 1
Especificacin
UNIVERSIDAD
DE CANTABRIA
DEPARTAMENTO DE MATEMTICAS,
ESTADSTICA Y COMPUTACIN
( 1 )
i=1
Diseo
i1
x
---- }
i
UNIVERSIDAD
DE CANTABRIA
UNIVERSIDAD
DE CANTABRIA
numerador
signo
log
1.2
0.2
0.2
1.2
0.2
0.04
-1
0.19999
1.2
1.2
0.2
0.2
3
4
0.008
0.0016
1
-1
0.17999
0.18266
DEPARTAMENTO DE MATEMTICAS,
ESTADSTICA Y COMPUTACIN
Invariantes
UNIVERSIDAD
DE CANTABRIA
UNIVERSIDAD
DE CANTABRIA
inicializaciones;
{se cumple el Invariante I}
mientras B hacer
{se cumplen I y B}
S;
{se cumple I}
fmientras
{se cumple I y no B}
{se cumple la Postcondicin}
DEPARTAMENTO DE MATEMTICAS,
ESTADSTICA Y COMPUTACIN
UNIVERSIDAD
DE CANTABRIA
Disear el invariante
para que indique el resultado que se obtendr a cada paso del
bucle
Determinar las inicializaciones para que se cumpla el invariante
Determinar la condicin de continuacin, que nos permite saber
cundo abandonamos el bucle
Determinar el cuerpo del bucle
comprobar que se sigue cumpliendo el invariante cada vez
Verificar que el bucle termina
Razonar sobre el estado obtenido a la terminacin
DEPARTAMENTO DE MATEMTICAS,
ESTADSTICA Y COMPUTACIN
10
UNIVERSIDAD
DE CANTABRIA
DEPARTAMENTO DE MATEMTICAS,
ESTADSTICA Y COMPUTACIN
11
Ejercicios propuestos
UNIVERSIDAD
DE CANTABRIA
2n + 1
x x x
n x
sin ( x ) = x ----- + ----- ----- + + ( 1 ) ----------------------,
3! 5! 7!
( 2n + 1 )!
DEPARTAMENTO DE MATEMTICAS,
ESTADSTICA Y COMPUTACIN
xR
12
UNIVERSIDAD
DE CANTABRIA
DEPARTAMENTO DE MATEMTICAS,
ESTADSTICA Y COMPUTACIN
13
UNIVERSIDAD
DE CANTABRIA
14
Ejemplo
UNIVERSIDAD
DE CANTABRIA
n! = 1 , n = 0
n! = 1 2 3 ( n 1 ) n, n 1
Especificacin
mtodo factorial (n:entero) retorna entero
{Pre: n>=0}
clculo del factorial
{Post: valor retornado=n!}
fmtodo
DEPARTAMENTO DE MATEMTICAS,
ESTADSTICA Y COMPUTACIN
15
UNIVERSIDAD
DE CANTABRIA
16
Estructura final
UNIVERSIDAD
DE CANTABRIA
17
UNIVERSIDAD
DE CANTABRIA
DEPARTAMENTO DE MATEMTICAS,
ESTADSTICA Y COMPUTACIN
18
Recursin
UNIVERSIDAD
DE CANTABRIA
DEPARTAMENTO DE MATEMTICAS,
ESTADSTICA Y COMPUTACIN
Ejemplos
19
UNIVERSIDAD
DE CANTABRIA
n! = 1 , n = 0
n! = 1 2 3 ( n 1 ) n, n 1
Definicin recursiva para el clculo del factorial de un nmero
natural
n! = 1 , n = 0
n! = n ( n 1 )!, n 1
La definicin es correcta pues el nmero de recursiones es finito
DEPARTAMENTO DE MATEMTICAS,
ESTADSTICA Y COMPUTACIN
20
UNIVERSIDAD
DE CANTABRIA
DEPARTAMENTO DE MATEMTICAS,
ESTADSTICA Y COMPUTACIN
Correccin de la implementacin
recursiva
21
UNIVERSIDAD
DE CANTABRIA
n! = n ( n 1 )!
n! = n ( n 1 ) ( n 2 )!
n! = n ( n 1 ) ( n 2 ) 2 1
DEPARTAMENTO DE MATEMTICAS,
ESTADSTICA Y COMPUTACIN
22
Correccin de la implementacin
recursiva
UNIVERSIDAD
DE CANTABRIA
DEPARTAMENTO DE MATEMTICAS,
ESTADSTICA Y COMPUTACIN
23
UNIVERSIDAD
DE CANTABRIA
DEPARTAMENTO DE MATEMTICAS,
ESTADSTICA Y COMPUTACIN
24
Ejemplos propuestos
UNIVERSIDAD
DE CANTABRIA
producto ( x, 0 ) = 0
producto ( x, y ) = producto ( x, y 1 ) + x,
DEPARTAMENTO DE MATEMTICAS,
ESTADSTICA Y COMPUTACIN
y>0
25
UNIVERSIDAD
DE CANTABRIA
producto ( x, 0 ) = 0
producto ( x, y ) = producto ( x, y 1 ) + x, y > 0 es impar
producto ( x, y ) = producto ( 2x, y 2 ), y > 0 es par
DEPARTAMENTO DE MATEMTICAS,
ESTADSTICA Y COMPUTACIN
26
UNIVERSIDAD
DE CANTABRIA
y = 0 mcd = x
y > 0 mcd = mcd ( y, x mod y )
DEPARTAMENTO DE MATEMTICAS,
ESTADSTICA Y COMPUTACIN
27
UNIVERSIDAD
DE CANTABRIA
DEPARTAMENTO DE MATEMTICAS,
ESTADSTICA Y COMPUTACIN
28
UNIVERSIDAD
DE CANTABRIA
Definicin iterativa
2
x x x
n 1x
----,
ln ( 1 + x, n ) = x ---- + ---- ---- + + ( 1 )
2 3 4
n
1 < x 1
Definicin recursiva
ln ( 1 + x, i, n ) = ( 1 )
i 1x
---- + ln ( 1 + x, i + 1, n ), 1 < x 1
i
ln ( 1 + x, i, n ) = ( 1 )
i 1x
----, i = n
i
ln ( 1 + x, n ) = ln ( 1 + x, 1, n )
DEPARTAMENTO DE MATEMTICAS,
ESTADSTICA Y COMPUTACIN
29
Especificacin
UNIVERSIDAD
DE CANTABRIA
DEPARTAMENTO DE MATEMTICAS,
ESTADSTICA Y COMPUTACIN
j=i
j1
x
---- }
j
30
UNIVERSIDAD
DE CANTABRIA
Ejemplo (cont.)
31
UNIVERSIDAD
DE CANTABRIA
32
Ejemplo correcto
UNIVERSIDAD
DE CANTABRIA
DEPARTAMENTO DE MATEMTICAS,
ESTADSTICA Y COMPUTACIN
33
Ejemplo correcto
UNIVERSIDAD
DE CANTABRIA
si i==n entonces
retorna trmino;
si no
retorna trmino+
logaritmo(y,i+1,n,signo,numerador);
fsi;
n
j=i
j1
x
---- }
j
34
y 1.2
y 1.2
y 1.2
i 1
i 2
i 3
i 4
n 4
n 4
n 4
n 4
sig -1
sig 1
sig -1
num 1.0
num 0.2
num 0.04
num 0.008
term 0.2
term -0.02
term 0.0026
term -4.0e-4
v. ret 0.18226
v. ret -0.0177
v. ret 0.00226
v. ret -4.0e-4
DEPARTAMENTO DE MATEMTICAS,
ESTADSTICA Y COMPUTACIN
UNIVERSIDAD
DE CANTABRIA
sig 1
35