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

bennyprolog1 Comment

### פיתרון 1

1

```sum(N,RES1):-N>1,N1 is N-1,sum(N1,RES2), RES1 is RES2+N.
sum(1,RES):-RES is 1.
/*
3 ?- sum(5,L).
L = 15.

4 ?- sum(20,L).
L = 210.

5 ?- sum(1,L).
L = 1.
*/

sumDigits(0,0).
sumDigits(Num,Sum):-Num>0,N is mod(Num,10), N1 is Num//10,sumDigits(N1,N2),Sum is N+N2.
/*
10 ?- sumDigits(123,R).
R = 6 .

11 ?- sumDigits(12001,R).
R = 4 .

12 ?- sumDigits(9876,R).
R = 30 .

13 ?- sumDigits(7,R).
R = 7 .
*/

```

2

```
listDigits(Num,[N|L]):-Num>0, N is mod(Num,10), N1 is Num//10,listDigits(N1,L).
/*
19 ?- listDigits(1020,L).
L = [0, 2, 0, 1]

20 ?- listDigits(10454,L).
L = [4, 5, 4, 0, 1] .
*/

ב

num(R,L):-num(R,1,L).
num([],_,L):-L is 0.
num([X|RES],N,L1):-N1 is N*10,num(RES,N1,L),L1 is N*X+L.
/*
6 ?- num([1,2,3],L).
L = 321.

7 ?- num([1,2,3,4],L).
L = 4321.

8 ?- num([1,0],L).
L = 1.

9 ?- num([0,1,2,3],L).
L = 3210.

10 ?- num([1,2,3,4,0],L).
L = 4321.
*/

ג

my_reverse([],[]).
my_reverse([X|Rest],R):-my_reverse(Rest,Rest_Rev),append(Rest_Rev,[X],R).
revNum(L,R):-listDigits(L,K),my_reverse(K,S),num(S,R).
/*
57 ?- revNum(1,R).
R = 1 .

58 ?- revNum(1234,R).
R = 4321 .

59 ?-  revNum(0457565,R).
R = 565754 .

60 ?- revNum(0000,R).
R = 0
*/

```

3

```
my_member(X,[X|_]).
my_member(X,[_|Rest]):-my_member(X,Rest).

intersection([],_,[]).
intersection([X|L1],L2,[X|Z]):-my_member(X,L2),intersection(L1,L2,Z).
intersection([_|L1],L2,Z):-intersection(L1,L2,Z).
/*
26 ?- intersection([1,2,3],[1,2],S).
S = [1, 2] .

27 ?- intersection([1],[1],S).
S = [1] .

28 ?- intersection([1],[1,2],S).
S = [1] .

29 ?- intersection([6,1],[1,1,3,6],S).
S = [6, 1] .
*/
minus([],_,[]).
minus([X|L1],L2,Z):-my_member(X,L2),minus(L1,L2,Z).
minus([X|L1],L2,[X|Z]):-minus(L1,L2,Z).
/*
[2] 36 ?- minus([1,2,3],[1,2],Z).
Z = [3] .

[2] 37 ?- minus([1],[1],Z).
Z = [] .

[2] 38 ?- minus([1,2,3],[],Z).
Z = [1, 2, 3].

[2] 39 ?- minus([1,2,3,5,6,7],[3],Z).
Z = [1, 2, 5, 6, 7]
*/
```

### פיתרון 2

```sum(1,1).
sum(N,R):-M is N-1 , sum(M,S), R is S+(M+1).

sumDigits(Num,1):- Num =< 9 , Num >= 0.
sumDigits(Num,Sum):- M is Num // 10 , Num > 9 ,sumDigits(M, S), Sum is S+1.

numberToList(N,[N]):-N=<9, N>=0.
numberToList(N, R):- M is N mod 10, X is N //10 ,numberToList(X,S), append(S,[M],R).

listToNum(R,L):-listToNum(R,1,L).
listToNum([],_,L):-L is 0.
listToNum([X|RES],N,L1):-N1 is N*10,listToNum(RES,N1,L),L1 is N*X+L.

revNum(X,S):-numberToList(X,Y), listToNum(Y,S).

intersection([],_,[]).
intersection([X|L1],L2,[X|Z]):-member(X,L2),intersection(L1,L2,Z).
intersection([_|L1],L2,Z):-intersection(L1,L2,Z).

minus([],_,[]).
minus([X|L1],L2,Z):-member(X,L2),minus(L1,L2,Z).
minus([X|L1],L2,[X|Z]):-minus(L1,L2,Z).

דוגמאות הרצה

? sum(3,R)
R = 6

? sumDigits(1234,R).
R = 4

? numberToList(123,R).
R = [1, 2, 3]

? listToNum([1,2,3] ,R).
R = 321

? revNum(65465465461321321,R).
R = 12312316456456456

? intersection([1,2,3],[3,4],R).
R = [3]

? intersection([],[3,4],R).
R = []

? minus([],[3,4],R).
R = []

? minus([3,4],[3,4],R).
R = []

```

y.Levental

### פיתרון 3

```sum(0, 0).
sum(N, RES) :- N > 0, N17 is N - 1, sum(N17, RES17), RES is N + RES17.

sumDigits(Num, Num) :- Num < 10. sumDigits(Num, Sum) :- Num >= 10, Num17 is Num//10, sumDigits(Num17,Sum17), Sum is Sum17 + Num mod 10.

numbertolist(X, [X]) :- X < 10. numbertolist(X, L) :- X >= 10, X17 is (X//10), numbertolist(X17, L17), X34 is (X mod 10),append(L17,[X34],L).

listtonumber([], 0).
listtonumber([X17 | L], X) :- listtonumber(L, Y), Y17 is Y*10, X is X17 + Y17.

reverseDigits(Num, ReverseNum) :- numbertolist(Num,Numlist), listtonumber(Numlist, ReverseNum).

intersection([],_,[]).
intersection([X | L1], L2, Z) :- member(X, L2), intersection(L1, L2, Z17), Z = [X | Z17].
intersection([X | L1], L2, Z) :- not(member(X, L2)), intersection(L1, L2, Z).

minus([],_,[]).
minus([X | L1], L2, Z) :- member(X, L2), minus(L1, L2, Z).
minus([X | L1], L2, Z) :- not(member(X, L2)), minus(L1, L2, Z17), Z = [X | Z17].

subset([], _).
subset([X | S], [X | L]) :- subset(S, L).
subset(S, [_ | L]):- subset(S, L).

sumofalist([], 0).
sumofalist([X | L], Sum) :- sumofalist(L, Num), Sum is X + Num.

subSum(L, Sum, Sub) :- subset(Sub, L), sumofalist(Sub, Sum).

```

hod