Está en la página 1de 6

Nombre: Jorge Gustavo Banegas Melgar

Fecha: 30/10/18

Porcentaje terminado : 45%

Comentarios: En esta practica aprendi a implementar los ciclos anidados en prolog usando la
recursiviad

%Prolog
%Mostrar la tabla de multiplicar de n.

comenzar:- read(N), cicloI(N,1),!.

cicloI(N,I):- I > N,!.


cicloI(N,I):- cicloJ(N,I,1), I1 is I+1, cicloI(N,I1).

cicloJ(N,_,J):- J > N,!.


cicloJ(N,I,J):- write(I), write(' x '), write(J), write(' = '), P
is I*J, write(P), nl, J1 is J+1, cicloJ(N,I,J1).

%Mostrar la tabla de multiplicar con operandos iguales.

operandosIguales(N):- operandosIguales(N,1).
operandosIguales(N,I):- I > N,!.
operandosIguales(N,I):- operandosIguales2(N,I,1), I1 is I+1,
operandosIguales(N,I1).

operandosIguales2(N,_,J):- J > N,!.


operandosIguales2(N,I,J):- I=:=J, write(I), write(' x '),
write(J), write(' = '), P is I*J, write(P), nl, J1 is J+1,
operandosIguales2(N,I,J1),!.
operandosIguales2(N,I,J):- J1 is J+1, operandosIguales2(N,I,J1),!.
%Mostrar la tabla de multiplicar con operandos diferentes.

operandosDif(N):- operandosDif(N,1).
operandosDif(N,I):- I > N,!.
operandosDif(N,I):- operandosDif2(N,I,1), I1 is I+1,
operandosDif(N,I1).

operandosDif2(N,_,J):- J > N,!.


operandosDif2(N,I,J):- I=\=J, write(I), write(' x '), write(J),
write(' = '), P is I*J, write(P), nl, J1 is J+1,
operandosDif2(N,I,J1),!.
operandosDif2(N,I,J):- J1 is J+1, operandosDif2(N,I,J1),!.

%Mostrar la tabla de multiplicar con operandos pares.

operandosPares(N):- operandosPares(N,1).
operandosPares(N,I):-I > N,!.
operandosPares(N,I):- operandosPares2(N,I,1), I1 is I+1,
operandosPares(N,I1).

operandosPares2(N,_,J):- J > N,!.


operandosPares2(N,I,J):- I mod 2 =:= 0, J mod 2=:=0, write(I),
write(' x '), write(J), write(' = '), P is I*J, write(P), nl, J1
is J+1, operandosPares2(N,I,J1),!.
operandosPares2(N,I,J):- J1 is J+1, operandosPares2(N,I,J1),!.
%Mostrar la tabla de multiplicar con operandos impares.
operandosImpares(N):- operandosImpares(N,1).
operandosImpares(N,I):-I > N,!.
operandosImpares(N,I):- operandosImpares2(N,I,1), I1 is I+1,
operandosImpares(N,I1).

operandosImpares2(N,_,J):- J > N,!.


operandosImpares2(N,I,J):- I mod 2 =\= 0, J mod 2=\=0, write(I),
write(' x '), write(J), write(' = '), P is I*J, write(P), nl, J1
is J+1, operandosImpares2(N,I,J1),!.
operandosImpares2(N,I,J):- J1 is J+1, operandosImpares2(N,I,J1),!.

%Mostrar la tabla de multiplicar dónde ambos operandos no sean


pares o impares.

operandosDis(N):- operandosDis(N,1).
operandosDis(N,I):- I > N,!.
operandosDis(N,I):- operandosDis2(N,I,1), I1 is I+1,
operandosDis(N,I1).

operandosDis2(N,_,J):- J > N,!.


operandosDis2(N,I,J):- (I mod 2 =:= 0, J mod 2 =\= 0 ; J mod 2 =:=
0, I mod 2 =\= 0 ), write(I), write(' x '), write(J), write(' =
'), P is I*J, write(P), nl, J1 is J+1, operandosDis2(N,I,J1),!.
operandosDis2(N,I,J):- J1 is J+1, operandosDis2(N,I,J1).

%Mostrar la tabla de multiplicar con el primer operando ascendente


y el segundo operandor descendente.
operandosAsc(N):- operandosAsc(N,1).
operandosAsc(N,I):- I > N,!.
operandosAsc(N,I):- operandosAsc2(N,I,N), I1 is I+1,
operandosAsc(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 cicloI(n, i):


if i > n:
return
cicloJ(n,i,1)
cicloI(n,i+1)

'''Mostrar la tabla de multiplicar con operandos iguales.'''

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)

'''Mostrar la tabla de multiplicar con operandos diferentes'''


def operandosDif(n):
i = 1
while i <= n:
j = 1
while j<=n:
if i!=j:
print(i,' x ',j,' = ', i*j)
j += 1

i += 1

También podría gustarte