暴力查找:
public int getGreatestCommonDivisor(int a,int b){
int big = a>b ? a:b;
int small = a<b ? a:b;
if(big%small==0){
return small;
}
for(int i = 2; i <small/2; i++){
if(a%i==0 && b%i==0){
return i;
}
}
return 1;
}
辗转相除法:
public int getGreatestCommonDivisor(int a,int b){
int result = 1;
if(a > b){
result = gcd(a,b);
}else{
result = gcd(b,a);
}
return result;
}
public int gcd(int a,int b){
if(a%b == 0){
return b;
}else{
return gcd(b,a%b);
}
}