Пример #1
0
        public bool IsPrime(BigInteger number)
        {
            if (number < TWO)
            {
                return(false);
            }
            if (number == TWO)
            {
                return(true);
            }
            if (number == THREE)
            {
                return(true);
            }
            if (number % TWO == ZERO)
            {
                return(false);
            }

            if (BaseSequence == null)
            {
                BaseSequence = MillerRabinHelpers.RandomBases(10, number);
            }
            var        minone = number - ONE;      //should be even;
            var        d      = minone;
            BigInteger r      = 0;

            while (d % 2 == 0)
            {
                r++;
                d >>= 1;                 //divide by 2
            }

            foreach (var a in BaseSequence)
            {
                var x = a * d % number;
                if (x == ONE || x == minone)
                {
                    continue;
                }
                for (BigInteger i = r - ONE; i > ZERO; i--)
                {
                    x = BigInteger.ModPow(x, 2, number);
                    if (x == minone)
                    {
                        continue;
                    }
                }
                return(false);
            }
            return(true);
        }
Пример #2
0
        public bool IsPrime(BigInteger n)
        {
            if (BaseSequence == null)
            {
                BaseSequence = MillerRabinHelpers.JimSinclair2011();
            }

            //make sure n in odd and n > 3
            if (n < TWO)
            {
                return(false);
            }
            if (n == TWO || n == THREE)
            {
                return(true);
            }
            if (n % TWO == ZERO)
            {
                return(false);
            }
            var m = n - ONE;

            // u will be even, as n is required to be odd
            var  u = m >> 1;
            long t = 1;             //long should work for n up to 2^(2^63) ~ (2.7 pentillion digits)

            //while even
            while (u % TWO == ZERO)
            {
                t++;
                u >>= 1;
            }

            foreach (var b in BaseSequence)
            {
                var a = b;
                if (a >= n)
                {
                    a %= n;
                }
                if (a == 0)
                {
                    continue;
                }
                var x = BigInteger.ModPow(a, u, n);
                if (x == ONE || x == m)
                {
                    continue;
                }

                long i;
                for (i = 1; i < t; i++)
                {
                    x = x * x % n;
                    if (x == ONE)
                    {
                        return(false);
                    }
                    if (x == m)
                    {
                        break;
                    }
                }

                // if we didn't break, the number is composite
                if (i == t)
                {
                    return(false);
                }
            }
            return(true);
        }