示例#1
0
        public static bool Lucas(ulong n)
        {
            var random        = new System.Random();
            var factorisation = Factorise.Optimised(n - 1);

            for (ulong a = 2; a < n;)
            {
c:
                if (GCD.Standard(a, n) > 1)
                {
                    return(false);
                }
                if (Power.BinaryMod(a, n - 1, n) == 1)
                {
                    foreach (uint prime in factorisation)
                    {
                        if (Power.BinaryMod(a, (n - 1) / prime, n) == 1)
                        {
                            a++;
                            goto c;
                        }
                    }
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            return(false);
        }
示例#2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="n"></param>
        /// <param name="iterations"></param>
        /// <returns>true if n is prime, false if n is possibly composite</returns>
        public static bool Lucas(ulong n, ulong iterations)
        {
            ulong[] factorisation = Factorise.Optimised(n - 1);
            for (ulong i = 0; i < iterations; i++)
            {
c:
                ulong a = (ulong)random.Next(2, (int)n);
                if (GCD.Standard(a, n) > 1)
                {
                    return(false);
                }
                if (Power.BinaryMod(a, n - 1, n) == 1)
                {
                    foreach (uint prime in factorisation)
                    {
                        if (Power.BinaryMod(a, (n - 1) / prime, n) == 1)
                        {
                            goto c;
                        }
                    }
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            return(false);
        }
示例#3
0
        public static bool Pocklington(ulong n)
        {
            ulong[] primes = Sieve.Standard((ulong)System.Math.Log(n - 1)).Cast <ulong>().ToArray();
            ulong   f = n - 1, sqrt = (ulong)System.Math.Sqrt(n);

            foreach (ulong prime in primes)
            {
                if (f / prime < sqrt)
                {
                    goto g;
                }
                while (f % prime == 0)
                {
                    if (f / prime < sqrt)
                    {
                        goto g;
                    }
                    f /= prime;
                }
            }
g:
            var factors = Factorise.Standard(f, (z) => (ulong)Factoring.Special.TrialDivision((int)z));

            for (uint i = 2; i < n; i++)
            {
                if (Power.BinaryMod(i, n - 1, n) == 1)
                {
                    bool isPrime = true;
                    foreach (uint factor in factors)
                    {
                        if (GCD.Standard((uint)Power.Binary(i, (n - 1) / factor) - 1, n) != 1)
                        {
                            isPrime = false;
                            break;
                        }
                    }
                    if (isPrime)
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }