Pascal: Greatest common divider

I often used this function – a simple and clear to understand algorithm to find the greatest common divider of two numbers. Here is the whole code for that:

function greatest_common_divider(a, b:integer):integer;
  begin
     while (a <> 0) AND (b <> 0) do
        if a >= b
          then a := a mod b
          else b := b mod a;
    greatest_common_divider := a + b;
  end;

I bet there’s no better way to do this on Pascal.

Leave a comment