Java实现算法导论中最大公约数欧几里得算法

最大公约数的欧几里得算法,代码如下:

package cn.ansj;

public class GCD {
	public static void main(String args[]) {  
		GCD g = new GCD();  
		//d=gcd(a,b)=gcd(b,a mod b)
        int igcd = g.getGcd(42823, 6409);  
        System.out.println("Iterator Algorithm get gcd: " + igcd);  
        int rgcd=g.Euclid(42823, 6409);
        System.out.println("recursion Algorithm get gcd: " + rgcd); 
    }  
	
	/** 
     * author  欧几里得算法递归实现
     */  
	public int Euclid(int a,int b){
	    if (b==0) return a;
	    else return Euclid(b,a%b);
	}

    /** 
     * author  欧几里得算法迭代实现
     */  
    public int getGcd(int i, int j) {  
        int k;   
        while ((k=i%j) != 0) {            
            i = j;  
            j = k;  
        }  
        return j;  
    }  
}

执行结果:

Iterator Algorithm get gcd: 17
recursion Algorithm get gcd: 17


你可能感兴趣的:(Algorithm,Java,算法导论专栏)