A formula for finding the greatest common divisor (GCD) of two numbers was formulated by the mathematician Euclid around 300 BCE. The GCD of two numbers is the largest number that will divide into...


A formula for finding the greatest common divisor (GCD) of two numbers was formulated by the mathematician Euclid around 300 BCE. The GCD of two numbers is the largest number that will divide into both numbers without any remainder. For example, the GCD of 12 and 16 is 4, the GCD of 18 and 12 is 6.


The basic algorithm is as follows:


Assume we are computing the GCD of two integers x and y. Follow the steps below:


1. Replace the larger of x and y with the remainder after (integer) dividing the larger number by the smaller one.
2. If x or y is zero, stop. The answer is the nonzero value.
3 If neither x nor y is zero, go back to step 1.


Here is an example listing the successive values of x and y:


  x          y
135         20       %(135 / 20) = 15
 15         20       %(20 / 15) = 5
 15          5       %(15 / 5) = 0

  0          5       GCD = 5




Write a recursive method that finds the GCD of two numbers using Euclid’s algorithm.


public class Arithmetic
{
   public static int gcd(int a, int b)
   {
      // Your work here
   }
}


Write a tester program for your GCD algorithm.


Coding Language: Java



Jun 08, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here