private HugeInteger larger(HugeInteger hugeInt) { if (length() > hugeInt.length()) { return(this); } if (hugeInt.length() > length()) { return(hugeInt); } var larger = this; for (int i = DIGITS_SIZE - 1; i >= 0; i--) { if (digits[i] > hugeInt.digits[i]) { larger = this; break; } else if (digits[i] < hugeInt.digits[i]) { larger = hugeInt; break; } } return(larger); }
public HugeInteger prod(HugeInteger hugeInt) { var carry = 0; var accumulator = new HugeInteger(new int[DIGITS_SIZE], Sign.Positive); var thisLength = length(); //Add one to length if room to account for carry thisLength = thisLength == DIGITS_SIZE ? thisLength : thisLength + 1; var hugeIntLength = hugeInt.length(); //Add one to length if room to account for carry hugeIntLength = hugeIntLength == DIGITS_SIZE ? hugeIntLength : hugeIntLength + 1; for (int i = 0; i < thisLength; i++) { var product = new int[DIGITS_SIZE]; var coefficientOne = digits[i]; //build an addend, partial product to be added to running sum for (int j = 0; j < hugeIntLength; j++) { var coefficientTwo = hugeInt.digits[j]; var total = coefficientOne * coefficientTwo + carry; product[i + j] = (total) % 10; carry = total >= 10 ? total / 10 : 0; } //add addened, partial product to running sum accumulator = accumulator.sum(new HugeInteger(product, Sign.Positive)); } var newSign = sign != hugeInt.sign ? Sign.Negative : Sign.Positive; return(new HugeInteger(accumulator.digits, newSign)); }