You are on page 1of 6

VISUAL PROLOG

Pr.1. Definiti un predicat care sa calculeze suma primelor n numere naturale nenule.

suma(1,1)
suma(N,V):-
N1 is N-1,
suma(N1,V1),
V is N+V1.

Pr.2. Sa se verifice daca un numar este sau nu numar prim.

prim(2).
prim(3).

prim(P):-
integer(P),
P>3,
P mod 2\==0,
\+ divide(P,3).
divide(N,L):-N mod L=:=0.
divide(N,L):-
L*L<N,
L2 is L+2,
divide(N,L2).

Interogari:
prim(13) .
yes
prim(25) .
no
prim(59) .
yes

Pr.3. Sa se afiseze lista de nr prime dintr-un interval [a,b]

1
prim(2).
prim(3).

prim(P) :- integer(P) ,
P>3,
P mod 2 =\= 0,
\+ divide(P,3) .
divide(N,L) :- N mod L =:= 0.
divide(N,L) :- L * L < N,
L2 is L + 2,
divide(N,L2).
% prime_list(A,B,L) :- L este lista de nr pare P, unde A <= P <= B
prime_list(A,B,L) :- A =< 2, !, p_list(2,B,L).
prime_list(A,B,L) :- A1 is (A // 2) * 2 + 1, p_list(A1,B,L).
p_list(A,B,[]) :- A > B, !.
p_list(A,B,[A|L]) :- prim(A), !,
next(A,A1),
p_list(A1,B,L).
p_list(A,B,L) :- next(A,A1), p_list(A1,B,L).

next(2,3) :- !.
next(A,A1) :- A1 is A + 2.

Interogari:
prime_list(6,200,L) .
L = [7,11,13,17,19,23,29,31,37,41|...] ?
prime_list(1,24,L) .
L = [2,3,5,7,11,13,17,19,23] ?

Pr.4. Rezolvarea ecuatiei de gradul al doilea

start:- citire(A,B,C),
rezolvare(A,B,C),!,
continuare.

% citim de la tastatura coeficientii


citire(A,B,C):-write('Dati coeficientii:'),nl,
write('A='),read(A),nl,
write('B='),read(B),nl,
write('C='),read(C),nl.
% calculam delta
delta(A,B,C,R):-D is (B*B-4*A*C),
D>=0,R is (sqrt(D)).
%rezolvarea ecuatiei

2
%A diferit de 0
rezolvare(A,B,C):-A=\=0,delta(A,B,C,R),R>=0,
X1 is (-B+R)/(2*A), X2 is (-B-R)/(2*A),
write('Solutiile ecuatiei sunt '),
write('x1='),write(X1),write(' x2='),write(X2),nl.
%A egal cu 0
rezolvare(A,B,C):-A=0,X is (-C/B),
write('Solutia ecuatiei de gradul I este:'),
write(X),nl.
rezolvare(A,B,C):-write('Ecuatia nu are solutie.'),nl.
continuare:-write('Doriti sa continuam(da,yes)?='),
read(X),
(X=da;X=d;X=a;X=y;X=ye;X=yes),
start.

Pr. 5. Operatii cu multimi: reuniunea, intersectia si diferenta a doua multimi

member(X,[X|_]).
member(X,[_|T]):-member(X,T).

%verificam daca o lista este multime


set([]).
set([X|T]):-not member(X,T), set(T).

% reuniunea a 2 multimi
union([],X,X):-!.
union([X|R],Y,Z):-member(X,Y), union(R,Y,Z),!.
union([X|R],Y,[X|Z]):-union(R,Y,Z).

%intersectia a 2 multimi
intersect([],X,[]):-!.
intersect([X|R],Y,[X|T]):-member(X,Y), intersect(R,Y,T), !.
intersect([X|R],Y,L):-intersect(R,Y,L).

%diferenta dintre 2 multimi


dif([],X,[]).
dif([H|T],X,D):-member(H,X),dif(T,X,D),!.
dif([H|T],X,[H|D]):-dif(T,X,D).

Interogari:

3
union([a,b,c],[a,c,d,e],U).
U = ["b","a","c","d","e"].

1 Solution

dif([a,b,c],[a,c,d,e],U).
U = ["b"].

1 Solution

Pr. 6. Sa se defineasca predicatele and, or, nand, nor, xor, impl si equ (echivalenta).
Sa se defineasca predicatul table care construieste tabela de adevar pentru o expresie logica data.

not(A) :- \+ A .
and(A,B) :- A, B.
or(A,_) :- A.
or(_,B) :- B.
equ(A,B) :- or( and(A,B), and( not(A),not(B) ) ).
xor(A,B) :- not(equ(A,B)).
nor(A,B) :- not(or(A,B)).
nand(A,B) :- not(and(A,B)).
impl(A,B) :- or(not(A),B).

% bind(X) da valori de adevar variabilei X


bind(true).
bind(fail).

table(A,B,Expr) :- bind(A), bind(B), do(A,B,Expr), fail.


do(A,B,_) :- write(A), write(' '), write(B), write(' '), fail.
do(_,_,Expr) :- Expr, !, write(true), nl.
do(_,_,_) :- write(fail), nl.

Interogari:
table(A,B,and(A,or(A,B))).
true true true
true fail true
fail true fail
fail fail fail

table(A,B,and(A,B)) .
true true true
true fail fail
fail true fail
fail fail fail

Pr.7. Diagnostice medicale ale copilariei

4
%domains
% boala, indicatia, nume=simbol

%predicates
ipoteza(nume,boala).
simptom(nume,indicatia).

%clauses
simptom(andrei,febra).
simptom(andrei,urticarie).
simptom(andrei,durere_in_corp).
simptom(andrei,tuse).

ipoteza(Pacient,pojar):-
simptom(Pacient,febra),
simptom(Pacient,tuse),
simptom(Pacient,conjunctivita),
simptom(Pacient,guturai),
simptom(Pacient,urticarie).

ipoteza(Pacient,gripa):-
simptom(Pacient,febra),
simptom(Pacient,durere_de_cap),
simptom(Pacient,durere_in_corp),
simptom(Pacient,conjunctivita),
simptom(Pacient,frisoane),
simptom(Pacient,durere_in_gat),
simptom(Pacient,tuse),
simptom(Pacient,guturai).

ipoteza(Pacient,raceala):-
simptom(Pacient,durere_de_cap),
simptom(Pacient,stranut),
simptom(Pacient,durere_in_gat),
simptom(Pacient,tuse),
simptom(Pacient,guturai).

ipoteza(Pacient,oreion):-
simptom(Pacient,febra),
simptom(Pacient,ganglioni_inflamati).

ipoteza(Pacient,varicela):-
simptom(Pacient,febra),
simptom(Pacient,urticarie),
simptom(Pacient,durere_in_corp),
simptom(Pacient,tuse).

ipoteza(Pacient,tuse_convulsiva):-
simptom(Pacient,raceala),
simptom(Pacient,stranut),
simptom(Pacient,guturai).

Interogare:

5
ipoteza(Pacient,boala)
Pacient=andrei, boala=varicela
1 Solution

Pr.8. Trei prieteni au obtinut primul, al doilea si respectiv al treilea loc intr-un concurs.
Fiecare dintre ei au nume diferite, prefera un alt sport si au nationalitati diferite. Michael prefera
baschetul si a obtinut un loc mai bun decat americanul. Simon este israelian si a obtinut un loc
mai bun decat jucatorul de tenis. Jucatorul de cricket a obtinut primul loc. Sa se scrie un
program in Prolog care sa raspunda la urmatoarele intrebari: Cine este australian ? Ce sport
prefera Richard ?

p([[_,_,cricket,1],[_,_,_,2],[_,_,_,3]]).
prieteni(L) :- p(L),
member([michael,_,baschet,A],L),member([_,american,_,B],L),A<B,
member([simon,israelian,_,C],L),member([_,_,tenis,D],L),C<D,
member([richard,_,_,_],L),member([_,australian,_,_],L).
nat(X) :- prieteni(L),
member([X,australian,_,_],L).
sport(X) :- prieteni(L),
member([richard,_,X,_],L).