Friday, 13 September 2013

Writing a method that finds the Greatest Common Divisor between 2 integers USING RECURSION?

Writing a method that finds the Greatest Common Divisor between 2 integers
USING RECURSION?

Euclid's algorithm tells us how to compute the greatest common divisor
(GCD) of two positive integers a and b. Using Euclid's algorithm, to find
the GCD of 206 and 40 (for example), first find the remainder when 206 is
divided by 40. Then find the GCD of 40 and this remainder (which turns out
to be 6), using the same idea again. When you reach the point where the
second number is 0, the first number will be the GCD of 206 and 40 that
you were looking for, as shown below.
gcd(206, 40) = gcd(40, 6) = gcd(6, 4) = gcd(4, 2) = gcd(2, 0) = 2
Write a method called gcd. YOU MUST USE RECURSION IN THIS METHOD (calling
the method within the method). This method should use Euclid's algorithm
to return the greatest common divisor of two positive integers.
So basically I don't know how to do this without recursion.. Please help!
:( I have been stuck on this for so long.. I wrote a method, but it
doesn't use recursion and it only woks for the given 206 and 40..

No comments:

Post a Comment