Różnice
Różnice między wybraną wersją a wersją aktualną.
|
|
pl:prolog:pllib:common_divisor_2 [2019/06/27 15:50] |
pl:prolog:pllib:common_divisor_2 [2019/06/27 15:50] (aktualna) |
| ====== Common divisor 2 ====== |
| {{tag>math}} |
| ===== 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 ===== |
| <code prolog> |
| % 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. |
| </code> |
| ===== Comments ===== |
| |