5. Write a Prolog program to implement GCD of two numbers.
%Write a Prolog program to implement GCD of two numbers.
% below gcd function is not predefined you can use any name as function
gcd(X,Y):-
( X=0->
write(Y)
;
Y=0->
write(X)
;
X=Y->
write(X)
;
X>Y ->
( Z is X-Y,
gcd(Z,Y)
)
;
Y>X ->
(C is Y-X,
gcd(X,C)
)
).
% Output
No comments:
Post a Comment