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

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this. Cookies are only used for statistical purposes.

Close