Exemple #1
0
 public BigInteger[] DivideAndRemainder(BigInteger val)
 {
     if (val.sign == 0)
     {
         throw new ArithmeticException("Division by zero error");
     }
     BigInteger[] integerArray = new BigInteger[2];
     if (this.sign == 0)
     {
         integerArray[0] = Zero;
         integerArray[1] = Zero;
         return integerArray;
     }
     if (val.QuickPow2Check())
     {
         int n = val.Abs().BitLength - 1;
         BigInteger integer = this.Abs().ShiftRight(n);
         int[] numArray = this.LastNBits(n);
         integerArray[0] = (val.sign == this.sign) ? integer : integer.Negate();
         integerArray[1] = new BigInteger(this.sign, numArray, true);
         return integerArray;
     }
     int[] x = (int[])this.magnitude.Clone();
     int[] mag = this.Divide(x, val.magnitude);
     integerArray[0] = new BigInteger(this.sign * val.sign, mag, true);
     integerArray[1] = new BigInteger(this.sign, x, true);
     return integerArray;
 }
Exemple #2
0
 public BigInteger Remainder(BigInteger n)
 {
     int[] numArray;
     if (n.sign == 0)
     {
         throw new ArithmeticException("Division by zero error");
     }
     if (this.sign == 0)
     {
         return Zero;
     }
     if (n.magnitude.Length == 1)
     {
         int m = n.magnitude[0];
         if (m > 0)
         {
             if (m != 1)
             {
                 int num2 = this.Remainder(m);
                 if (num2 != 0)
                 {
                     return new BigInteger(this.sign, new int[] { num2 }, false);
                 }
             }
             return Zero;
         }
     }
     if (CompareNoLeadingZeroes(0, this.magnitude, 0, n.magnitude) < 0)
     {
         return this;
     }
     if (n.QuickPow2Check())
     {
         numArray = this.LastNBits(n.Abs().BitLength - 1);
     }
     else
     {
         numArray = (int[])this.magnitude.Clone();
         numArray = this.Remainder(numArray, n.magnitude);
     }
     return new BigInteger(this.sign, numArray, true);
 }
Exemple #3
0
 public BigInteger Divide(BigInteger val)
 {
     if (val.sign == 0)
     {
         throw new ArithmeticException("Division by zero error");
     }
     if (this.sign == 0)
     {
         return Zero;
     }
     if (val.QuickPow2Check())
     {
         BigInteger integer = this.Abs().ShiftRight(val.Abs().BitLength - 1);
         if (val.sign != this.sign)
         {
             return integer.Negate();
         }
         return integer;
     }
     int[] x = (int[])this.magnitude.Clone();
     return new BigInteger(this.sign * val.sign, this.Divide(x, val.magnitude), true);
 }
Exemple #4
0
 public BigInteger Multiply(BigInteger val)
 {
     if ((this.sign == 0) || (val.sign == 0))
     {
         return Zero;
     }
     if (val.QuickPow2Check())
     {
         BigInteger integer = this.ShiftLeft(val.Abs().BitLength - 1);
         if (val.sign <= 0)
         {
             return integer.Negate();
         }
         return integer;
     }
     if (this.QuickPow2Check())
     {
         BigInteger integer2 = val.ShiftLeft(this.Abs().BitLength - 1);
         if (this.sign <= 0)
         {
             return integer2.Negate();
         }
         return integer2;
     }
     int num = ((this.BitLength + val.BitLength) / 0x20) + 1;
     int[] w = new int[num];
     if (val == this)
     {
         Square(w, this.magnitude);
     }
     else
     {
         Multiply(w, this.magnitude, val.magnitude);
     }
     return new BigInteger(this.sign * val.sign, w, true);
 }