Procedural Languages

 [Previous Chapter]  [Previous Page]  [Contents]  [Next Page]  [Next Chapter]

Example: Compute the greatest common divisor (GCD) of two natural numbers. Thanks to Euclid we know that

1.GCD(x,x)   =   x
2.GCD(x,y)   =   GCD(y,x)
3.x   >   y   =>   GCD(x,y)   =   GCD(x-y,y)

This allows to compute the GCD of two natural numbers x and y using following method:

1.If x equals y stop.
2.If x   <   y, subtract y from x and continue with step 1.
3.Subtract x from y and continue with step 1.

When this algorithm stops, the GCD is to be found in x.

This algorithm can be directly expressed in procedural high-level languages:
GCD.java
while (x != y) {
   if (x > y) {
      x = x - y;
   } else {
      y = y - x;
   }
}

 [Previous Chapter]  [Previous Page]  [Contents]  [Next Page]  [Next Chapter]
Copyright © 2001, 2002 Andreas Borchert, converted to HTML on February 11, 2002