Greatest common divisor

You are encouraged to solve this task according to the task description, using any language you may know.
Task

Find the greatest common divisor   (GCD)   of two integers.


Greatest common divisor   is also known as   greatest common factor (gcf)   and   greatest common measure.



Related task

   least common multiple.


See also

   MathWorld entry:   greatest common divisor.
   Wikipedia entry:     greatest common divisor.


 
rem - return p mod q
function mod(p, q = integer) = integer
end = p - q * (p / q)
 
rem - return greatest common divisor of x and y
function gcd(x, y = integer) = integer
  var r, temp = integer
  if x < y then
    begin
      temp = x
      x = y
      y = temp
    end
  r = mod(x, y)
  while r <> 0 do
    begin
      x = y
      y = r
      r = mod(x, y)
    end
end =  y
 
rem - exercise the function
 
print "The GCD of 21 and 35 is"; gcd(21,35)
print "The GCD of 23 and 35 is"; gcd(23,35)
print "The GCD of 1071 and 1029 is"; gcd(1071, 1029)
print "The GCD of 3528 and 3780 is"; gcd(3528,3780)
 
end
 