Problem

Find and Fix the ErrorsTo find the greatest common divisor of a pair of two non-negative i...

Find and Fix the Errors

To find the greatest common divisor of a pair of two non-negative integers:

• Continually form a “new pair” of integers by replacing the larger number with the positive difference of the two, until the smaller number is zero.

• The larger number of the final pair is the greatest common divisor of the original numbers. Return that number.

The following chart shows the calculation of the greatest common divisor of 100 and 38. On the last line of the chart, the smaller number is 0, therefore the greatest common divisor of 100 and 38 is 2.

Pair

Difference

New Pair

100, 38

100 − 38 = 62

62, 38

62, 38

62 − 38 = 24

38, 24

38, 24

38 − 24 = 14

24, 14

24, 14

24 − 14 = 10

14, 10

14, 10

14 − 10 = 4

10, 4

10, 4

10 − 4 = 6

6, 4

6, 4

6 − 4 = 2

4, 2

4, 2

4 − 2 = 2

2, 2

2, 2

2 − 2 = 0

2, 0

The following recursive method is a buggy attempt to implement this algorithm. Find the bug and fi x it. Make sure to thoroughly test the method. The method may work correctly under some circumstances.

public static int gcd(int small, int large){      if (small = = 0)           return large;      return gcd (small, large − small);}

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
Solutions For Problems in Chapter 8
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT