in Algorithms retagged by
4,937 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 __________

in Algorithms retagged by
by
4.9k views

4 Answers

6 votes
6 votes
Best answer
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 comment

Made the edit again, GCD of something with 15 can't be 17 as 17 doesn't divide 15.
0
0
1 vote
1 vote

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.

1 vote
1 vote

It will return 15.

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