Page |1
Assignment No.: 1 Date: 22.03.23
Problem Statement: From the following family tree define the fact for the bellow given problem:
Females are pam, ann, pat, liz.
Males are tom, bob, jim.
a) Who are the parents of bob?
b) Find out who is the parent of whom?
c) Who is the parent of jim?
d) Who is the grandparent of jim?
e) Who are tom’s grand children?
f) Do ann and pat have common parent?
g) Who is the sister of pat?
h) Who is the mother of bob ?
i) Who is the father of whom?
j) Who is the grandfather of whom?
k) Who is the mother of whom?
Source Code:
female(pam).
female(ann).
female(pat).
female(liz).
male(tom).
male(bob).
male(jim).
Page |2
parent_of(pam, bob).
parent_of(tom, bob).
parent_of(ann, jim).
parent_of(bob, jim).
parent_of(pat, liz).
parent_of(X, Y) :-
father_of(X, Y).
parent_of(X, Y) :-
mother_of(X, Y).
father_of(Father, Child) :-
male(Father),
parent_of(Father, Child).
mother_of(Mother, Child) :-
female(Mother),
parent_of(Mother, Child).
grandparent_of(Grandparent, Grandchild) :-
parent_of(Grandparent, Parent),
parent_of(Parent, Grandchild).
grandchild_of(Grandchild, Grandparent) :-
grandparent_of(Grandparent, Grandchild).
sibling_of(Sibling1, Sibling2) :-
parent_of(Parent, Sibling1),
parent_of(Parent, Sibling2),
Sibling1 \= Sibling2,
male(Parent).
sister_of(Sister, Sibling) :-
female(Sister),
sibling_of(Sister, Sibling).
brother_of(Brother, Sibling) :-
male(Brother),
sibling_of(Brother, Sibling).
grandchildren_of(Person, Grandparent) :-
grandchild_of(Grandparent, Person).
mother_of(Mother, Child) :-
female(Mother),
parent_of(Mother, Child).
father_of(Father, Child) :-
male(Father),
parent_of(Father, Child).
grandfather_of(Grandfather, Grandchild) :-
male(Grandfather),
grandparent_of(Grandfather, Grandchild).
grandmother_of(Grandmother, Grandchild) :-
Page |3
female(Grandmother),
grandparent_of(Grandmother, Grandchild).
Output:
A.
?- parent_of(Parent, bob).
Parent = pam ;
Parent = tom ;
false.
B.
?- parent_of(Parent, Child).
Parent = pam,
Child = bob ;
Parent = tom,
Child = bob ;
Parent = ann,
Child = jim ;
Parent = bob,
Child = jim ;
Parent = pat,
Child = liz ;
false.
C.
?- parent_of(Parent, jim).
Parent = ann ;
Parent = bob ;
false.
D.
?- grandparent_of(Grandparent, jim).
Grandparent = pam ;
Grandparent = tom ;
false.
E.
?- grandchildren_of(Grandchild, tom).
Grandchild = jim ;
Grandchild = liz ;
false.
Page |4
F.
?- parent_of(Parent, ann), parent_of(Parent, pat).
Parent = pam ;
false.
G.
?- sister_of(Sister, pat).
Sister = ann .
H.
?- mother_of(Mother, bob).
Mother = pam .
I.
?- father_of(Father, Child).
Father = tom,
Child = bob ;
Father = bob,
Child = jim ;
false.
J.
?- grandfather_of(Grandfather, Grandchild).
Grandfather = pam,
Grandchild = jim ;
Grandfather = tom,
Grandchild = jim ;
false.
K.
?- mother_of(Mother, Child).
Mother = pam,
Child = bob ;
Mother = ann,
Child = jim ;
Mother = pat,
Child = liz ;
false.
Page |5
Assignment No.: 2 Date: 29.03.23
Problem Statement: Write a prolog program to determine whether in a list, the first two
elements are same.
Source Code:
first_two_same([X, X | _]).
Output:
?- first_two_same([1, 1, 2, 3]).
true.
?- first_two_same([a, b, c]).
false.
?- first_two_same([X, X, Y, Z]).
True.
?- first_two_same([1, c, 2, 3]).
false.
?- first_two_same([w, w, 2, 3]).
true.
?- first_two_same([c,c]).
true.
?- first_two_same([2, 3]).
false.
Page |6
Assignment No.: 3 Date: 29.03.23
Problem Statement: Write a prolog program that checks whether a list does not contain
exactly two elements.
Source Code:
not_two_elements(List) :-
\+ has_two_elements(List).
has_two_elements([_, _]).
Output:
?- not_two_elements([1, 2, 3]).
true.
?- not_two_elements([a]).
true.
?- not_two_elements([x, y])
false.
?- not_two_elements([ 2, 3]).
false.
?- not_two_elements([w,m,j]).
true.
?- not_two_elements([ a, a]).
false.
?- not_two_elements([]).
true.
?- not_two_elements([a,1,u]).
true.
Page |7
Assignment No.: 4 Date: 29.03.23
Problem Statement: Write a prolog program to determine whether the two lists are of same
length.
Source Code:
same_length([], []).
same_length([_|T1], [_|T2]) :-
same_length(T1, T2).
Output:
?- same_length([1, 2, 3], [a, b, c]).
true.
?- same_length([1, 2, 3], [a, b]).
false.
?- same_length([], []).
true.
?- same_length([a,b,c,f], [a, b, c]).
false.
?- same_length([], [a, b]).
false.
?- same_length([89, s, 3], [a,1,m]).
true.
?- same_length([1,v,m, s, 3], [a,1,m,o,l,i,k]).
false.
?- same_length([m, s, 3], [l,i,k]).
true.
Page |8
Assignment No.: 5 Date: 29.03.23
Problem Statement: Write a prolog program to determine length of a list.
Source Code:
list_length([], 0).
list_length([_|T], Length) :- list_length(T, SubLength), Length is SubLength + 1.
Output:
?- list_length([1, 2, 3, 4, 5], Length).
Length = 5.
?- list_length([a, b, c, d], Length).
Length = 4.
?- list_length([a,b,c,d,e,f,g,h,i,j],Length).
Length = 10
?- list_length([],Length).
Length = 0
?- list_length([[a,b],[c,d],[e,f]],Length).
Length = 3
?- list_length([1, 2, 3, 4, 5,m,n,w,q], Length).
Length = 9.
?- list_length([1,[ 2, 3, 4, 5]], Length).
Length = 2.
?- list_length([a,[1,2],[m,n,p,i],9], Length).
Length = 4.
Page |9
Assignment No.: 6 Date: 29.03.23
Problem Statement: Write a prolog program to find the last element of a list.
Source Code:
last_element([X], X).
last_element([_|T], X) :-
last_element(T, X).
Output:
?- last_element([1,2,3,4], LE).
LE = 4 .
[1] ?- last_element([1,2,3,4,a,b,g,h], LE).
LE = h .
[1] ?- last_element([], LE).
false.
[1] ?- last_element([1,[2,3],[4,5]], LE).
LE = [4, 5] .
[1] ?- last_element([a,b,c,v,[g,1],m,5], LE).
LE = 5
[1] ?- last_element([u,m,l,k,o,p], LE).
LE = p .
[1] ?- last_element([g,k,l,[j,l,p]], LE).
LE = [j, l, p]
P a g e | 10
Assignment No.: 7 Date: 29.03.23
Problem Statement: Write a prolog program to check whether two elements are
consecutive elements in a list.
Source Code:
consecutive_elements([X, Y | _], X, Y).
consecutive_elements([_, T | Rest], X, Y) :-
consecutive_elements([T | Rest], X, Y).
Output:
?- consecutive_elements([1, 2, 3, 4, 5], 2, 3).
true.
?- consecutive_elements([a, b, c, d], c, d).
true.
?- consecutive_elements([42, 43, 44, 45], 45, 46).
false.
?- consecutive_elements([42, 43, 45, 46],1,2).
false.
[1] ?- consecutive_elements([s,t,u,v],t,u ).
true .
[1] ?- consecutive_elements([1,s,2,r,3,f], 2,r).
true .
[1] ?- consecutive_elements([1,s,2,r,3,f], 2,f).
false.
P a g e | 11
Assignment No.: 8 Date: 11.04.23
Problem Statement: Write a prolog program to determine whether an element is a member
of a list.
Source Code:
member_of_list(X, [X|_]).
member_of_list(X, [_|T]) :-
member_of_list(X, T).
Output:
?- member_of_list(2, [1, 2, 3, 4, 5]).
true.
?- member_of_list(d, [a, b, c, d]).
true.
?- member_of_list(6, [1, 2, 3, 4, 5]).
false.
?- member_of_list(1,[a,b,n]).
false.
?- member_of_list(4,[n,2,5,4,6]).
true .
?- member_of_list(2,[1,2,5,a,m,k]).
true.
?- member_of_list(1,[b,k,l,2,1,l,i]).
true.
?- member_of_list(a,[1,2,5,7,k,l,o]).
false.
P a g e | 12
Assignment No.: 9 Date: 11.04.23
Problem Statement: Write a prolog program to append two lists to generate a 3rd list.
Source Code:
append_lists([], L, L).
append_lists([H|T], L, [H|Result]) :-
append_lists(T, L, Result).
Output:
?- append_lists([1, 2, 3], [a, b, c], Result).
Result = [1, 2, 3, a, b, c].
?- append_lists([a, b], [1, 2, 3], Result).
Result = [a, b, 1, 2, 3].
?- append_lists([], [x, y, z], Result).
Result = [x, y, z].
?- append_lists([u,v,w,1], [a, b, c,5], Result).
Result = [u, v, w, 1, a, b, c, 5].
?- append_lists([1, 2, 3], [a, [1,2]], Result).
Result = [1, 2, 3, a, [1, 2]].
?- append_lists([1, [2,l], 3], [a, [b,v], c], Result).
Result = [1, [2, l], 3, a, [b, v], c].
P a g e | 13
Assignment No.: 10 Date: 11.04.23
Problem Statement: Write a prolog program to reverse a list.
Source Code:
reverse_list(List, Reversed) :-
reverse_list_helper(List, [], Reversed).
reverse_list_helper([], Reversed, Reversed).
reverse_list_helper([H|T], Acc, Reversed) :-
reverse_list_helper(T, [H|Acc], Reversed).
Output:
?- reverse_list([1, 2, 3, 4, 5], Reversed).
Reversed = [5, 4, 3, 2, 1].
?- reverse_list([a, b, c, d], Reversed).
Reversed = [d, c, b, a].
?- reverse_list([], Reversed).
Reversed = [].
?- reverse_list([1, 2, 3, 4, 5,6,7,8,9], Reversed).
Reversed = [9, 8, 7, 6, 5, 4, 3, 2, 1].
?- reverse_list([1,a,2,b,3,c,4,d], Reversed).
Reversed = [d, 4, c, 3, b, 2, a, 1].
?- reverse_list([w,w,w,w,5], Reversed).
Reversed = [5, w, w, w, w].
?- reverse_list([u,w,1,2,n,m,5,8], Reversed).
Reversed = [8, 5, m, n, 2, 1, w, u].
?- reverse_list([p,i,d,o,r,v,u,s], Reversed).
Reversed = [s, u, v, r, o, d, i, p].
P a g e | 14
Assignment No.: 11 Date: 11.04.23
Problem Statement: Write a prolog program to determine whether a list is a palindrome.
Source Code:
is_palindrome(List) :-
reverse_list(List, Reversed),
List = Reversed.
reverse_list([], []).
reverse_list([X|Rest], Reversed) :-
reverse_list(Rest, ReversedRest),
append_lists(ReversedRest, [X], Reversed).
append_lists([], L, L).
append_lists([H|T], L, [H|Result]) :-
append_lists(T, L, Result).
Output:
?- is_palindrome([1, 2, 3, 2, 1]).
true.
?- is_palindrome([a, b, c, b, a]).
true.
?- is_palindrome([1, 2, 3, 4, 5]).
false.
?- is_palindrome([w,w,u,u,p,p]).
false.
?- is_palindrome([r,a,c,e,c,a,r]).
true.
?- is_palindrome([1,0,1,0,1,0,1]).
true.
P a g e | 15
Assignment No.: 12 Date: 11.04.23
Problem Statement: Write a program in PROLOG for sum, subtraction, multiplication,
division, square root using function.
Source Code:
sum(A, B, Result) :-
Result is A + B.
subtraction(A, B, Result) :-
Result is A - B.
multiplication(A, B, Result) :-
Result is A * B.
division(A, B, Result) :-
B =\= 0, % Ensure B is not zero
Result is A / B.
square_root(A, Result) :-
A >= 0, % Ensure A is non-negative
Result is sqrt(A).
Output:
?- sum(5, 3, Result).
Result = 8.
?- subtraction(10, 4, Result).
Result = 6.
?- multiplication(3, 4, Result).
Result = 12.
?- division(10, 2, Result).
Result = 5.
?- square_root(25, Result).
Result = 5.
P a g e | 16
Assignment No.: 13 Date: 26.04.23
Problem Statement: Write a prolog program to find gcd of two +ve integers.
Source Code:
gcd(A, B, Result) :-
B = 0,
Result is A.
gcd(A, B, Result) :-
B > 0,
Remainder is A mod B,
gcd(B, Remainder, Result).
Output:
?- gcd(24, 36, Result).
Result = 12.
?- gcd(15, 25, Result).
Result = 5.
?- gcd(8, 12, Result).
Result = 4.
?- gcd(13, 87, Result).
Result = 1 .
?- gcd(112, 36, Result).
Result = 4 .
?- gcd(24, 0, Result).
Result = 24 .
?- gcd(1110, 99, Result).
Result = 3
P a g e | 17
Assignment No.: 14 Date: 26.04.23
Problem Statement: Write a prolog program to find the maximum between two numbers.
Source Code:
maximum(A, B, A) :-
A >= B.
maximum(A, B, B) :-
B > A.
Output:
?- maximum(5, 3, Result).
Result = 5.
?- maximum(10, 15, Result).
Result = 15.
?- maximum(7, 7, Result).
Result = 7.
?- maximum(15, 322, Result).
Result = 322.
?- maximum(5, -3, Result).
Result = 5 .
?- maximum(-5, -3, Result).
Result = -3.
?- maximum(45888955, 89556666, Result).
Result = 89556666.
P a g e | 18
Assignment No.: 15 Date: 26.04.23
Problem Statement: Write a prolog program to find the maximum number of a list.
Source Code:
max_list([X], X).
max_list([H|T], Max) :-
max_list(T, TempMax),
(H > TempMax, Max = H; Max = TempMax).
Output:
?- max_list([2, 8, 5, 10, 3], Max).
Max = 10.
?- max_list([7, 4, 9, 2, 6], Max).
Max = 9.
?- max_list([2, 18, 5, 10, 3], Max).
Max = 18 .
?- max_list([100,200,300,400], Max).
Max = 400 .
?- max_list([21, 80, 52, 100, 301], Max).
Max = 301 .
?- max_list([12,18,15, 10, 13], Max).
Max = 18
?- max_list([100,200,300,400,500,800,1000,1000000000], Max).
Max = 1000000000
P a g e | 19
Assignment No.: 16 Date: 26.04.23
Problem Statement: Write a prolog program to check whether a list is ordered list.
Source Code:
ordered_list([]).
ordered_list([_]).
ordered_list([X, Y|T]) :-
X =< Y,
ordered_list([Y|T]).
Output:
?- ordered_list([1, 2, 3, 4, 5]).
true.
?- ordered_list([5, 4, 3, 2, 1]).
false.
?- ordered_list([10,15,20,30]).
true .
?- ordered_list([1, 2, 3, 4, 50]).
true .
?- ordered_list([1, 2, 13, 4, 15]).
false.
?- ordered_list([101,201,501]).
true .
?- ordered_list([10,9]).
false.
P a g e | 20
Assignment No.: 17 Date: 26.04.23
Problem Statement: Write a prolog program to remove duplicate element from a list.
Source Code:
remove_duplicates([], []).
remove_duplicates([H|T], Result) :-
member(H, T),
remove_duplicates(T, Result).
remove_duplicates([H|T], [H|Result]) :-
\+ member(H, T),
remove_duplicates(T, Result).
Output:
?- remove_duplicates([1, 2, 2, 3, 4, 4, 5, 5, 5], Result).
Result = [1, 2, 3, 4, 5].
?- remove_duplicates([a, b, c, c, d, d, e], Result).
Result = [a, b, c, d, e].
?- remove_duplicates([a,1,b,a,w,w], Result).
Result = [1, b, a, w] .
?- remove_duplicates([7,7,7,7], Result).
Result = [7] .
?- remove_duplicates([4,4,5,5,6,6], Result).
Result = [4, 5, 6].
?- remove_duplicates([a,s,s,e,r,r], Result).
Result = [a, s, e, r].
P a g e | 21
Assignment No.: 18 Date: 26.04.23
Problem Statement: Write a prolog program that selects an element from a list, saving the
remaining elements in another list.
Source Code:
select_element(Element, [Element|Rest], Rest).
select_element(Element, [Head|Tail], [Head|Rest]) :-
select_element(Element, Tail, Rest).
Output:
?- select_element(3, [1, 2, 3, 4, 5], Rest).
Rest = [1, 2, 4, 5].
?- select_element(a, [a, b, c, d, e], Rest).
Rest = [b, c, d, e].
?- select_element(o, [a,e,i,o,u], Rest).
Rest = [a, e, i, u] .
?- select_element(p, [m,n,o,p], Rest).
Rest = [m, n, o] .
?- select_element(r, [w,u,i,o], Rest).
false.
?- select_element(k, [t,s,1,2,u,k,3,5], Rest).
Rest = [t, s, 1, 2, u, 3, 5] .
P a g e | 22
Assignment No.: 19 Date: 10.05.23
Problem Statement: Write a Prolog program to find the Square root of a number using
function.
Source Code:
square_root(X, Result) :-
Result is sqrt(X).
Output:
?- square_root(25, Result).
Result = 5.0.
?- square_root(16, Result).
Result = 4.0.
?- square_root(125, Result).
Result = 11.180339887498949.
?- square_root(1225, Result).
Result = 35.0.
?- square_root(4025, Result).
Result = 63.4428877022476.
?- square_root(963587, Result).
Result = 981.6246736915286.
?- square_root(105625, Result).
Result = 325.0.
P a g e | 23
Assignment No.: 20 Date: 10.05.23
Problem Statement: Write a Prolog program to find the modulus of two number using
function.
Source Code:
modulus(X, Y, Result) :-
Result is X mod Y.
Output:
?- modulus(10, 3, Result).
Result = 1.
?- modulus(15, 4, Result).
Result = 3.
?- modulus(110, 4, Result).
Result = 2.
?- modulus(150000, 40, Result).
Result = 0.
?- modulus(-15, 4, Result).
Result = 1.
?- modulus(5000,417, Result).
Result = 413.
?- modulus(65484,147, Result).
Result = 69.
P a g e | 24
Assignment No.: 21 Date: 10.05.23
Problem Statement: Write a prolog program to find the cube of a number.
Source Code:
cube(Number, Result) :-
Result is Number * Number * Number.
Output:
?- cube(2, Result).
Result = 8.
?- cube(5, Result).
Result = 125.
?- cube(23, Result).
Result = 12167.
?- cube(12, Result).
Result = 1728.
?- cube(-2, Result).
Result = -8.
?- cube(205, Result).
Result = 8615125.
?- cube(252, Result).
Result = 16003008.
?- cube(25, Result).
Result = 15625.
P a g e | 25
Assignment No.: 22 Date: 10.05.23
Problem Statement: Write a prolog program whether a number is positive or negative or
zero.
Source Code:
number_type(Number, positive) :-
Number > 0.
number_type(0, zero).
number_type(Number, negative) :-
Number < 0.
Output:
?- number_type(5, Type).
Type = positive.
?- number_type(0, Type).
Type = zero.
?- number_type(-3, Type).
Type = negative.
?- number_type(15, Type).
Type = positive .
?- number_type(-95, Type).
Type = negative.
?- number_type(00, Type).
Type = zero .
?- number_type(-9, Type).
Type = negative.
?- number_type(-7.5, Type).
Type = negative.
P a g e | 26
Assignment No.: 23 Date: 10.05.23
Problem Statement: Write a prolog program to find even-odd number using function.
Source Code:
even_odd(Number, even) :-
Number mod 2 =:= 0.
even_odd(Number, odd) :-
Number mod 2 =\= 0.
Output:
?- even_odd(11, R).
R = odd.
?- even_odd(20, R).
R = even .
?- even_odd(1, R).
R = odd.
?- even_odd(0, R).
R = even .
?- even_odd(101, R).
R = odd.
?- even_odd(10000, R).
R = even .
?- even_odd(-558, R).
R = even .
P a g e | 27
Assignment No.: 24 Date: 10.05.23
Problem Statement: Write a prolog program to print n to m numbers using for loop.
Source Code:
print_numbers(N, M) :-
N =< M,
write(N),
n1,
Next is N + 1,
print_numbers(Next, M).
print_numbers(N, M) :-
N > M.
Output:
?- print_numbers(1, 5).
1
2
3
4
5
true .
?- print_numbers(-1, 5).
-1
0
1
2
3
4
5
true .
P a g e | 28
Assignment No.: 25 Date: 10.05.23
Problem Statement: Write a program to calculate the sum of n natural numbers.
Source Code:
sum_of_natural_numbers(0, 0).
sum_of_natural_numbers(N, Sum) :-
N > 0,
N1 is N - 1,
sum_of_natural_numbers(N1, SubSum),
Sum is SubSum + N.
Output:
?- sum_of_natural_numbers(5, Sum).
Sum = 15.
?- sum_of_natural_numbers(10, Sum).
Sum = 55.
?- sum_of_natural_numbers(15, Sum).
Sum = 120 .
?- sum_of_natural_numbers(20, Sum).
Sum = 210 .
?- sum_of_natural_numbers(30, Sum).
Sum = 465 .
?- sum_of_natural_numbers(50, Sum).
Sum = 1275 .
?- sum_of_natural_numbers(100, Sum).
Sum = 5050 .
P a g e | 29
Assignment No.: 26 Date: 26.05.23
Problem Statement: Write a prolog program to find the sum of square of each number of n
natural number.
Source Code:
sum_of_squares(0, 0).
sum_of_squares(N, Sum) :-
N > 0,
N1 is N - 1,
sum_of_squares(N1, SubSum),
Sum is SubSum + N * N.
Output:
?- sum_of_squares(5, Sum).
Sum = 55.
?- sum_of_squares(10, Sum).
Sum = 385.
?- sum_of_squares(15, Sum).
Sum = 1240 .
?- sum_of_squares(20, Sum).
Sum = 2870 .
?- sum_of_squares(30, Sum).
Sum = 9455 .
?- sum_of_squares(50, Sum).
Sum = 42925 .
?- sum_of_squares(80, Sum).
Sum = 173880 .
?- sum_of_squares(100, Sum).
Sum = 338350 .
P a g e | 30
Assignment No.: 27 Date: 26.05.23
Problem Statement: Write a prolog program to find the sum of inverse of each number of n
natural number.
Source Code:
sum_of_inverses(0, 0).
sum_of_inverses(N, Sum) :-
N > 0,
N1 is N - 1,
sum_of_inverses(N1, SubSum),
Sum is SubSum + 1 / N.
Output:
?- sum_of_inverses(5, Sum).
Sum = 2.283333333333333.
?- sum_of_inverses(10, Sum).
Sum = 2.9289682539682538.
?- sum_of_inverses(15, Sum).
Sum = 3.3182289932289937 .
?- sum_of_inverses(20, Sum).
Sum = 3.597739657143682 .
?- sum_of_inverses(25, Sum).
Sum = 3.8159581777535068 .
?- sum_of_inverses(30, Sum).
Sum = 3.9949871309203906 .
?- sum_of_inverses(50, Sum).
Sum = 4.499205338329423 .
P a g e | 31
Assignment No.: 28 Date: 26.05.23
Problem Statement: Write a prolog program to find factorial of a number.
Source Code:
factorial(0, 1).
factorial(N, Result) :-
N > 0,
N1 is N - 1,
factorial(N1, SubResult),
Result is N * SubResult.
Output:
?- factorial(5, Result).
Result = 120.
?- factorial(10, Result).
Result = 3628800.
?- factorial(7, Result).
Result = 5040 .
?- factorial(15, Result).
Result = 1307674368000 .
?- factorial(18, Result).
Result = 6402373705728000 .
?- factorial(20, Result).
Result = 2432902008176640000 .
?- factorial(25, Result).
Result = 15511210043330985984000000 .
?- factorial(19, Result).
Result = 121645100408832000
P a g e | 32
Assignment No.: 29 Date: 26.05.23
Problem Statement: Write a prolog program to find Fibonacci series.
Source Code:
fibonacci(0, 0).
fibonacci(1, 1).
fibonacci(N, Result) :-
N > 1,
N1 is N - 1,
N2 is N - 2,
fibonacci(N1, Fib1),
fibonacci(N2, Fib2),
Result is Fib1 + Fib2.
Output:
?- fibonacci(6, Result).
Result = 8.
?- fibonacci(10, Result).
Result = 55.
?- fibonacci(7, Result).
Result = 13 .
?- fibonacci(11, Result).
Result = 89 .
?- fibonacci(13, Result).
Result = 233 .
?- fibonacci(15, Result).
Result = 610 .
?- fibonacci(18, Result).
Result = 2584 .
?- fibonacci(20, Result).
Result = 6765 .
P a g e | 33
Assignment No.: 30 Date: 26.05.23
Problem Statement: Write a prolog program to find out whether a list is sublist of a list.
Source Code:
subset([], _).
subset([X|Sublist], List) :-
member(X, List),
subset(Sublist, List).
Output:
?- subset([1, 2], [1, 2, 3, 4]).
true.
?- subset([a, b], [1, 2, 3, 4]).
false.
?- subset([a, b], [1, 2, 3, 4,a]).
false.
?- subset([a, u], [a,e,i,o,u]).
true .
?- subset([50,60], [30,40,50,60]).
true .
?- subset([8,19,17], [11,12,14,17,8,19]).
true .
?- subset([f,l,4], [w,r,i,h,f,l,u,p]).
false.