You are on page 1of 3

PROBLEM STATEMENT:

Find the GCD of 2 numbers.

PROLOG PROGRAM LISTING:

gcd(X,X,X).

gcd(X,Y,D):- X>Y,

X1 is X-Y,

gcd(Y,X1,D).

gcd(X,Y,D):- X<Y,

gcd(Y,X,D).

PROGRAM OUTPUT:

1 ?-

% c:/Documents and Settings/Owner/My Documents/Prolog/proj/gcd.pl compiled 0.00 sec, 1,296 bytes

1 ?- gcd(12,42,R).

R=6

Yes

DISCUSSION:

A programming language is characterized by its control and its data manipulation mechanism. Prolog as the
general purpose programming language can be discussed in these terms as are conventional languages. We
compare the flow control and data manipulation of prolog.

The control in prolog programs is like that in conventional procedural languages as long as the computation
progresses forward. Goal invocation corresponds to procedure invocation and the ordering of goals in the body
of clauses corresponds to a sequence of statements. Specifically A B 1B2…………..Bn can be viewed as the
definition of a procedure A as follows:-

Procedure A

Call B1
Call B2
.
.
.
Call Bn
End
A recursive goal invocation in prolog is similar behavior and implementation of that of conventional recursive
languages. The differences shows when backtracking occurs. In a conventional language, if a computational
cannot proceed when a runtime error occurs. In prolog, the computation simple undone to the last choice made
and a different computation path is attempted. The data structures manipulated by logic programs, terms
correspond to general record structures in conventional programming languages. The handling of data structures
is very flexible in prolog. The major differences between prolog and conventional languages in the use of data
structure arise from the nature of logical variables refer to individuals rather than memory locations.
Consequently having once beed specified to refer to a particular individual, a variable cannot be made to refer to
another location.

2 ?- trace.

Yes

[trace] 2 ?- gcd(12,42,R).
Call: (8) gcd(12, 42, _G446) ? creep
^ Call: (9) 12>42 ? creep
^ Fail: (9) 12>42 ? creep
Redo: (8) gcd(12, 42, _G446) ? creep
^ Call: (9) 12<42 ? creep
^ Exit: (9) 12<42 ? creep
Call: (9) gcd(42, 12, _G446) ? creep
^ Call: (10) 42>12 ? creep
^ Exit: (10) 42>12 ? creep
^ Call: (10) _L199 is 42-12 ? creep
^ Exit: (10) 30 is 42-12 ? creep
Call: (10) gcd(12, 30, _G446) ? creep
^ Call: (11) 12>30 ? creep
^ Fail: (11) 12>30 ? creep
Redo: (10) gcd(12, 30, _G446) ? creep
^ Call: (11) 12<30 ? creep
^ Exit: (11) 12<30 ? creep
Call: (11) gcd(30, 12, _G446) ? creep
^ Call: (12) 30>12 ? creep
^ Exit: (12) 30>12 ? creep
^ Call: (12) _L229 is 30-12 ? creep
^ Exit: (12) 18 is 30-12 ? creep
Call: (12) gcd(12, 18, _G446) ? creep
^ Call: (13) 12>18 ? creep
^ Fail: (13) 12>18 ? creep
Redo: (12) gcd(12, 18, _G446) ? creep
^ Call: (13) 12<18 ? creep
^ Exit: (13) 12<18 ? creep
Call: (13) gcd(18, 12, _G446) ? creep
^ Call: (14) 18>12 ? creep
^ Exit: (14) 18>12 ? creep
^ Call: (14) _L259 is 18-12 ? creep
^ Exit: (14) 6 is 18-12 ? creep
Call: (14) gcd(12, 6, _G446) ? creep
^ Call: (15) 12>6 ? creep
^ Exit: (15) 12>6 ? creep
^ Call: (15) _L278 is 12-6 ? creep
^ Exit: (15) 6 is 12-6 ? creep
Call: (15) gcd(6, 6, _G446) ? creep
Exit: (15) gcd(6, 6, 6) ? creep
Exit: (14) gcd(12, 6, 6) ? creep
Exit: (13) gcd(18, 12, 6) ? creep
Exit: (12) gcd(12, 18, 6) ? creep
Exit: (11) gcd(30, 12, 6) ? creep
Exit: (10) gcd(12, 30, 6) ? creep
Exit: (9) gcd(42, 12, 6) ? creep
Exit: (8) gcd(12, 42, 6) ? creep
R=6
Yes

The complexity of GCD is of the order of n i.e. O(n).

FACTS: The simplest kind of statement is called a fact. Facts are means of stating that a relation holds between
objects. A finite set of facts constitutes a program. In this program the fact is:
gcd(X,Y,D).
This fact says that the GCD of X and Y will be stored in D.

Proposition Logic: Logical statement which have a definite truth value (true or false). Generally each
proposition is either TRUE or FALSE. Remember that the commands, wishes are not treated as propositions.

You might also like