# פרולוג תרגיל 6

בסיס נתונים דינמי – תוך שימוש במה שצריך מתוך פרדיקטים, כגון: findall, bagof, setoff, assert, assertz, asserta, retract,

פיתרון 1

```memb([_ | L], Num, H) :- Num17 is Num - 1, memb(L, Num17, H).
memb([H | _], 1, H).

set([H | T], [H | Out]) :- not(member(H,T)), !, set(T,Out).
set([_ | T], Out) :-set(T,Out).
set([], []).

milga(Num, Grade, L) :- findall(G, grade(_,G), T), sort(T, H17), set(H17, H), length(H, Num17), Num34 is Num17 - Num + 1, memb(H, Num34, Grade), findall(Talmid, grade(Talmid, Grade), L17), Num51 is Num - 1, milga(Num51, _, L34), append(L17, L34, L), !.
%if Num=0 i don't know what Grade needs to be

div(_, _) :- fail. %it's because our prolog can't add a none existing predicate

divs(N, N) :- assert(div(N, N)), !.
divs(N, M) :- N - (N//M) * M =\= 0, !, M17 is M + 1, divs(N, M17).
divs(N, M) :- M17 is M + 1, assert(div(N, M)), divs(N, M17).

allDivs(Num, L) :- divs(Num, 1), findall(N, div(Num, N), L17), sort(L17, L34), reverse(L34, L).

a(_, _) :- fail. %it's because our prolog can't add a none existing predicate
max_min([], Min, Max) :- findall(Temp, bagof(Temp17, a(Temp17, _), Temp), L), sort(L, L17), L17 = [[Max | _] | _], last(L17, [Min | _]).
max_min([H | T], Min, Max) :- assert(a(H, H)), max_min(T, Min, Max).

?solve(member(X, [1,2])).
1.	failed - member(X, [1,2]) is not true.
2.	failed - member(X, [1,2]) is not (A,B).
3.	solve(member(X, [1,2])) :- clause(member(1, [1,2]), true), solve(true).
a.	solve(true) is true.
1
4.	solve(member(X, [1,2])) :- clause(member(X, [1,2]), member(X, [2])), solve(member(X, [2]).
.	failed - member(X, [2]) is not true
a.	failed - member(X, [2]) is not (A,B)
b.	solve member(X, [2]) :- clause(member (2,[2]), true), solve(true).
i.	solve(true) is true.
c.	solve member(X, [2]) :- clause(member(X, [2]), member (X,[])), solve(member(X,[]))
.	failed - member(X, []) is not true.
i.	failed - member(X, []) is not (A,B).
ii.	solve(member(X, [])) :- clause(member(1, []), failed), solve()
failed

```

H.s
פיתרון 2

```

% help predicats
maxlist([X|L],Max):-maxlist([X|L],X,Max).
maxlist([X|L],Y,Z):-X>Y,!,maxlist(L,X,Z).
maxlist([_|L],Y,Z):-maxlist(L,Y,Z),!.
maxlist([],Max,Max).
findsmall([X|_],Max,X):-X<Max,!.
findsmall([_|L],Max,X):-findsmall(L,Max,X).
nextsize(L,Max,Ns):-findsmall(L,Max,X),nextsize(L,Max,X,Ns).
nextsize([],_,Ns,Ns).
nextsize([X|L],Max,Ns,N):-X>Ns,X<Max,!,nextsize(L,Max,X,N).
nextsize([_|L],Max,Ns,N):-nextsize(L,Max,Ns,N).
findi(L,Num,I):-maxlist(L,Max),findi(L,Num,1,Max,I).
findi(_,Num,Num,I,I).
findi(L,Num,Num1,I,I2):-nextsize(L,I,I1),Num2 is Num1 +1,findi(L,Num,Num2,I1,I2).
%facts for 6.1
% 6.1
findall(Name,Goal,L).
%6.2

allDivs(Num,L):-divides(Num,Num),findall(X,divider(X,Num),L).
divides(_,0):-!.
divides(Num,Divider):-0 is Num mod Divider ,!,assert(divider(Divider,Num))
,Divider2 is Divider - 1,divides(Num,Divider2).
divides(Num,Divider):-Divider2 is Divider -1 ,divides(Num, Divider2).

%6.3
max_min(L,Y,X):-timesinlist(L),setof(Y,X^freq(X,Y),[Z|R]),
freq(X,Z),reverse(R,[Z2|_]),freq(Y,Z2).
timesinlist([Y|L]):-countAtom(X,[Y|L],R),assert(freq(Y,X)),timesinlist(R),!.
timesinlist([]).
countAtom(Num,[X|R],L):-countAtom(X,Num,[X|R],L).
countAtom(X,M,[X|L],R):-countAtom(X,N,L,R),M is N +1,!.
countAtom(X,M,[Y|L],[Y|R]):-countAtom(X,M,L,R),!.
countAtom(_,0,[],[]).

?- milga(4,G,L).
G = 87,
L = [sara, rivka, eli, sason] .

2 ?- allDivs(200,X).
X = [200, 100, 50, 40, 25, 20, 10, 8, 5|...].

3 ?- max_min([a,a,s,d,f,a,s,d,a,a,t,f],X,Y).
X = a,
Y = t .

```

Y.levental