DU AI Practical File
DU AI Practical File
BHASKARACHARYA COLLEGE OF
APPLIED SCIENCES
SEMESTER – 6
ARTIFICIAL
INTELLIGENCE
Practical File
Rohit Kumar
2102052
1. Write a Prolog program to calculate the sum of two numbers.
sum(A, B, C):-
C is A + B.
Output:
?- sum(4, 5, S).
S = 9.
Output:
?- max(5, 2, M).
M = 5.
factorial(0, 1):- !.
factorial(N, F):-
N > 0,
N1 is N - 1,
factorial(N1, F1),
F is N * F1.
Page 1 of 10
Output:
?- factorial(1, F).
F = 1.
?- factorial(5, F).
F = 120.
?- factorial(-5, F).
false.
?- factorial(10, F).
F = 3628800.
fib(1, 0):- !.
fib(2, 1):- !.
fib(N, T):-
N > 2,
N1 is N - 1,
N2 is N1 - 1,
fib(N1, T1),
fib(N2, T2),
T is T1 + T2.
Output:
?- fib(1, T).
T = 0.
?- fib(2, T).
T = 1.
?- fib(4, T).
T = 2.
?- fib(10, T).
T = 34.
?- fib(-1, T).
false.
Page 2 of 10
5. Write a Prolog program to implement GCD of two numbers.
gcd(0, A, A):- !.
gcd(A, 0, A):- !.
gcd(A, B, C):-
B1 is mod(A, B),
gcd(B, B1, C).
Output:
?- gcd(15, 25, C).
C = 5.
?- gcd(12, 0, C).
C = 12.
power(X, 0, 1):- !.
power(Num, Pow, Ans):-
Ans is Num^Pow.
Output:
?- power(10, 3, Ans).
Ans = 1000.
?- power(5, 6, Ans).
Ans = 15625.
?- power(11, 0, Ans).
Ans = 1.
Page 3 of 10
7. Write a Prolog program to implement multi(N1, N2, R) : where
N1 and N2 denotes the numbers to be multiplied and R
represents the result.
Output:
?- multi(11, 22, R).
R = 242.
?- multi(7, 0, R).
R = 0.
memb(X, [X | Tail]).
memb(X, [Head | Tail]):-
memb(X, Tail).
Output:
?- memb(b, [a, b, c]).
true .
Page 4 of 10
9. Write a Prolog program to implement conc(L1, L2, L3) where
L2 is the list to be appended with L1 to get the resulted list L3.
conc([], L, L).
conc([X | L1], L2, [X | L3]):-
conc(L1, L2, L3).
Output:
?- conc([a, b, c], [1, 2, 3], L).
L = [a, b, c, 1, 2, 3].
conc([], L, L).
conc([X|L1], L2, [X|L3]):-
conc(L1, L2, L3).
reverse([], []).
reverse([Head|Tail], R):-
reverse(Tail, L1),
conc(L1, [Head], R).
Output:
Page 5 of 10
?- reverse([], R).
R = [].
conc([], L, L).
conc([X|L1], L2, [X|L3]):-
conc(L1, L2, L3).
palindrome([]):- !.
palindrome([_]):- !.
palindrome(L):-
conc([Head|Tail], [Head], L),
palindrome(Tail), !.
Output:
?- palindrome([]).
true.
?- palindrome([a]).
true.
?- palindrome([a, b, a]).
true.
?- palindrome([a, b, b]).
false.
Page 6 of 10
sumList([], 0).
sumList([Head|Tail], S):-
sumList(Tail, X),
S is Head + X.
Output:
?- sumList([1], S).
S = 1.
?- sumList([], S).
S = 0.
evenlength([]):- !.
evenlength([_|T]):- oddlength(T).
oddlength([_]):- !.
oddlength([_|T]):- evenlength(T).
Output:
?- evenlength([]).
true.
?- oddlength([1]).
true.
?- oddlength([1, 2, 3, 4]).
false.
?- evenlength([1, 2, 3, 4]).
true.
Page 7 of 10
14. Write a Prolog program to implement nth_element(N, L, X)
where N is the desired position, L is a list and X represents the
Nth element of L.
Output:
?- nth_element(1, [a, b, c, d, e, f], X).
X = a.
maxlist([H], H):- !.
maxlist([H|T], M):-
maxlist(T, M1),
max(H, M1, M).
Output:
?- maxlist([1, 2, 3, 4, 5], M).
M = 5.
?- maxlist([1], M).
Page 8 of 10
M = 1.
?- maxlist([], M).
false.
insert_nth(I, 1, L, [I|L]):- !.
insert_nth(I, N, [H|T], [H|T1]):-
N1 is N - 1,
insert_nth(I, N1, T, T1).
Output:
?- insert_nth(2, 2, [1,3,4,5], R).
R = [1, 2, 3, 4, 5].
?- insert_nth(20, 5, [23, 535, 55, 34, 56, 778, 67, 97], R).
R = [23, 535, 55, 34, 20, 56, 778, 67, 97].
?- insert_nth(25, 15, [23, 535, 55, 34, 56, 778, 67, 97], R).
false.
Page 9 of 10
Output:
?- delete_nth(2, [1, 2, 3, 4, 5], R).
R = [1, 3, 4, 5].
?- delete_nth(5, [23, 535, 55, 34, 20, 56, 778, 67, 97], R).
R = [23, 535, 55, 34, 56, 778, 67, 97].
?- delete_nth(15, [23, 535, 55, 34, 20, 56, 778, 67, 97], R).
false.
Output:
?- merge([1, 3, 5, 7], [2, 4, 6, 8], L).
L = [1, 2, 3, 4, 5, 6, 7, 8].
Page 10 of 10