This is my first problem:
gcd(x,y)
if (x < y)
gcd(y,x)
else
if (y = 0)
return x
else
return gcd(y, x mod y)
This is my second problem:
public static int Test2(int x, int y) {
if (x > y) {
return 10;
} else {
return Test2(x-5, y+5) + 5;
}
}
The question is: What is returned for gcd(84, 21)?
- a. 84
- b. 21
- c. 3 (This is the correct answer)
- d. 10
X equals 84 and y equals 21. So I run them through the Algorithm class. 84 is not less than 21 so I skip that if statement. 84 is not equal so I skip that statement. I go to return gcd(y, x mod y). I don’t understand what is mod and how do you figure out what it means?
Second problem!
Question: What is returned for Test2(18,5)?
- A. 5
- B. 10
I choose ten , because x is greater than y and when processed to the if statement. It returns a value of ten. The if statements does run anything but the return statement. - C. 15 The answer is 15.
- D. 20
modis the modulo function. It’s the rest when when you divide two integers. For example,10 is the correct answer for the second question, your argument is correct.