Created
November 23, 2018 10:41
-
-
Save thinkphp/5f0959f502519ccbdcfdebf020cf165b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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