Greatest Common Divisor

 public int gcd(int[] A){
  if(A==null|A.length==0) return 0;
  int gcd = A[0];
  for(int i =1; i<A.length;i++){
    gcd = egcd(gcd,A[i]);
  }
   return gcd;
 }
 //Euclidean algorithm
 public int egcd(int a, int b) {
       if(a==0)
       return b;

       while(b!=0){
       if(a>b){
       a = a-b;
       }
       else{
       b=b-a;
       }
       }
       return a;
    }

results matching ""

    No results matching ""