/** * DAC Algorithm to multiply two long numbers * * @param a : Input object of BigArithmetic class * @param b : Input object of BigArithmetic class * @return : Returns an object of the BigArithmetic class which is a product of the two inputs */ public static G05_BigArithmetic product(G05_BigArithmetic a, G05_BigArithmetic b) { // If any of the two lists is null return null if (a == null || b == null) { return null; } // We remove the MS zeros to get the effective size of the two numbers. G05_BigArithmetic x = a.removeMSZeros(); G05_BigArithmetic y = b.removeMSZeros(); /*If the size is zero, then the list is 0, so return zero - Multiply by zero */ if (x.effectiveSize == 0 || y.effectiveSize == 0) { return new G05_BigArithmetic(0L); } // Call the productBA for DAC G05_BigArithmetic c = productBA(x, y); return c; }
/** * Method to subtract two large numbers * * @param a : Input a - an object of the BigArithmetic Class. * @param b : Input b - Second object of the BigArithmetic Class. * @return : An object of the type BigArithmetic, that contains the result of the subtraction. */ public static G05_BigArithmetic subtract(G05_BigArithmetic a, G05_BigArithmetic b) { boolean sign = false, bo = false; // Two boolean flags to represent sign and borrow /* * If second number is bigger than first, swap the two and set sign to true * We will return zero if sign is still set to true - Level 1 - No negative nos. */ if (a.number.size() < b.number.size()) { G05_BigArithmetic t = b; b = a; a = t; sign = true; } /* * If the size of the two numbers are equal, then iterate to see if second is bigger than first. * If yes, swap and set sign as true like the previous case. * Else, we are good to continue with the subtraction, so break out of the loop. */ else if (a.number.size() == b.number.size()) { for (int i = a.number.size() - 1; i >= 0; i--) { if (a.number.get(i) < b.number.get(i)) { sign = true; G05_BigArithmetic t = b; b = a; a = t; continue; } else break; } } // Iterators for looping through the linked list. Iterator<Long> ita = a.number.iterator(); Iterator<Long> itb = b.number.iterator(); G05_BigArithmetic result = new G05_BigArithmetic(); Long diff, firstElement = nextElement(ita), secondElement = nextElement(itb); while (secondElement != null) { if (bo) { if (firstElement == 0) { firstElement = 9L; bo = true; } else { firstElement = firstElement - 1; bo = false; } } if (firstElement < secondElement) { firstElement += 10; bo = true; } diff = firstElement - secondElement; result.number.add(diff % B); firstElement = nextElement(ita); secondElement = nextElement(itb); } while (firstElement != null) { if (bo) { if (firstElement == 0) { firstElement = 9L; bo = true; } else { firstElement = firstElement - 1; bo = false; } } result.number.add(firstElement); firstElement = nextElement(ita); } // If sign, result is a negative number - so return zero if (sign) { return new G05_BigArithmetic(0L); } // Remove the most significant zeros from the list. result.removeMSZeros(); // If after removing the zeros the size is zero, we return the answer as zero if (result.effectiveSize == 0) { return new G05_BigArithmetic(0L); } return result; }