GATE / CS ARCHIVE

GATE CSE 1995 / QUESTION 2.3

Repeated subtraction algorithm

Algorithms · Correctness, Invariants & Design · Full GATE CSE 1995 paper

GATE 1995, Q2.3 2 marks · MCQ
Report issue

Correctness, Invariants & Design

Question 2.3, GATE 1995: For positive integers X,Y, repeatedly replace the larger by its difference from the smaller until X=Y, then print X. What is computed?
Tap to enlarge
Your answer
Answer & solution

Answer: C: gcd of the original integers (no official key was published)

For X>Y, gcd(X,Y)=gcd(X−Y,Y), because the common divisors are unchanged by subtraction. The symmetric identity holds when Y>X. Thus every loop preserves the original gcd. Both variables remain positive, and their sum strictly decreases on each iteration, so the process terminates. At termination X=Y, whose gcd is that common value. The printed result is therefore the original gcd, not their lcm or the quotient of a single division.

Original paper, page 3