Question
It's a follow-up of the answer I got from my last question on BINARYGCD. The algorithm terminates and BINARYGCD returns 1. How does it return
It's a follow-up of the answer I got from my last question on BINARYGCD. The algorithm terminates and BINARYGCD returns 1. How does it return 12 = 2 * 2 * 3 as GCD? I cannot trace where the program did this.
Original question:
Prove that the following algorithm computes gcd(x, y) the greatest common divisor of x and y, and show its worst-case running time. BINARYGCD(x,y): if x = y: return x else if x and y are both even: return 2*BINARYGCD(x/2,y/2) else if x is even: return BINARYGCD(x/2,y) else if y is even: return BINARYGCD(x,y/2) else if x > y: return BINARYGCD( (x-y)/2,y ) else return BINARYGCD( x, (y-x)/2 ) I saw the posted answer. It tried to get GCD for 132 and 84. The end result only returns 3 following the logic. How does it come up with 12? It didn't explain on the answer. I cannot get any line of the code where 3 * 3. Appreciate any guidance!
Answer from Chegg:
Regarding your question about how the result of 12 was obtained for the input (132, 84), the result should actually be 12. The algorithm was applied correctly in the answer you saw, but the mistake was made in the final step when computing the GCD of 33 and 21.
To see how the GCD of 132 and 84 is 12, we can go through the algorithm step by step:
We start by calling BINARYGCD(132, 84).
Since both 132 and 84 are even, we divide them both by 2 and compute the GCD of the resulting numbers. Specifically, we return 2 times the GCD of 66 and 42, which is 2 * BINARYGCD(66, 42).
Since both 66 and 42 are even, we divide them both by 2 and compute the GCD of the resulting numbers. Specifically, we return 2 times the GCD of 33 and 21, which is 2 * BINARYGCD(33, 21).
Since both 33 and 21 are odd, we use the last case in the algorithm and call BINARYGCD(12, 21).
Since 21 is odd and 12 is even, we use the fourth case in the algorithm and call BINARYGCD((21-12)/2, 12), which is the same as BINARYGCD(4, 12).
Since both 4 and 12 are even, we divide them both by 2 and compute the GCD of the resulting numbers. Specifically, we return 2 times the GCD of 2 and 6, which is 2 * BINARYGCD(2, 6).
Since both 2 and 6 are even, we divide them both by 2 and compute the GCD of the resulting numbers. Specifically, we return 2 times the GCD of 1 and 3, which is 2 * BINARYGCD(1, 3).
Since both 1 and 3 are odd, we use the last case in the algorithm and call BINARYGCD(1, 2).
Since 2 is even and 1 is odd, we use the third case in the algorithm and call BINARYGCD(1, 1).
Since both 1 and 1 are equal, we return either one as the GCD. In this case, we return 1.
The algorithm terminates, and we conclude that the GCD of 132 and 84 is 2 * 2 * 2 * 3, which is equal to 12.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started