Question:medium

Consider the following ANSI C 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 SomeFunction(15, 255) is \(\underline{\hspace{2cm}}\).

Show Hint

Repeated subtraction in recursion is a form of the Euclidean algorithm for GCD.
Updated On: Feb 2, 2026
Show Solution

Correct Answer: 15

Solution and Explanation

To solve the problem, we need to understand what the given C function SomeFunction does. Let's break it down:

  • Base cases:
    1. If either x==1 or y==1, the function returns 1.
    2. If x==y, it returns x (or y, since they are equal).
  • Recursive Reduction:
    1. If x>y, the function is called recursively with (x-y, y)
    2. If y>x, the recursion occurs with (x, y-x).

The function effectively computes the greatest common divisor (GCD) of x and y using a recursive form of the Euclidean algorithm.

Let's apply this to find SomeFunction(15, 255):

  1. x=15, y=255: Since y>x, call SomeFunction(15, 255-15).
  2. x=15, y=240: Still y>x, call SomeFunction(15, 240-15).
  3. Repeat the above step, reducing y by 15 each time, until:
  4. x=15, y=15: The condition x==y is met, so return 15.

Thus, the value returned by SomeFunction(15, 255) is 15.

Finally, the computed value 15 falls within the given range [15, 15].

InputsTraceResult
15, 25515, 240→ ... → 15, 15
Was this answer helpful?
0