Recursion example implmenting Euclid's algorithm for greatest common
denominator of two numbers
@author
@version
public class GCD{
Euclidean algorithm for calculating the greatest common denominator
(GCD) of two numbers.
@param a
@param b
@return
public static long gcd( long a, long b ){
System.out.println( "gcd("+a+","+b+")");
if( b == 0 ){
return a;
}else{
return gcd( b, a % b );
}
}
public static void main( String[] args ){
long num1 = 100;
long num2 = 8;
long result = gcd( num1, num2 );
System.out.println("gcd("+num1+","+num2+")="+result+" (in main)\n");
num1 = 8;
num2 = 100;
result = gcd( num1, num2 );
System.out.println("gcd("+num1+","+num2+")="+result+" (in main)\n");
num1 = 200;
num2 = 13;
result = gcd( num1, num2 );
System.out.println("gcd("+num1+","+num2+")="+result+" (in main)\n");
}
}