Write Prolog definitions a Write a Prolog definition of the

Write Prolog definitions

a) Write a Prolog definition of the greatest common divisor of two numbers. Then use it to compute gcd(4, 10), gcd(15, 36), and gcd(25, 55).

b) Write a Prolog program to find the last item in a list.

Solution

traditional programming languages are said to be procedural
procedural programmer must specify in detail how to solve a problem:
mix ingredients;
beat until smooth;
bake for 20 minutes in a moderate oven;
remove tin from oven;
put on bench;
close oven;
turn off oven;
in purely declarative languages, the programmer only states what the problem is and leaves the rest to the language system
We\'ll see specific, simple examples of cases where Prolog fits really well shortly

Applications of Prolog
Some applications of Prolog are:

intelligent data base retrieval
natural language understanding
expert systems
specification language
machine learning
robot planning
automated reasoning
problem solving


Prolog code for GCD and LCM

Find the GCD of the two number X and Y.

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

Input: gcd(10,5,G).

Output: G = 5

Find the LCM of the two number X and Y.

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

lcm(X,Y,LCM):-gcd(X,Y,GCD), LCM is X*Y//GCD.

Input: lcm(4,5,M).

Output: M = 20

gcd(4,10)=2

4=2x2

10=5x2

Integer numbers prime factorization:
15 = 3 * 5;
36 = 22 * 32;
Take all the common prime factors, by the lowest powers.
gcf, gcd (15; 36) = 3

Greatest common factor (divisor)
gcf, gcd (25; 55) = ?

Integer numbers prime factorization:
25 = 52;
55 = 5 * 11;
Take all the common prime factors, by the lowest powers.
gcf, gcd (25; 55) = 5

Write Prolog definitions a) Write a Prolog definition of the greatest common divisor of two numbers. Then use it to compute gcd(4, 10), gcd(15, 36), and gcd(25,
Write Prolog definitions a) Write a Prolog definition of the greatest common divisor of two numbers. Then use it to compute gcd(4, 10), gcd(15, 36), and gcd(25,

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site