예제 #1
0
        public BigInteger remainder(BigInteger divisor)
        {
            if (divisor.sign == 0)
            {
                throw new ArithmeticException("BigInteger divide by zero");
            }
            int thisLen    = numberLength;
            int divisorLen = divisor.numberLength;

            if (((thisLen != divisorLen) ? ((thisLen > divisorLen) ? 1 : -1)
                 : Elementary.compareArrays(digits, divisor.digits, thisLen)) == LESS)
            {
                return(this);
            }
            int resLength = divisorLen;

            int[] resDigits = new int[resLength];
            if (resLength == 1)
            {
                resDigits[0] = Division.remainderArrayByInt(digits, thisLen,
                                                            divisor.digits[0]);
            }
            else
            {
                int qLen = thisLen - divisorLen + 1;
                resDigits = Division.divide(null, qLen, digits, thisLen,
                                            divisor.digits, divisorLen);
            }
            BigInteger result = new BigInteger(sign, resLength, resDigits);

            result.cutOffLeadingZeroes();
            return(result);
        }
예제 #2
0
        internal static bool isProbablePrime(BigInteger n, int certainty)
        {
            // PRE: n >= 0;
            if ((certainty <= 0) || ((n.numberLength == 1) && (n.digits[0] == 2)))
            {
                return(true);
            }
            // To discard all even numbers
            if (!n.testBit(0))
            {
                return(false);
            }
            // To check if 'n' exists in the table (it fit in 10 bits)
            if ((n.numberLength == 1) && ((n.digits[0] & 0XFFFFFC00) == 0))
            {
                return(Array.BinarySearch(primes, n.digits[0]) >= 0);
            }
            // To check if 'n' is divisible by some prime of the table
            for (int i = 1; i < primes.Length; i++)
            {
                if (Division.remainderArrayByInt(n.digits, n.numberLength,
                                                 primes[i]) == 0)
                {
                    return(false);
                }
            }
            // To set the number of iterations necessary for Miller-Rabin test
            int ix;
            int bitLength = n.bitLength();

            for (ix = 2; bitLength < BITS[ix]; ix++)
            {
                ;
            }
            certainty = Math.Min(ix, 1 + ((certainty - 1) >> 1));

            return(millerRabin(n, certainty));
        }