Consider the following function:
- int SomeFunction (int x, int y)
- {
- if ((x==1) || (y==1)) return 1;
- if (x==y) return x;
- if (x > y) return SomeFunction(x-y, y);
- if (y > x) return SomeFunction (x, y-x);
- }
The value returned by is __________
GATE CSE 2021Algorithms › Program & Function TracingVerified
15
Recognize that the given recursive function computes the greatest common divisor (GCD) using repeated subtraction. For SomeFunction(15, 255), repeatedly subtract the smaller number 15 from the larger number 255 until both arguments become equal, then return that common value.
The recursive function is the repeated-subtraction version of Euclid's GCD algorithm. Since 255 is a multiple of 15, eventually the two arguments become equal to 15, and the function returns 15.
Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.