next up previous
Next: GCD: problems of the Up: Unit 06 Previous: Example: computing the greatest

GCD: by directly exploiting the definition

First refinement of the algorithm:

public static int greatestCommonDivisor(int x, int y) {
  int gcd;

  initialize gcd to the minimum of x and y
  while ((gcd > 1) && (we have not found a common divisor))
    if (gcd divides both x and y)
      we have found a common divisor
    else
      gcd-;

  return gcd;
}

Observations:


next up previous
Next: GCD: problems of the Up: Unit 06 Previous: Example: computing the greatest