Greatest common divisor: Difference between revisions

m
Line 596:
Here is an implementation using the do macro. We call the function <code>gcd2</code> so as not to conflict with <code>common-lisp:gcd</code>.
 
<lang lisp>(defun gcd2gcd* (a b)
(do () ((zerop b) (abs a))
(shiftf a b (mod a b))))</lang>
Line 602:
Here is a tail-recursive implementation.
 
<lang lisp>(defun gcd2gcd* (a b)
(if (zerop b) a
a
(gcd2 b (mod a b))))</lang>
 
The last implementation is based on the loop macro.
 
<lang lisp>(defun gcd2gcd* (a b)
(loop for x = a then y
and y = b then (mod x y)
until (zerop y)
finally (return x)))</lang>
 
=={{header|Component Pascal}}==
BlackBox Component Builder
Anonymous user