Spis treści

Common divisor 2

Description

A pattern-directed program to find the greatest common divisor of a set of numbers.

Source: PROLOG programming for artificial intelligence, 3rd Edition, Harlow, 2001, ISBN 0-201-40375-7.

Download

Program source code: common_divisor_2.pl

Listing

%  Figure 23.12  A pattern-directed program to find the greatest 
%  common divisor of a set of numbers.
 
%  The follwing directive is required by some Prologs:
 
:-  dynamic num/1.
 
%  Production rules for finding greatest common divisor (Euclid algorithm)
 
:-  op( 800, xfx, --->).
:-  op( 300, fx, num).
 
[ num X, num Y, X > Y ]  --->
[ NewX is X - Y, replace( num X, num NewX) ].
 
[ num X]  --->  [ write( X), stop ].
 
 
%  An initial database
 
num 25.
num 10.   
num 15.   
num 30.

Comments