Example #1
0
 /**
  * Method to multiply the two Big Arithmetic objects
  *
  * @param a : Input - object of class BigArithmetic
  * @param b : Input - object of class BigArithmetic
  * @return : Returns the product of the two inputs.
  */
 public static G05_BigArithmetic productBA(G05_BigArithmetic a, G05_BigArithmetic b) {
   int maxSize = Math.max(a.effectiveSize, b.effectiveSize); // Get the max size of the two lists
   // If max size is one - normal single digit multiplication
   if (maxSize <= 1) {
     return new G05_BigArithmetic(a.number.get(0) * b.number.get(0));
   }
   // If any of the lists is small - pad zeros to make the size equal
   if (a.number.size() < maxSize) {
     a.addZero(maxSize - a.number.size());
   } else if (b.number.size() < maxSize) {
     b.addZero(maxSize - b.number.size());
   }
   // Get the middle of the two lists
   int mid = (maxSize + 1) / 2;
   // Find a1, a2, b1 and b2
   G05_BigArithmetic a1 = a.split(0, mid);
   G05_BigArithmetic a2 = a.split(mid, maxSize);
   G05_BigArithmetic b1 = b.split(0, mid);
   G05_BigArithmetic b2 = b.split(mid, maxSize);
   G05_BigArithmetic temp1 = productBA(a1, b1); // a1*b1
   G05_BigArithmetic temp2 = productBA(a2, b2); // a2*b2
   G05_BigArithmetic temp12 =
       productBA(G05_BigArithmetic.add(a1, a2), G05_BigArithmetic.add(b1, b2)); // (a1+a2)*(b1+b2)
   // [(a1+a2)*(b1+b2) - {(a1*b1) + (a2*b2)}]
   G05_BigArithmetic temp =
       G05_BigArithmetic.subtract(temp12, G05_BigArithmetic.add(temp1, temp2));
   temp12 = temp;
   // Pad zeroes - similar to raising to base
   temp12 = temp12.addLeastZero(mid);
   temp2 = temp2.addLeastZero(mid * 2);
   // Return the product while removing MS Zeros
   return G05_BigArithmetic.add(G05_BigArithmetic.add(temp2, temp12), temp1).removeMSZeros();
 }