InterventionPRO
2012-04-04 10:35:03 UTC
Consider the problem of finding the Greatest Common Divisor (GCD) of two positive integers a and b. It can be mathematically proved that if b<=a
GCD(a, b) = b, if (a mod b) = 0;
GCD(a, b) = GCD(b, a mod b), if (a mod b) != 0.
Write a recursive function called GCD with signature “public static int GCD(int a, int b)” that returns the greatest common divisor of two positive integers a and b with b <= a. Also write a java program to test your function
MUST use recursion This is my code
public class GCDNO1 {
public static int GCD(int a, int b)
{
if (a mod b == 0)
return b;
else if (a mod b !=0)
return GCD(b, a mod b);
else
return (a-1, GCD(a,b-1));
}
public static void main(String[] args) {
System.out.println("GCD(a,b)= " + GCD(a,b));
}
}
Thank you!