retagged by
5,041 views
11 votes
11 votes

Consider the following $\text{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 $\textrm{SomeFunction(15, 255)}$ is __________

retagged by

4 Answers

Best answer
6 votes
6 votes
This function is calculating the $\text{GCD}$ of the two numbers by repeated subtraction.

$\text{GCD}(15, 255) = 15.$ So it’ll return $15.$
edited by
1 votes
1 votes

1st call SomeFunction(15, 255)

2nd call SomeFunction(15, 240)

3rd call SomeFunction(15, 225)

……...

Now for each call 15 is subtracted from 255 and 255 is a multiple of 15.

once the call reaches SomeFunction(15, 15) it’ll return x i.e 15.

So output is 15.

Note: This is a method to find GCD by repeated subtraction.

0 votes
0 votes

1st call SomeFunction(15, 255)

2nd call SomeFunction(15, 240)

similarly ,

………….. function calling till it reaches to call Somefunction(15,15).

so, It will Return 15 as output .

Answer:

Related questions

40 votes
40 votes
11 answers
2