Exemple #1
0
        public static bool IsPrimeLowNumber(int n, int k)
        {
            Random rn = new Random((int)DateTime.Now.Ticks);

            for (int i = 0; i < k; i++)
            {
                int a = rn.Next(0, n - 1);
                if (AbvMath.GetGCDBinary(n, a) > 1)
                {
                    return(false);
                }
                BigInteger buf = BigInteger.ModPow(a, (n - 1) >> 1, n);
                switch (AbvMath.GetLegendreSymbolValue(a, n))
                {
                case 0:
                    return(false);

                case 1:
                    if (buf != 1)
                    {
                        return(false);
                    }
                    break;

                case -1:
                    if (buf != n - 1)
                    {
                        return(false);
                    }
                    break;
                }
            }
            return(true);
        }
Exemple #2
0
        /// <summary>
        /// Test is it num prime
        /// </summary>
        /// <param name="n">Number to test</param>
        /// <param name="k">Count of iterations (accuary = 1-2^-k)</param>
        /// <returns>true if n prime and false otherwise</returns>
        public static bool IsPrime(ref BigInteger n, int k)
        {
            Random rn  = new Random((int)DateTime.Now.Ticks);
            int    min = 0x7fffffff;

            if (n < min)
            {
                min = int.Parse(n.ToString()) - 1;
            }
            for (int i = 0; i < k; i++)
            {
                BigInteger a = rn.Next(0, min);
                if (AbvMath.GetGCDBinary(n, a) > 1)
                {
                    return(false);
                }
                BigInteger buf = BigInteger.ModPow(a, (n - 1) >> 1, n);
                switch (AbvMath.GetLegendreSymbolValue(a, n))
                {
                case 0:
                    return(false);

                case 1:
                    if (buf != 1)
                    {
                        return(false);
                    }
                    break;

                case -1:
                    if (buf != n - 1)
                    {
                        return(false);
                    }
                    break;
                }
            }
            return(true);
        }
Exemple #3
0
        private bool PrivateIsPrime(ref BigInteger n, int k)
        {
            Random     rn  = new Random();
            BigInteger one = BigInteger.One;

            for (_CurrentState = 0; _CurrentState < k; _CurrentState++)
            {
                OnStatusChanged(this);
                BigInteger a = rn.Next(0, 2147000000);
                if (AbvMath.GetGCDBinary(n, a) > 1)
                {
                    return(false);
                }
                BigInteger buf = BigInteger.ModPow(a, (n - 1) >> 1, n);
                switch (AbvMath.GetJacobiSymbolValue(a, n))
                {
                case 0:
                    return(false);

                case 1:
                    if (buf != 1)
                    {
                        return(false);
                    }
                    break;

                case -1:
                    if (buf != n - 1)
                    {
                        return(false);
                    }
                    break;
                }
            }
            return(true);
        }