Write a Prolog program to implement GCD of two numbers.

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

Featured post

Amazon Interview Process

On July 5, 1994, Jeff Bezos started the world's most "customer-centric" firm out of his garage in Bellevue, Washington. The A...