Пример #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
        public static bool Strong(ulong n, ulong a)
        {
            if (n == 2)
            {
                return(true);
            }
            if (n < 2 || (n & 1) == 0)
            {
                return(false);
            }
            ulong d = (n - 1) >> 1, s = 1;

            for (; (d & 1) == 0; d >>= 1, s++)
            {
                ;
            }
            a = Power.BinaryMod(a, d, n);
            if (a == 1 || a == n - 1)
            {
                return(true);
            }
            for (ulong r = 1; r < s; r++)
            {
                a = (a * a) % n;
                if (a == 1)
                {
                    return(false);
                }
                if (a == n - 1)
                {
                    return(true);
                }
            }
            return(false);
        }
Пример #3
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);
        }
Пример #4
0
 public static bool Fermat(ulong n, ulong a)
 {
     if (n == 2)
     {
         return(true);
     }
     if (n < 2 || (n & 1) == 0)
     {
         return(false);
     }
     return(Power.BinaryMod(a, n - 1, n) == 1);
 }
Пример #5
0
 public static bool Euler(ulong n, ulong a)
 {
     if (n == 2)
     {
         return(true);
     }
     if (n < 2 || (n & 1) == 0)
     {
         return(false);
     }
     a = Power.BinaryMod(a, (n - 1) >> 1, n);
     return(a == 1 || a == n - 1);
 }
Пример #6
0
        public static short?Legendre(long a, long prime)
        {
            short s = (short)Power.BinaryMod((ulong)a, (ulong)((prime - 1) >> 1), (ulong)prime);

            if (s == 0 || s == 1)
            {
                return(s);
            }
            if (s == a - 1)
            {
                return(-1);
            }
            return(null);
        }
Пример #7
0
        public static bool EulerJacobi(ulong n, ulong a)
        {
            if (n == 2)
            {
                return(true);
            }
            if (n < 2 || (n & 1) == 0)
            {
                return(false);
            }
            long jac = Symbol.Jacobi(a, n);

            return((long)Power.BinaryMod(a, (n - 1) >> 1, n) == (jac < 0 ? (long)n - 1 : jac));
        }
Пример #8
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);
        }
Пример #9
0
        public static bool MillerRabin(ulong n, ulong iterations)
        {
            if (n == 2)
            {
                return(true);
            }
            if (n < 2 || (n & 1) == 0)
            {
                return(false);
            }
            ulong d = (n - 1) >> 1, r = 1;

            for (; (d & 1) == 0; d >>= 1, r++)
            {
                ;
            }
            for (ulong i = 0; i < iterations; i++)
            {
c:
                ulong x = Power.BinaryMod((ulong)random.Next(2, (int)(n - 1)), d, n);
                if (x == 1 || x == n - 1)
                {
                    continue;
                }
                for (ulong j = 0; j < r - 1; j++)
                {
                    x = (x * x) % n;
                    if (x == 1)
                    {
                        return(false);
                    }
                    if (x == n - 1)
                    {
                        goto c;
                    }
                }
                return(false);
            }
            return(true);
        }
Пример #10
0
        /// <summary>
        /// Applies Miller test to n.
        /// </summary>
        /// <param name="n">number to be tested</param>
        /// <returns>true if n is prime otherwise false</returns>
        public static bool MillerTest(ulong n)
        {
            if (n == 2)
            {
                return(true);
            }
            if (n < 2 || (n & 1) == 0)
            {
                return(false);
            }
            ulong d = (n - 1) >> 1, s = 1;

            for (; (d & 1) == 0; d >>= 1, s++)
            {
                ;
            }
            ulong max = (ulong)System.Math.Min(n - 1, System.Math.Floor(2 * System.Math.Pow(System.Math.Log(n), 2)));

            for (ulong a = 2; a <= max; a++)
            {
                ulong m = Power.BinaryMod(a, d, n);
                if (m != 1 && m != n - 1)
                {
                    for (ulong r = 1; r < s; r++)
                    {
                        if ((m = (m * m) % n) == n - 1)
                        {
                            goto c;
                        }
                    }
                    return(false);
                }
                c :;
            }
            return(true);
        }
Пример #11
0
        public static bool Pepin(ulong exponent)//fermat numbers
        {
            ulong n = (ulong)System.Math.Pow(2, System.Math.Pow(2, exponent));

            return(Power.BinaryMod(3, n >> 1, n + 1) == n);
        }
Пример #12
0
 public static bool BinomialTheorem(int p, int a, int b) => (int)Power.BinaryMod((ulong)(a + b), (ulong)p, (ulong)p) == (int)(Power.BinaryMod((ulong)a, (ulong)p, (ulong)p) + Power.BinaryMod((ulong)b, (ulong)p, (ulong)p)) % p;