Professional Documents
Culture Documents
AI Lab Work
AI Lab Work
Roll: CSE2016/006
Dept: CSE(Sec-A)
PaperCode:CS793C
Registration Number:
161170110055 of 2016-1017
Day-1
1) Write a Prolog program to compute the factorial of a given number.
2) Write a Prolog program to find whether a given number is odd or
even.
1 Experiment 1 CO1
Day-2
3) Write a Prolog program to find the GCD of a list of numbers.
4) Write a Prolog program to find the LCM of a list of numbers.
4 CO1
Day-3
6 5) Write a Prolog program to find the ‘K’th element from a list. CO2
6) Write a Prolog program to find the sum of two
numbers.
Day-4
7) Write a Prolog program to find the average of two numbers.
7 8) Write a Prolog program to generate all integers in a given range. CO2
Day-5
9) Write a Prolog program to count the number of vowels in a list of
characters (word).
10) Write a Prolog program to generate all permutations of a list of
elements.
11 CO2,CO3
Day-6
14 11) Write a Prolog program to sort a given list using merge sort.
12) Write a Prolog program to sort a given list using insertion sort. CO2,CO3
CO1
Day-7
13) Write a Prolog program to sort a given list using bubble sort.
14) Write a Prolog program for Tower of Hanoi.
17 CO1
CO1,CO2,CO3
Day-8
15) Write a Prolog program to decompose a list.
16) Write a Prolog program to increment one value of list element.
20 CO1,CO2,CO3
Day-9
22 17) Write a Prolog program to find nth term of fibonacci series. CO1,CO2,CO3
18) Write a Prolog program to determine whether a given list is
palindrome or not.
Day-12
23) Write a Prolog program to find whether the node of the graph is
connected or not.
Day-13
25) Write a prolog program to find the circumference and area of a
circle.
26) Write a prolog program to represent a family tree and write the
28 predicates for different CO1,CO2
Day-14
27) Write a prolog program to find the last element of a list.
28) Write a prolog program to find the size of a list.
30 CO1,CO2,CO3
Day-15
29) Write a prolog program to find the Fibonacci series upto N terms.
30) Explain Different Arithmetic Predicates of Prolog
32 . CO1,CO2,CO3
33 CO1,CO2,CO3
Day-16
32) Write a Prolog program to verifies if three numbers can be the edges
of a triangle.
34 CO1
Focusing on key AI technologies, such as machine learning, natural language processing, and robotics, AI
will help to understand the implications of these new technologies for business strategy, as well as the
economic and societal issues they raise. How artificial intelligence will complement and strengthen our
workforce rather than just eliminate jobs. Additionally, the program will emphasize how the collective
intelligence of people and computers together can solve business problems that not long ago were
considered impossible.
5 ?- fact(6,X).
X = 720
6 ?- odd_even(19).
odd number
true.
7 ?- odd_even(16).
even number
true
3 ?- gcd(12,18,X).
X=6.
[1] 12 ?- nth(2,[a,b,c,d],R).
R=c.
2 ?- start.
X= 26.
Y= |: 62.
Sum is 88
true.
1 ?- start.
X= 26
|: .
Y= |: 62
|: .
R is 44
true.
?- range(10,50,L).
L = [10, 11, 12, 13, 14, 15, 16, 17, 18|...] .
9) Write a Prolog program to count the number of vowels in a list of characters (word).
vowel(X):- member(X,[a,e,i,o,u]).
nr_vowel([],0).
nr_vowel([X|T],N):- vowel(X),nr_vowel(T,N1),N is N1+1,!.
nr_vowel([X|T],N):- nr_vowel(T,N).
[1] 13 ?- perm([1,2,3],P).
P = [1, 2, 3] .
11) Write a Prolog program to sort a given list using merge sort.
merge(A,[],A).
merge([],B,B).
merge([A|Ra],[B|Rb],[A|M]) :- A =< B, merge(Ra,[B|Rb],M).
merge([A|Ra],[B|Rb],[B|M]) :- A > B, merge([A|Ra],Rb,M).
mergesort([],[]). /* covers special case */
mergesort([A],[A]).
mergesort([A,B|R],S) :-split([A,B|R],L1,L2),mergesort(L1,S1),
mergesort(L2,S2),merge(S1,S2,S).
split([],[],[]).
split([A],[A],[]).
split([A,B|R],[A|Ra],[B|Rb]) :- split(R,Ra,Rb).
[1] 9 ?- mergesort([4,3,6,5,9,1,7],S).
S = [1, 3, 4, 5, 6, 7, 9] .
12) Write a Prolog program to sort a given list using insertion sort.
insert_sort(List,Sorted):-i_sort(List,[],Sorted).
i_sort([],Acc,Acc).
i_sort([H|T],Acc,Sorted):-insert(H,Acc,NAcc),i_sort(T,NAcc,Sorted).
insert(X,[Y|T],[Y|NT]):-X>Y,insert(X,T,NT).
insert(X,[Y|T],[X,Y|T]):-X=<Y.
insert(X,[],[X]).
[1] 10 ?- insert_sort([25,3,65,1,12],A).
A = [1, 3, 12, 25, 65] .
13) Write a Prolog program to sort a given list using bubble sort.
bubble_sort(List,Sorted):-b_sort(List,[],Sorted).
b_sort([],Acc,Acc).
b_sort([H|T],Acc,Sorted):-bubble(H,T,NT,Max),b_sort(NT,[Max|Acc],Sorted).
[1] 11 ?- bubble_sort([25,3,65,1,12],A).
A = [1, 3, 12, 25, 65] .
[1] 14 ?- move(3,left,right,center).
Move top disk from left to right
Move top disk from left to center
Move top disk from right to center
Move top disk from left to right
Move top disk from center to left
Move top disk from center to right
Move top disk from left to right
true .
[1] 17 ?- inc([24,26,35,68,99],X).
X = [25, 27, 36, 69, 100].
?- fib(6,X).
X=8.
18) Write a Prolog program to determine whether a given list is palindrome or not.
palindrome(A):-reverse(A,A).
?- palindrome([a,b,c,b,a]).
true.
?- palindrome([a,b,c,b,e]).
false.
join2(String1,String2,Newstring):-
name(String1,L1),name(String2,L2),append(L1,L2,Newlist),name(Newstring,Newlist).
join3(String1,String2,String3,Newstring):-join2(String1,String2,S),join2(S,String3,Newstring).
?- join2('Souvik',' Majumdar',S).
S = 'Souvik Majumdar'.
?- conc([a,b,c],[1,2,3],L).
L = [a, b, c, 1, 2, 3].
21) Write a Prolog program to check whether the element is member of list or not.
member(X,[X| ]).
member(X,[ |T]) :- member(X,T).
?- member(x,[w,x]).
true .
?- member(x,[]).
false.
22) Write a Prolog program to find before and after list value of particular element in list.
?- conc(Before,[may|After],[jan,feb,mar,apr,may,june,july,aug,sep,oct,nov,dec]).
Before = [jan, feb, mar, apr],
After = [june, july, aug, sep, oct, nov, dec] .
?- conc([a,b],[c,d],[a,b,c,d]).
true.
?- conc([a,b],[c,d],[a,b,a,c,d]).
false.
23) Write a Prolog program to find whether the node of the graph is connected or not.
Prolog fact :
edge(1,2).
edge(1,4).
edge(1,3).
edge(2,3).
edge(2,5).
edge(3,4).
edge(3,5).
edge(4,5).
connected(X,Y) :- edge(X,Y) ; edge(Y,X).
connected(X,Y) :- edge(X,Y).
connected(X,Y) :- edge(Y,X).
prodv([X],[Y],[R]):- R is X*Y.
prodv([H|T],[H1|T1],[R|R1]):- prodv(T,T1,R1),R is H*H1.
?- prodv([1,2,4],[3,2,3],R).
R = [3,4,12]
25) Write a prolog program to find the circumference and area of a circle.
3 ?- start.
Radius= 1.
Circumference is 6.28
Area is 3.14
true.
26) Write a prolog program to represent a family tree and write the predicates for different
relationships like father, mother, brother, sister, cousin, grandmother, grandfather etc.
parent(abraham,ismael).
parent(abraham,isaac).
parent(isaac,esau).
parent(isaac,iacob).
grandfather(B,N):- parent(B,P),parent(P,N).
?- grandfather(abraham,iacob).
yes
?- grandfather(abraham,Grandson).
Grandson = esau
Grandson = iacob
No
?- brother(esau,iacob).
Yes
descendent(X,Y):- parent(X,Y).
descendent(X,Y):- parent(X,Z),descendent(Z,Y).
?- descendent(abraham,esau).
yes
last([X],X).
last([H|T],R):- last(T,R).
?- last([a],R)
R=a
?-last([a,b,1,c],X).
X=c
?-last([a,b,[c,[d]]],X).
X=[c,[d]]
my_length([],0).
my_length([_|T],R):- my_length(T,R1),R is R1+1.
29) Write a prolog program to find the Fibonacci series upto N terms.
:- dynamic(stored/1).
memo(Goal) :-stored(Goal) -> true;
Goal, assertz(stored(Goal)).
2 ?- fib(7,X).
1, 1, 2,3,5,8,13,
X = 13.
?- X is 5+4, Y is 5-4.
X=9
Y=1
?- X is 5*4, Y is 2^3.
X = 20
Y=8
?- X is 234556^100.
Error 0:Arithmetic Overflow
?- X is 5/3, Y=5//3.
X=1.66666667
Y=1
?- X is sqrt(3),Y is 3^0.5.
X = 1.73205080756888
Y = 1.73205080756888
?- Y is 10^3 * (1+1) + 3.
Y = 2003
:- op(500,xfx,'is_parent').
:- op(500,xfx,'is_sibling_of').
X is_sibling_of Y :- Z is_parent X,
Z is_parent Y,
X \== Y.
:-op(500,xfx,'is_same_level_as').
:- op(500,xfx,'has_depth').
a has_depth 0 :- !.
Node has_depth D :- Mother is_parent Node,
Mother has_depth D1,
D is D1 + 1.
ht(Node,0) :- leaf(Node), !.
ht(Node,H) :- Node is_parent Child,
ht(Child,H1),
H is H1 +1.
max([],M,M).
max([X|R],M,A) :- (X > M -> max(R,X,A) ; max(R,M,A)).
?- h is_sibling_of S.
S=g ;
S=i ;
no
?- t has_depth D.
D=4
?- locate(n).
32) Write a Prolog program to verifies if three numbers can be the edges of a triangle.
?- start.
input a= 3.
input b= 4.
input c= 5.
These numbers are the edges of a triangle.
yes