k = f(x, y)求最大公约数
1. 假设x = m*x1 m为质数且 y%m!=0, 所以k = f(m*x1, y) = f(x1, y)
2. 如果x = m*x1, y = m*y1, 所以 k = f(m*x1, m*y1) = m* f(x1, y1)
3. 如果不同时满足上述2点 则 k = f(x, y) = f(y, x-y) (x>=y)
private static boolean isEven(int num) {
return num%2==0 ? true : false;
}
private static int gcd(int m, int n) {
if(m<n) {
return gcd(n, m);
}
if(n==0){
return m;
}
if(isEven(m)) {
if(isEven(n)){
return 2 * gcd(m>>1, n>>1);
} else {
return gcd(m>>1, n);
}
}
else {
if(isEven(n)) {
return gcd(m, n>>1);
} else {
return gcd(n, m-n);
}
}
}