ProgrammingVery hardCAT · MBA placement

For a = 240 and b = 46, the extended Euclid procedure below keeps x and y so that ax + by equals the gcd. What is the value of x printed at the end?

For a = 240 and b = 46, the extended Euclid procedure below keeps x and y so that a*x + b*y equals the gcd. What is the value of x printed at the end?
(old_r, r) = (a, b); (old_x, x) = (1, 0); (old_y, y) = (0, 1)
while r != 0
    q = old_r div r
    (old_r, r) = (r, old_r - q*r)
    (old_x, x) = (x, old_x - q*x)
    (old_y, y) = (y, old_y - q*y)
end while
Print old_x
  1. A. 47
  2. B. -9
  3. C. 2
  4. D. 38

Correct answer: B. -9

Tracking the coefficient of a alongside the remainders gives the Bezout pair; the first printed coefficient is x = -9 for this run. Printing the y coefficient, printing the gcd itself, and adding the two coefficients (38) each give other values.

More Pseudocode and Code Tracing questions

Exams that ask this topic