Ejemplo n.º 1
0
        public void Multiply(Natural factor1, Natural factor2)
        {
            if (factor1 == 0 || factor2 == 0)
            {
                factor1.SetValue(Natural.Zero);
            }
            else if (factor2 == 1)
            {
                // factor1 = factor1    (jupp, that's the only optimization)
            }
            else
            {
                Natural product = Natural.Zero;

                for (int i = 0; i < factor1.usedDigits; i++)
                {
                    Digit d = factor1.digits[i];

                    Natural partial = MulDigit(factor2, d);
                    Natural.ShiftLeft(partial, i);

                    Natural.Add(product, partial);
                }

                factor1.SetValue(product);
            }
        }
        public void Multiply(Natural factor1, Natural factor2)
        {
            if (factor1 == Natural.Zero || factor2 == Natural.Zero)
            {
                factor1.SetValue(Natural.Zero);
            }
            else if (factor1 == Natural.One)
            {
                factor1.SetValue(factor2);
            }
            else
            {
                Natural increase = factor1.Clone();
                Natural decrease = factor2.Clone();

                factor1.PushLength(factor1.usedDigits * factor2.usedDigits);

                while (decrease > 1) // We already have 1 factor1
                {
                    Natural.Add(factor1, increase);
                    Natural.Decrement(decrease);
                }

                factor1.PopLength();
            }
        }
 public void DivRem(Natural dividend, Natural divisor)
 {
     Natural quotient, remainder;
     DivRem(dividend, divisor, out quotient, out remainder);
     dividend.SetValue(quotient);
     divisor.SetValue(remainder);
 }
Ejemplo n.º 4
0
 public void Subtract(Natural minuend, Natural subtrahend)
 {
     Natural difference;
     Subtract(minuend, subtrahend, out difference);
     minuend.SetValue(difference);
 }