Пример #1
0
 public HugeInteger div(HugeInteger hugeInt)
 {
     //return zero if numerator is less than denominator
     if (larger(hugeInt) == hugeInt)
     {
         return(new HugeInteger());
     }
     else
     {
         var newSign = sign != hugeInt.sign ? Sign.Negative : Sign.Positive;
         var divisor = hugeInt.clone();
         divisor.sign = Sign.Positive;
         var dividend = clone();
         dividend.sign = Sign.Positive;
         var quotient = new HugeInteger();
         //repeatedly subtract divisor from dividend until dividend is smaller than divisor
         while (dividend.larger(divisor) != divisor)
         {
             quotient = quotient.sum(Input("1"));
             dividend = dividend.diff(divisor);
         }
         quotient.sign = newSign;
         return(quotient);
     }
 }
Пример #2
0
        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));
        }