Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Fecha: 30/10/18
Comentarios: En esta practica aprendi a implementar los ciclos anidados en prolog usando la
recursiviad
%Prolog
%Mostrar la tabla de multiplicar de n.
operandosIguales(N):- operandosIguales(N,1).
operandosIguales(N,I):- I > N,!.
operandosIguales(N,I):- operandosIguales2(N,I,1), I1 is I+1,
operandosIguales(N,I1).
operandosDif(N):- operandosDif(N,1).
operandosDif(N,I):- I > N,!.
operandosDif(N,I):- operandosDif2(N,I,1), I1 is I+1,
operandosDif(N,I1).
operandosPares(N):- operandosPares(N,1).
operandosPares(N,I):-I > N,!.
operandosPares(N,I):- operandosPares2(N,I,1), I1 is I+1,
operandosPares(N,I1).
operandosDis(N):- operandosDis(N,1).
operandosDis(N,I):- I > N,!.
operandosDis(N,I):- operandosDis2(N,I,1), I1 is I+1,
operandosDis(N,I1).
operandosAsc2(_,_,0):-!.
operandosAsc2(N,I,J):- write(I), write(' x '), write(J), write('
= '), P is I*J, write(P), nl, J1 is J-1, operandosAsc2(N,I,J1).
% Iterativo Y recursivo (Python)
'''Mostrar la tabla de multiplicar de n.'''
def tabla(n):
i = 1
while i <= n:
j = 1
while j <= n:
print(i,' x ',j,' = ', i*j)
j += 1
i += 1
def cicloJ(n,i,j):
if j > n:
return
print(i,' x ',j,' = ', i*j)
cicloJ(n,i,j+1)
def operandosIguales(n):
i = 1
while i <= n:
j = 1
while j<=n:
if i==j:
print(i,' x ',j,' = ', i*j)
j += 1
i += 1
def operandosIgualesRec(n,i):
if i > n:
return
operandosIgualesRec2(n,i,j)
operandosIgualesRec(n,i+1)
def operandosIgualesRec2(n,i,j):
if j > n:
return
if i==j:
print(i,' x ',j,' = ', i*j)
operandosIgualesRec2(n,i,j+1)
i += 1