public void ScatteredLongTest(IPrimalityTest p)
        {
            foreach (long lTestValue in laScatteredLongPrimes)
            {
                Assert.IsTrue(p.IsPrime(lTestValue));
                Assert.IsFalse(p.IsComposite(lTestValue));

                Assert.IsTrue(p.IsComposite(lTestValue + 1));
                Assert.IsFalse(p.IsPrime(lTestValue + 1));
            }
        }
Пример #2
0
 public bool IsPrime(int iNum)
 {
     if (iNum > _pg.largestPrime())
     {
         return(_pt.IsPrime(iNum));
     }
     else
     {
         return(_pg.IsPrime(iNum));
     }
 }
 public void LargeSieveIntegerTest(IPrimalityTest p)
 {
     for (int i = 0; i <= pg.largestPrime(); i++)
     {
         Assert.AreEqual(pg.IsPrime(i), p.IsPrime(i));
     }
 }
Пример #4
0
        public bool IsPrime(ulong n)
        {
            if (n == 1)
            {
                // Not considered prime
                return(false);
            }

            if (m_primeTable.IsInPrimeTable(n))
            {
                // Known prime
                return(true);
            }

            ulong limit = (ulong)Math.Sqrt(n);

            // Resharper offers to turn this into LINQ, but according to APP, it's way slower!
            foreach (ulong p in m_primeTable)
            {
                // Only try primes lower than sqrt(n).
                if (p > limit)
                {
                    break;
                }
                if (n % p == 0)
                {
                    // Composite (p is a factor of n)
                    return(false);
                }
            }

            // Don't know - use backup method
            return(m_fallbackAlgorithm.IsPrime(n));
        }
 public void TestSmallIntegers(IPrimalityTest p)
 {
     for (int i = 0; i <= liPrimes[liPrimes.Count - 1]; i++)
     {
         Assert.AreEqual(liPrimes.Contains(i), p.IsPrime(i));
         Assert.AreEqual(!liPrimes.Contains(i), p.IsComposite(i));
     }
 }
Пример #6
0
 private void PerformFermatPseudoprimeTest(int number, Counter counter)
 {
     if (fermatPseudoprimeTest.IsPrime(number))
     {
         Console.WriteLine("Unfortunately, you chose a pseudoprime");
         counter.Decrement();
     }
 }
        private void IntegerTest(IPrimalityTest p)
        {
            bool b;

            for (int i = 0; i < 10000; i++)
            {
                b = p.IsPrime(i);
            }
        }
        public void ScatteredBigIntegerTest(IPrimalityTest p)
        {
            BigInteger b;

            for (int i = 0; i < saTestPrimes.Length; i++)
            {
                b = BigInteger.Parse(saTestPrimes[i]);
                Assert.IsTrue(p.IsPrime(b));
                Assert.IsFalse(p.IsComposite(b));

                Assert.IsFalse(p.IsPrime(b + 1));
                Assert.IsTrue(p.IsComposite(b + 1));
            }
            for (int i = 0; i < saTestPseudoPrimes.Length - 1; i++)
            {
                b = BigInteger.Parse(saTestPseudoPrimes[i]);
                Assert.IsTrue(p.IsComposite(b));
            }
        }
        private void BigIntegerTest(IPrimalityTest p)
        {
            bool       bIsPrime;
            BigInteger bStop = bStop = BigInteger.Pow(10, 30);

            for (BigInteger b = bStop - 10000; b < bStop; b++)
            {
                bIsPrime = p.IsPrime(b);
            }
        }
Пример #10
0
        private void PrimeBigIntegerTest(IPrimalityTest p)
        {
            bool       bIsPrime;
            BigInteger bPrime;

            foreach (string s in saTestPrimes)
            {
                bPrime   = BigInteger.Parse(s);
                bIsPrime = p.IsPrime(bPrime);
            }
        }
Пример #11
0
        private void LongTest(IPrimalityTest p)
        {
            bool b;
            long lStop = int.MaxValue;

            lStop += 10000;
            for (long l = lStop - 10000; l < lStop; l++)
            {
                b = p.IsPrime(l);
            }
        }
Пример #12
0
 private void PerformMillerRabinTest(int number, Counter counter)
 {
     if (millerRabinTest.IsPrime(number))
     {
         counter.Increment();
         Console.WriteLine("Result of Miller-Rabin Test is true, it means that you may have chosen a prime number");
     }
     else
     {
         Console.WriteLine("Result of Miller-Rabin Test is false, it means that you may have chosen a composite number");
     }
 }
        public void FullBattery(IPrimalityTest p)
        {
            Ticker t = new Ticker();

            Debug.WriteLine("#######");
            Debug.WriteLine(p.GetType().Name);
            Debug.WriteLine("-------");
            TestSmallIntegers(p);
            t.Tick("Small ints");
            ScatteredLongTest(p);
            t.Tick("Scattered Longs");
            LargeSieveIntegerTest(p);
            t.Tick("LargeSieve");
            ScatteredBigIntegerTest(p);
            t.Tick("Scattered Big");
            Assert.IsTrue(p.IsPrime(2147483647));
        }
Пример #14
0
 private void PerformAksTest(int number, Counter counter)
 {
     if (number < 100)
     {
         if (aksTest.IsPrime(number))
         {
             counter.Increment();
             Console.WriteLine("Result of AKS Test is true, it means that you may have chosen a prime number");
         }
         else
         {
             Console.WriteLine("Result of AKS Test is false, it means that you may have chosen a composite number");
         }
     }
     else
     {
         Console.WriteLine(" Your number is too big to provide AKS Test, so it will be skipped");
     }
 }