Download MuPAD from SciFace

MuPAD Pro Computing Essentials - Examples 

Example 5.2 The Euclid algorithm to find GCD of two integers

The Euclid algorithm to find the greatest common divisor of two numbers is considered to be one of the most classical examples of high school algorithms. Of course, it depends on the country and the school that you are in. However, the idea is quite simple. If you do not remember it, just look at the procedure enclosed below and try to figure out how it works. 

  • euclid:=proc(a,b)
    local u,v;
    begin
       if testtype(a,Type::Integer) and
          testtype(b,Type::Integer)
       then
          u := abs(a);
          v := abs(b);
          while u<>v do
             if u>v then
                u := u - v
             else
                v := v - u
             end
          end;
          return(u);
       else
          error("use integer numbers")
       end;
    end: 

Now you can try testing it and see what you get:

  • euclid(-12,234)
  • euclid(-22,223)
  • euclid(12,244)
  • euclid(12765,23456)

 

TOP