0% found this document useful (0 votes)
43 views8 pages

Artificial Intelligence Deception File

Uploaded by

nitob90303
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views8 pages

Artificial Intelligence Deception File

Uploaded by

nitob90303
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

1. Write a prolog program to calculate the sum of two numbers.

Code:-

sum(X, Y) :- S is X + Y,
write(S),
nl.
/* sum(20,30). */

2. Write a Prolog program to implement max(X, Y, M) so that M is


the maximum of twonumbers X and Y.

Code:-

max(X, Y) :- X == Y -> (format('X and Y are Equal'), nl);


X > Y -> (Z is X, write(Z), nl);
X < Y -> (Z is Y, write(Z), nl).
/* max(-150,90). */

3. Write a program in PROLOG to implement factorial (N, F) where


F represents the factorial of a number N.

Code:-

factorial(0, 1).

factorial(N, F) :- N > 0, N1 is N - 1,
factorial(N1, F1),
F is N * F1.
/* factorial(5,F). */
4. Write a program in PROLOG to implement generate_fib(N,T)
where T represents the Nth term of the fibonacci series.

Code:-

generate_fib(0, 1).
generate_fib(1, 1).

generate_fib(N, T) :- N1 is N - 1,
generate_fib(N1, T1),
N2 is N - 2,
generate_fib(N2, T2),
T is T1 + T2.
/* generate_fib(7,T). */

5. Write a Prolog program to implement GCD of two numbers.

Code:-

gcd(X, Y) :- X == 0 -> write(Y);


Y == 0 -> write(X);
X == Y -> write(X);
X > Y -> (Z is X - Y, gcd(Z, Y));
Y > X -> (C is Y - X, gcd(X, C)).
/* gcd(12,36). */

6. Write a Prolog program to implement power (Num,Pow, Ans) :


where Num is raised to the power Pow to get Ans.

Code:-

power(0, Power, 0) :- Power > 0.


power(Num, 0, 1) :- Num > 0.
power(Num, Power, Ans) :- Num > 0, Power > 0,
P1 is Power - 1,
power(Num, P1, A1),
Ans is A1 * Num.
/* power(2,8,A). */

7. Prolog program to implement multi (N1, N2, R) : where N1 and


N2 denotes the numbers to be multiplied and R represents the
result.

Code:-

multi(N, 1, N).

multi(N1, N2, R) :- T is N2 - 1,
multi(N1, T, Y),
R is Y + N1.
/* multi(8,3,A). */

8. Write a Prolog program to implement memb(X, L): to check


whether X is a member of L or not.
Code:-

memb(X, [X|_]) :- !.
memb(X, [_|L]) :- memb(X, L).
/* memb(e,[b,c,d,a]). */

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.

Code:-
conc([], L, L) :- !.
conc([H|T], L2, [H|R]) :- conc(T, L2, R).
/* conc([1,2], [3,4], R). */

10. Write a Prolog program to implement reverse (L, R) where


List L is original and List R is reversed list.

Code:-

append([], L, L).
append([H|L1], L2, [H|L3]) :- append(L1, L2, L3).

reverse([],[]).
reverse([H|T],R):- reverse(T,L1), append(L1,[H],R).
/* append([1,2,3], [4,5,6], X). */
/* reverse([a,b,c,d],X). */

11. Write a program in PROLOG to implement palindrome (L)


which checks whether a list L is a palindrome or not.

Code:-

conc([], L, L) :- !. % base case


conc([H|T], L2, [H|R]) :- conc(T, L2, R). % recursive case

reverse([], []) :- !. % base case


reverse([H|T], R) :- reverse(T, RT), % recursive case
conc(RT, [H], R).

palindrome(L) :- reverse(L, L).


/* palindrome([1,2,3,2,1]). */
12. Write a Prolog program to implement sumlist(L, S) so that
S is the sum of a given list L.

Code:-

sumlist([], 0). % base case


sumlist([H|T], S) :- sumlist(T, S1), % recursive case
S is H + S1.
/* sumlist([1,2,3], S). */

13. Write a Prolog program to implement two predicates


evenlength(List) and oddlength(List) so that they are true if
their argument is a list of even or odd length respectively.

Code:-
evenlength([]) :- !.
evenlength([_|T]) :- oddlength(T).
oddlength([_]) :- !.
oddlength([_|T]) :- evenlength(T).
/* evenlength([1,2,3,4]). */

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.

Code:-

nth_element(N, [H|_], H) :- N = 1. % Optimized base case for


single element list

nth_element(N, [_|T], X) :-
N1 is N - 1,
nth_element(N1, T, X).

nth_element(_, _, fail). % Fail if N is out of bounds


/* nth_element(3,[apple, banana, cherry, dragonfruit], X). */

15. Write a Prolog program to implement maxlist(L, M) so


that M is the maximum number in the list.

Code:-

max2([H],H).
max2([H|T],R):-
max2(T,M1),
H>=M1,
R is H,!.
max2([H|T],R):-
max2(T,M1),
H<M1,
R is M1.
/* max2([20,100,80,102,99], R). */

16. Write a prolog program to implement insert_nth (I, N, L,


R) that inserts an item I into Nth position of list L to generate a
list R.

Code:-

mem(X,[X|_]).
mem(X,[_|T]):- mem(X,T).
/* insert a number in the list. */
insert(L,[X|Y],[L|_]).
insert(L,P,[X|Y],[X|M]):-
P>1,
P1 is P-1,
insert(L,P1,Y,M).
insert(L,1,[X|Y],M):- append([L],[X|Y],M).
/* insert(44,3,[0,1,b,2,c,5],R). */

17. Write a Prolog program to implement delete_nth (N, L, R)


that removes the element on Nth position from a list L to
generate a list R.

Code:-

delete_nth(1, [_|T], T).


delete_nth(N, [H|T], [H|R]) :- N > 1, N1 is N - 1, delete_nth(N1, T,
R).
/* delete_nth(2, [a,b,c,d,e], R). */

18. Write a program in PROLOG to implement merge (L1, L2,


L3) where L1 is first ordered list and L2 is second ordered list
and L3 represents the merged list.

Code:-

merge([],[],[]).
merge([],L2,L2).
merge(L1,[],L1).
merge([H1|T1],[H2|T2],[H1|T3]):- H1=<H2,
merge(T1, [H2|T2], T3).
merge([H1|T1],[H2|T2],[H2|T3]):- merge([H1|T1], T2, T3).
/* merge([1,3,5],[2,4,6],X). */

You might also like