最大公约数最优算法


/****
*辗转相除法,也叫欧几里得算法 在大数的时候很高效
*/
public static int gcd(int m, int n){
	    m = m<0?-m:m;
	    n = n<0?-n:n;
	    
		if(m==0)return n;
	    if(n==0)return m;

		int a = 0,b = 0; 
	    a = m>n?m:n;
	    b = m+n-a;
	    
	    int temp=0;
	    while(a%b!=0){
	    	temp = a%b;
	    	a = b;
	    	b = temp;
	    }
	    return b;
	}
	
    /****
    *更相减损发,出自九章算术
    */
	public static int gcd1(int m,int n){
		m = m<0?-m : m;
		n = n<0?-n : n;
		
		if(m==0)return n;
		if(n==0)return m;
		int x=0,y=0;
		x=m>n?m:n;
		y = m+n-x;
		
		while(x!=y){
			x = x-y;
			if(x

 

你可能感兴趣的:(Java)