Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created November 23, 2018 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thinkphp/5f0959f502519ccbdcfdebf020cf165b to your computer and use it in GitHub Desktop.
Save thinkphp/5f0959f502519ccbdcfdebf020cf165b to your computer and use it in GitHub Desktop.
public class Euclid {
//recursive implementation
public static int gcd(int x, int y) {
if( y == 0 ) return x;
else return gcd( y, x % y );
}
//iterative implementation
public static int gcd_rec(int x, int y) {
while( y != 0 ) {
int holder = y;
y = x % y;
x = holder;
}
return x;
}
public static void main(String[] args) {
int x = Integer.parseInt(args[0]);
int y = Integer.parseInt(args[1]);
int r = gcd(x, y);
System.out.println("Greatest Common Divisor ( " + x + ", " + y + " ) = " + r);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment