Prolog Assignment 6th Semester University of Calcutta
Prolog Assignment 6th Semester University of Calcutta
|Page
INDEX
Page
Assignment No Assignment Name Date Signature
No.
Write a prolog program to find
1. 1 02/04/2024
gcd of two +ve integers.
Write a prolog program to find
2. the maximum between two 2 02/04/2024
numbers.
Write a prolog program to find
3. 3 12/04/2024
the maximum number of a list.
Write a prolog program to
4. check whether a list is ordered 4 12/04/2024
list.
Write a prolog program to
5. remove duplicate element from 5 19/04/2024
a list.
Write a prolog program that
selects an element from a list,
6. 6 19/04/2024
saving the remaining elements
in another list.
Write a prolog program to find
7. the square root of a number 7 26/04/2024
using function.
Write a prolog program to find
8. the modulus of two number 8 26/04/2024
using function.
Write a prolog program to find
9. 9 03/05/2024
the cube of a number.
Write a prolog program
10. whether a number is positive or 10 03/05/2024
negative or zero.
Write a prolog program to find
11. even-odd number using 11 10/05/2024
function.
Write a prolog program to print
12. 12 10/05/2024
n to m numbers using for loop.
Write a program to calculate
13. 13 17/05/2024
the sum of n natural numbers.
Write a prolog program to find
14. the sum of square of each 14 17/05/2024
number of n natural number.
Write a prolog program to find
15. the sum of inverse of each 15 24/05/2024
number of n natural number.
Write a prolog program to find
16. 16 24/05/2024
factorial of a number.
Write a prolog program to find
17. 17 31/05/2024
Fibonacci series.
Write a prolog program to find
18. out whether a list is subset of a 18 31/05/2024
list.
|Page
DATE: 02/04/2024
Assignment -1
Problem Statement: - Write a prolog program to find gcd of two +ve integers.
Theory:- Greatest common divisor (GCD) of integers a and b, is the greatest positive integer d such that d is a divisor of
both a and b; that is, there are integers e and f such that a = de and b = df, and d is the largest such integer. The GCD of a and b is
generally denoted gcd (a, b). In the name "greatest common divisor", the adjective "greatest" may be replaced by "highest", and
the word "divisor" may be replaced by "factor", so that another name include highest common factor.
For example, the GCD of 8 and 12 is 4, that is, gcd (8, 12) = 4.
Source Code: -
great_cd(A,0,A):-
A>0.
great_cd(A,B,R):-
B>0,
C is A mod B,
great_cd(B,C,R).
Output: -
1| P a g e
DATE: 02/04/2024
Assignment – 2
Problem Statement: - Write a prolog program to find the maximum between two numbers.
Theory: - In Prolog, finding the maximum of two numbers can be approached using declarative logic, where we define rules
based on conditions rather than step-by-step instructions. The maximum of two numbers X and Y can be defined as:
Source Code: -
max(X,Y,X):-
X=Y.
max(X,Y,Max):-
X>Y,
Max is X.
max(X,Y,Max):-
Y>X,
Max is Y.
Output: -
2| P a g e
DATE:12/04/2024
Assignment – 3
Problem Statement: - Write a prolog program to find the maximum number of a list.
Theory: - This operation is used to find the maximum element from a list. In Prolog, finding the maximum number in a list
involves recursively traversing the list and comparing elements to determine the maximum. Suppose a list L contains
[-21,0,89,34,67]. In this list 89 is the maximum number. So, after executing the code we should receive the output 89.
Source Code: -
maxitem([H],H).
maxitem([H|T],M):-
maxitem(T,M1),
max(H,M1,M).
max(X,Y,X):-
X>=Y.
max(X,Y,Y):-
X<Y.
Output: -
3| P a g e
DATE:12/04/2024
Assignment – 4
Problem Statement: - Write a prolog program to check whether a list is ordered list.
Theory: - An "ordered list" typically refers to a list where its elements are arranged in a specific order, often either in ascending
or descending order based on a certain criterion, such as numerical or lexicographical (alphabetical) order. In an ascending
ordered list, each element is less than or equal to the next element when considering a specific ordering criterion. In a descending
ordered list, each element is greater than or equal to the next element when considering a specific ordering criterion.
Source Code: -
list_orderasc([X, Y | Tail]) :- X =< Y, list_orderasc([Y|Tail]).
list_orderasc([ _ ]).
list_orderasc([ ]).
list_orderdes([ _ ]).
list_orderdes([ ]).
Output: -
4| P a g e
DATE:19/04/2024
Assignment – 5
Problem Statement: - Write a prolog program to remove duplicate elements from a list.
Theory: - When the same number of elements occurs in sorted or unsorted list, then those elements of the list are called
the duplicate elements. And we need to delete these duplicate elements or same number from the list to make the resultant
list consists of unique elements. For example, there is a list that contains [ 5, 8, 3, 3, 5, 9] elements. In this list, 3 and 5
occurs two times. Hence, these elements are duplicate elements. So, after deleting the duplicate elements from the list, we
get the list as [5, 8, 3,9].
Source Code: -
remove_duplicate([ ],[ ]).
remove_duplicate([H|T],[H|R]):- remove_duplicate(T,R).
membership(X,[X|_]).
membership(X,[_|T]):- membership(X,T).
Output: -
5| P a g e
DATE:19/04/2024
Assignment – 6
Problem Statement: - Write a prolog program that selects an element from a list, saving the remaining elements in another
list.
Theory: - The Lists is a data structure that can be used in different cases for non-numeric programming. Lists are used to store
the atoms as a collection. The list of elements will be enclosed with square brackets. A list can be either empty or non-empty. In
the first case, the list is simply written as a Prolog atom, []. In the second case, the list consists of two things as given below −The
first item, called the head of the list and the remaining part of the list, called the tail.
Source Code: -
select(Element, [Element|Rest], Rest).
Output: -
6| P a g e
DATE:26/04/2024
Assignment – 7
Problem Statement: - Write a prolog program to find the square root of a number using function.
Theory: - The square root of a number a, denoted as √a, is a value x such that x2 = ax^2 = a. In simpler terms, it is a
number that, when multiplied by itself (squared), gives the original number. We are going to use binary search algorithm
to optimize the approach. The primary objective of the Binary Search algorithm is to efficiently determine the appropriate
half to eliminate, thereby reducing the search space by half. It does this by determining a specific condition that ensures
that the target is not present in that half.
Source Code: -
square_root_binary_search(N, Result) :-
square_root_binary_search(N, 0, N, Result).
Result is Mid.
Output: -
7| P a g e
DATE:26/04/2024
Assignment – 8
Problem Statement: - Write a prolog program to find modulus of two number using functions.
Theory: - In mathematics, the mod is also known as the modulo or the modulus. The modulus is defined as a remainder value
when two numbers are divided. The mathematical representation of the modulus function is given as a mod b, where a and b are
two numbers.
Here, a = 16, b = 3
When 16 is divided by 3, the quotient obtained is 5, and it leaves the remainder 1. Hence, the 16 mod 3 is equal to 1.
Source Code: -
modulu(A,B,X):-
A >= B,
A1 is A - B,
modulu(A1,B,X).
modulu(A,B,A):-
A < B.
Output: -
8| P a g e
DATE:03/05/2024
Assignment – 9
Theory: In arithmetic and algebra, the cube of a number ‘n’ is its third power, that is, the result of multiplying three instances
of ‘n’ together. This operation is represented mathematically as:
n3 = n × n × n
For example, If x = 2, then 23 = 2 × 2 × 2 = 8; If x = −3, then (−3)3 = (−3) × (−3) × (−3) = −27
The cube of an even number is always even. The cube of an odd number is always odd. The cube of a positive number is positive.
The cube of a negative number is negative. The cube of zero is zero, i.e., 03=0.
Source Code: -
cube(Number,R):-
Output: -
9| P a g e
DATE: 03/05/2024
Assignment – 10
Problem Statement: - Write a prolog program whether a number is positive, negative or zero.
Theory: Positive and negative numbers are fundamental concepts in mathematics, each representing different sets of values in
relation to zero.
Positive numbers are all the numbers greater than zero. They are located to the right of zero on the number line. They can be
expressed without a sign or with a plus sign (+) in front of them, though the plus sign is often omitted in practice. Eg- 23,98,9.7,
….
Negative numbers are all the numbers less than zero. They are located to the left of zero on the number line. They are always
expressed with a minus sign (-) in front of them. Eg-1, -2, -3, -4, -5, ...
Zero is neither positive nor negative. It is the neutral point on the number line, separating positive numbers from negative
numbers.
Source Code: -
classify_number(X1, positive) :-
X1 > 0.
classify_number(X2, negative) :-
X2 < 0.
classify_number(0, zero).
check_n(X) :-
classify_number(X, Sign),
Output: -
10| P a g e
DATE: 10/05/2024
Assignment – 11
Problem Statement: - Write a prolog program to find even or odd number using function.
Theory: A number ‘n’ is called even if it is divisible by 2 without leaving a remainder i.e., an even number can be expressed in
the form: n = 2k, where k is an integer.
Eg: - 0, 2, 4, 6, 8, 10, ...
An integer n is called odd if it leaves a remainder 1 when divided by 2 i.e., an odd number can be expressed in the form n=2k+1,
where k is an integer.
Eg: - 1,3,5,7,9, ….
Source Code: -
evenodd_check(N):-
N>=2,
N1 is N-2,
evenodd_check(N1).
evenodd_check(N):-
N<0,
N1 is N+2,
evenodd_check(N1).
evenodd_check(0):-write('Number is even').
evenodd_check(1):-write('Number is odd').
Output: -
11| P a g e
DATE: 10/05/2024
Assignment – 12
Problem Statement: - Write a prolog program to print n to m numbers using for loop.
Theory: A for loop is a control flow statement used in many programming languages to iterate over a sequence of elements,
such as a range of numbers, elements in an array, or characters in a string. It is typically used to execute a block of code a specific
number of times. The general structure of a for loop includes initialization, a condition, and an iteration statement. Prolog does not
have traditional for loops like those found in imperative programming languages such as Python, Java, or C. Instead, Prolog relies
on recursion and backtracking to achieve repetitive tasks. In Prolog, recursion is used to perform repetitive tasks. A recursive
predicate calls itself with updated arguments until a base case is met.
Source Code: -
print_range(N, M) :-
N > M, !.
print_range(N, M) :-
N=<M,
write(N), nl,
Next is N+ 1,
print_range(Next, M).
Output: -
12| P a g e
DATE: 17/05/2024
Assignment – 13
Problem Statement: - Write a prolog program to calculate the sum of n natural numbers.
Theory: Prolog is a logic programming language that is used for solving problems that involve objects and relationships
between them. In Prolog, computation is expressed as relations, represented as facts and rules. The problem of summing the first n
natural numbers can be tackled recursively. The recursive definition of the sum of the first n natural numbers is as follows:
Source Code: -
sum_to(N,1):- N=<1,!.
sum_to(N, R):-
N1 is N-1,
sum_to(N1, R1),
R is R1 + N.
Output: -
13| P a g e
DATE: 17/05/2024
Assignment – 14
Problem Statement: - Write a prolog program to find the sum of square of each number of n natural numbers.
Theory: Prolog is a logic programming language that is used for solving problems that involve objects and relationships
between them. In Prolog, computation is expressed as relations, represented as facts and rules. The task of summing the squares of
the first n natural numbers involves defining rules and base cases that recursively compute the sum. The recursive definition for
summing squares can be structured as follows:
Source Code: -
sum_square(1, R):- R is 1, ! .
sum_square(N, R):-
N > 0,
N1 is N * N,
N2 is N - 1,
sum_square(N2, R1),
R is N1 + R1.
Output: -
14| P a g e
DATE: 24/05/2024
Assignment – 15
Problem Statement: - Write a prolog program to find the sum of inverse of each number of n natural numbers.
Theory: Prolog is a logic programming language that is used for solving problems that involve objects and relationships
between them. In Prolog, computation is expressed as relations, represented as facts and rules. The task of summing the inverses
of the first n natural numbers involves defining rules and base cases that recursively compute the sum. The recursive definition for
summing squares can be structured as follows:
Source Code: -
sum_inverse(1 ,R):-R is 1,!.
sum_inverse(N ,R):-
N>0,
N1 is 1 / N,
N2 is N - 1,
sum_inverse(N2, R1),
R is N1 + R1.
Output: -
15| P a g e
DATE: 24/05/2024
Assignment – 16
Theory: The Factorial of a whole number 'n' is defined as the product of that number with every whole number less than or
equal to 'n' till 1. For example, the factorial of 4 is 4 × 3 × 2 × 1, which is equal to 24. It is represented using the symbol '!' So, 24
is the value of 4! . The study of factorials is at the root of several topics in mathematics, such as number theory, algebra,
geometry, probability, statistics, graph theory, discrete mathematics, etc.
"By definition, the factorial of 0 is 1 (0! = 1). This is a convention in mathematics to make certain equations and functions work
smoothly."
Source Code: -
factorial(0, Result1):-
Result1 is 1,!.
factorial(N, Result) :-
N > 0,
N1 is N - 1,
factorial(N1, SubResult),
Result is N * SubResult.
Output: -
16| P a g e
DATE: 31/05/2024
Assignment – 17
Theory: The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones, usually
starting with 0 and 1. Mathematically, the Fibonacci series is defined by the recurrence relation:
F(0) = 0 ; F(1) = 1
Source Code: -
fib(N) :- N=1,write('0 ').
fib(N) :- N=2,write('0 '),write('1 ').
fib(N) :- N>2,write('0 '),write('1 '),N1 is N-2,fib1(N1,0,1).
fib1(N,A,B) :- N>0,C is A+B,write(C),write(' '),A1 is B,B1 is C,M is N -
1,fib1(M,A1,B1).
Output: -
17| P a g e
DATE: 31/05/2024
Assignment – 18
Problem Statement: - Write a prolog program to find whether a list is subset of a list.
Theory: Subsets are a part of one of the mathematical concepts called Sets. In mathematics, set A is a subset of a set B if all
elements of A are also elements of B; B is then a superset of A. If all elements of set 1 are present in set 2 then we say that set 1 is
a subset of set 2. We know that a set is a well-defined collection of numbers, alphabets, objects, or any items. If set 1 = {A, B, C}
and set 2 = {A, B, C, D, E, F} we can say that set 1 is a subset of set 2 since all the elements in set 1 are present in set 2.
Source Code: -
sublist([X|L],[X|M]) :- prefix(L,M),!.
sublist(L,[_|M]) :- sublist(L,M).
prefix([],_).
prefix([X|L],[X|M]) :- prefix(L,M).
Output: -
18| P a g e