package myAlgotithmTest;
/**
* 求最大公约数
* @author chao
*
*/
public class CommonDivisor {
/**
* 求x和y的最大公约数
* 原理:x除以y的余数z,z与y的最大公约数和x与y的最大公约数是相同的
* @param x
* @param y
* @return
*/
public static int getCommonDivisor(int x,int y){
if(x>y){
int t=x;
x=y;
y=t;
}
if(x==0){
return y;
}
return getCommonDivisor(x, y%x);
}
public static void main(String []args){
long start=System.currentTimeMillis();
System.out.println("最大公约数:"+getCommonDivisor(1024, 256));
long end=System.currentTimeMillis();
System.out.println("时间:"+(end-start));
}
}