private bool StripDigitsPrime(int prime)
        {
            int r = prime;
            int l = prime;
            int d = (int)Math.Pow(10, (int)Math.Log10(prime) + 1); // Divider 10, 100, 1000,...

            while (r >= 10)
            {
                r /= 10;      // Strip R digits.
                l %= d /= 10; // Strip L digits.
                if (!p.IsPrime(r) || !p.IsPrime(l))
                {
                    return(false);
                }
            }
            return(true);
        }
Exemple #2
0
        // Test for primairly using the Miller-Rabin test.

        // Every prime yield to a^(p − 1) mod p = 1 where a is a coprime.

        // Miller-Rabin pseudoprime: a^(n − 1) mod n = 1
        // Euler pseudoprime.......: a^(n − 1)/2 mod n = +-1
        public static void UnitTest()
        {
            Stopwatch s = Stopwatch.StartNew();

            Debug.Assert(Factorsx(7653241) == "7653241 = 251 x 30491", "UnitTest factorsx failed.");
            Debug.Assert(C(4, 2) == 6, "UnitTest C(4,2) failed.");
            Debug.Assert(fac(4) == 4 * 3 * 2 * 1, "UnitTest fac(4) failed.");
            Debug.Assert(kgv(2, 3, 5, 7, 11) == 2 * 3 * 5 * 7 * 11, "Unittest kgv failed.");
            //for (int i = 0; i < 10; i++) kgv(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
            Debug.Assert(kgv(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) == 2520, "Unittest kgv failed.");
            //foreach (var item in Permutation(123456L, 2)) Console.WriteLine(item);
            Debug.Assert(Permutation(123L, 1).Count() == 3, "UnitTest Permutation1 failed.");
            Debug.Assert(Permutation(1234567L).Count() == fac(7), "UnitTest Permutation2 failed.");
            Debug.Assert(gcd(42, 56) == 14, "UnitTest gcd failed.");
            Debug.Assert(IsPalindrome(12345678987654321) == true, "UnitTest Ispalindrome failed.");
            s = Stopwatch.StartNew();
            for (int i = 0; i < 2000; i++)
            {
                Debug.Assert(IsPrime(i) == p.IsPrime(i), String.Format("UnitTest Isprime({0}) failed.", i));
            }
            Console.WriteLine("Helper : {0} ms.", s.ElapsedMilliseconds);
        }
 private bool IsPrime(int n)
 {
     return(n > 0 && p.IsPrime(n));
 }