Question 0-2

Consider the following method to compute the greatest common denominator of two numbers.

public static int gcd(int m, int n) {
    int r;
    while(m != 0) {
        r = m % n;
        m = n;
        n = r;
    }
    return m;
}
Say we call gcd(42, 54). Show how the values of r, m, and n change as the computer continues through the method.
r
m 42
n 54

Solution

r    42 12  6  0
m 42 54 42 12  6
n 54 42 12  6  0

Back to Review for Quiz 0