Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Name: Rashida Jahan

Roll: CSE2016/006

Dept: CSE(Sec-A)

Subject: Artificial Intelligence

PaperCode:CS793C

Registration Number:

161170110055 of 2016-1017

University Roll: 11700116058

Name: Rashida Jahan RollNo: CSE2016/006


Related
SL.No Name of the program COs

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.

Name: Rashida Jahan RollNo: CSE2016/006


Day-10
19) Write a Prolog program to Join two string or three string.
20) Write a Prolog program to concatenation of two list.
24 CO1
Day-11

21) Write a Prolog program to check whether the element is


member of list or not.
22) Write a Prolog program to find before and after list value of
particular element in list.
25 CO1

Day-12
23) Write a Prolog program to find whether the node of the graph is
connected or not.

24) Write a Prolog program to multiply two vectors.


26 CO1,CO2

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

Name: Rashida Jahan RollNo: CSE2016/006


31) Write a prolog program for below tree data relation.

32) Write a Prolog program to verifies if three numbers can be the edges
of a triangle.

34 CO1

Name: Rashida Jahan RollNo: CSE2016/006


Sample solutions for Lab Exercises

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.

1) Write a Prolog program to compute the factorial of a given number.


fact(0,1).
fact(X,Y):-X1 is X-1,fact(X1,Z),Y is X*Z.

5 ?- fact(6,X).
X = 720

2) Write a Prolog program to find whether a given number is odd or even.

odd_even(X):- X mod 2 =:= 0,


write('even number'),nl.
odd_even(X):- write('odd number'),nl.

6 ?- odd_even(19).
odd number
true.

7 ?- odd_even(16).
even number
true

3) Write a Prolog program to find the GCD of a list of numbers.


gcd(X, Y, G) :- X = Y, G = X.
gcd(X, Y, G) :- X < Y, Y1 is Y-X,gcd(X, Y1, G).
gcd(X, Y, G) :- X > Y, gcd(Y, X, G).

3 ?- gcd(12,18,X).
X=6.

4) Write a Prolog program to find the LCM of a list of numbers.


gcd2(X, Y, G) :- X = Y, G = X.
gcd2(X, Y, G) :- X < Y, Y1 is Y-X,gcd2(X, Y1, G).
gcd2(X, Y, G) :- X > Y, gcd(Y, X, G).
lcm(X,Y,LCM):-gcd2(X,Y,GCD), LCM is X*Y//GCD.

Name: Rashida Jahan RollNo: CSE2016/006


4 ?- lcm(12,18,X).
X = 36 .

5) Write a Prolog program to find the ‘K’th element from a list.


nth(0,[X|_],X).
nth(N,[_|T],R):- M is N-1,nth(M,T,R).

[1] 12 ?- nth(2,[a,b,c,d],R).
R=c.

6) Write a Prolog program to find the sum of two numbers.


start:- sum,nl.
sum:- write('X= '),read(X),write('Y= '),read(Y),S is X+Y,write('Sum is '),write(S).

2 ?- start.
X= 26.
Y= |: 62.
Sum is 88
true.

7) Write a Prolog program to find the average of two numbers.


start:- write('X= '),read(X),
write('Y= '),read(Y),
R is (X+Y)/2,
write('R is '),write(R),nl.

1 ?- start.
X= 26
|: .
Y= |: 62
|: .
R is 44
true.

8) Write a Prolog program to generate all integers in a given range.


range(I,I,[I]).
range(I,K,[I|L]) :- I < K, I1 is I + 1, range(I1,K,L).

?- 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).

Name: Rashida Jahan RollNo: CSE2016/006


[1] 8 ?- nr_vowel([a,r,e,d,i],X).
X = 3.

10) Write a Prolog program to generate all permutations of a list of elements.


takeout(X,[X|R],R).
takeout(X,[F|R],[F|S]) :- takeout(X,R,S).
perm([X|Y],Z) :- perm(Y,W), takeout(X,Z,W).
perm([],[]).

[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).

Name: Rashida Jahan RollNo: CSE2016/006


bubble(X,[],[],X).
bubble(X,[Y|T],[Y|NT],Max):-X>Y,bubble(X,T,NT,Max).
bubble(X,[Y|T],[X|NT],Max):-X=<Y,bubble(Y,T,NT,Max).

[1] 11 ?- bubble_sort([25,3,65,1,12],A).
A = [1, 3, 12, 25, 65] .

14) Write a Prolog program for Tower of Hanoi.


move(1,X,Y,_) :-
write('Move top disk from '),
write(X),
write(' to '),
write(Y),
nl.
move(N,X,Y,Z) :-
N>1,
M is N-1,
move(M,X,Z,Y),
move(1,X,Y,_),
move(M,Z,Y,X).

[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 .

15) Write a Prolog program to decompose a list.


writeall([]).
writeall([A|L]):- write(A),nl,writeall(L).

[1] 15 ?- writeall([alpha,'Thi is string',20,[a,b,c]]).


alpha
Thi is string
20
[a,b,c]
true.

16) Write a Prolog program to increment one value of list element.


inc([],[]).

Name: Rashida Jahan RollNo: CSE2016/006


inc([A|L],[A1|L1]):-A1 is A+1,inc(L,L1).

[1] 17 ?- inc([24,26,35,68,99],X).
X = [25, 27, 36, 69, 100].

17) Write a Prolog program to find nth term of fibonacci series.


fib(0,0).
fib(1,1).
fib(X, Ans) :- Xm1 is X-1 ,Xm2 is X-2 ,fib(Xm1,Sub1), fib(Xm2,Sub2),Ans is Sub1+Sub2.

?- 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.

19) Write a Prolog program to Join two string or three string.

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'.

?- join3('This is',' an',' example',Newstring).


Newstring = 'This is an example'.

20) Write a Prolog program to concatenation of two list.


conc([],L,L).
conc([I|L1],L2,[I|L3]) :- conc(L1,L2,L3).

?- conc([a,b,c],[1,2,3],L).
L = [a, b, c, 1, 2, 3].

?- conc(L1, L2, [1, 2, 3]).


L1 = [],
L2 = [1, 2, 3] ;
L1 = [1],
L2 = [2, 3] ;
L1 = [1, 2],
L2 = [3] ;

Name: Rashida Jahan RollNo: CSE2016/006


L1 = [1, 2, 3],
L2 = [] ;
false.

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).

Name: Rashida Jahan RollNo: CSE2016/006


Or

connected(X,Y) :- edge(X,Y).
connected(X,Y) :- edge(Y,X).

24) Write a Prolog program to multiply two vectors.

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.

start:- radius(R),circ(R,C),write('Circumference is '),write(C),nl,area(R,A),write('Area is


'),write(A).
radius(R):- write('Radius= '),read(R).
circ(R,C):- C is 2*3.14*R.
area(R,A):- A is 3.14*R*R.

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).

?- parent(abraham,X). /* The children of Abraham */


X = ismael
Y = isaac
yes
?- parent(abraham,_). /* Did Abraham have children? */
yes
?- parent(Father,esau). /* The father of Esau? */
Father = isaac
?- parent(F,S). /* All the pairs father-son from the data base */
F = abraham
S = ismael
...

Name: Rashida Jahan RollNo: CSE2016/006


?- parent(abraham,X),parent(X,Grandson). /* Is Abraham grandfather? */
X = isaac
Grandson = esau
X = isaac
Grandson = iacob
No

grandfather(B,N):- parent(B,P),parent(P,N).

?- grandfather(abraham,iacob).
yes
?- grandfather(abraham,Grandson).
Grandson = esau
Grandson = iacob
No

brother(F1,F2):- parent(P,F1),parent(P,F2),not F1=F2.

?- brother(esau,iacob).
Yes

descendent(X,Y):- parent(X,Y).
descendent(X,Y):- parent(X,Z),descendent(Z,Y).

?- descendent(abraham,esau).
yes

27) Write a prolog program to find the last element of a list.

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]]

28) Write a prolog program to find the size of a list.

my_length([],0).
my_length([_|T],R):- my_length(T,R1),R is R1+1.

Name: Rashida Jahan RollNo: CSE2016/006


?- my_length([a,b,[c,d],e],R).
R=4
?- my_length([[],[]],R).
R=2
?- my_length([[[]]],R).
R=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)).

fib(1,1) :- !, write('1, ').


fib(2,1) :- !, write('1, ').
fib(N,F) :-N1 is N-1,memo(fib(N1,F1)),N2 is N-2,memo(fib(N2,F2)),F is F1 + F2,write(F),
write(',').

2 ?- fib(7,X).
1, 1, 2,3,5,8,13,
X = 13.

30) Explain Different Arithmetic Predicates of Prolog.

?- 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

Name: Rashida Jahan RollNo: CSE2016/006


?- X is 8 mod 3.
X=2

?- Y is 10^3 * (1+1) + 3.
Y = 2003

?- halt. Exit from Prolog

31) Write a prolog program for below tree data relation.

/* The tree database */

:- op(500,xfx,'is_parent').

a is_parent b. c is_parent g. f is_parent l. j is_parent q.


a is_parent c. c is_parent h. f is_parent m. j is_parent r.
a is_parent d. c is_parent i. h is_parent n. j is_parent s.
b is_parent e. d is_parent j. i is_parent o. m is_parent t.
b is_parent f. e is_parent k. i is_parent p.

/* X and Y are siblings */

:- op(500,xfx,'is_sibling_of').

X is_sibling_of Y :- Z is_parent X,
Z is_parent Y,
X \== Y.

/* X and Y are on the same level in the tree. */

:-op(500,xfx,'is_same_level_as').

Name: Rashida Jahan RollNo: CSE2016/006


X is_same_level_as X .
X is_same_level_as Y :- W is_parent X,
Z is_parent Y,
W is_same_level_as Z.

/* Depth of node in the tree. */

:- 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.

/* Locate node by finding a path from root down to the node. */


locate(Node) :- path(Node),
write(Node),
nl.
path(a). /* Can start at a. */
path(Node) :- Mother is_parent Node, /* Choose parent, */
path(Mother), /* find path and then */
write(Mother),
write(' --> ').

/* Calculate the height of a node, length of longest path to


a leaf under the node. */

height(N,H) :- setof(Z,ht(N,Z),Set), max(Set,0,H).

ht(Node,0) :- leaf(Node), !.
ht(Node,H) :- Node is_parent Child,
ht(Child,H1),
H is H1 +1.

leaf(Node) :- not(is_parent(Node,Child)). %/* Node grounded */

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).

Name: Rashida Jahan RollNo: CSE2016/006


a --> c --> h --> n

32) Write a Prolog program to verifies if three numbers can be the edges of a triangle.

start:- write('input a= '),read(A),


write('input b= '),read(B),
write('input c= '),read(C),
A >= 0,B >= 0,C >= 0, /* must be positive */
A < B+C,B < C+A,C < A+B,
write('These numbers are the edges of a triangle.').

?- start.
input a= 3.
input b= 4.
input c= 5.
These numbers are the edges of a triangle.
yes

Name: Rashida Jahan RollNo: CSE2016/006

You might also like