Esempio n. 1
0
        public void PrimeDeciders_AgreeWithKnownOutput()
        {
            var sieveDecider         = new SieveOfEratosthenesDecider(2);
            var sieveProvider        = new SieveOfEratosthenesProvider(2);
            var sieveFactorizer      = new SieveOfEratosthenesFactorizer(2);
            var trialDivisionDecider = new TrialDivisionDecider(2);

            for (int n = 0; n <= 2; ++n)
            {
                Assert.AreEqual(_primesUpTo2.Contains(n), sieveDecider.IsPrime(n));
                Assert.AreEqual(_primesUpTo2.Contains(n), sieveProvider.IsPrime(n));
                Assert.AreEqual(_primesUpTo2.Contains(n), sieveFactorizer.IsPrime(n));
                Assert.AreEqual(_primesUpTo2.Contains(n), trialDivisionDecider.IsPrime(n));
                Assert.AreEqual(_primesUpTo2.Contains(n), NaivePrimeDeciderProviderFactorizer.IsPrime(n));
            }

            sieveDecider         = new SieveOfEratosthenesDecider(49);
            sieveProvider        = new SieveOfEratosthenesProvider(49);
            sieveFactorizer      = new SieveOfEratosthenesFactorizer(49);
            trialDivisionDecider = new TrialDivisionDecider(49);
            for (int n = 0; n <= 49; ++n)
            {
                Assert.AreEqual(_primesUpTo49.Contains(n), sieveDecider.IsPrime(n));
                Assert.AreEqual(_primesUpTo49.Contains(n), sieveProvider.IsPrime(n));
                Assert.AreEqual(_primesUpTo49.Contains(n), sieveFactorizer.IsPrime(n));
                Assert.AreEqual(_primesUpTo49.Contains(n), trialDivisionDecider.IsPrime(n));
                Assert.AreEqual(_primesUpTo49.Contains(n), NaivePrimeDeciderProviderFactorizer.IsPrime(n));
            }
        }
Esempio n. 2
0
        private void SieveOfEratosthenesDecider(int limit, int passes, int start, int end)
        {
            var decider = new SieveOfEratosthenesDecider(limit);

            for (int p = 0; p < passes; ++p)
            {
                for (int n = start; n <= end; ++n)
                {
                    decider.IsPrime(n);
                }
            }
        }
Esempio n. 3
0
 public void PrimeDeciders_AgreeWithNaiveDecider()
 {
     for (int i = 1000; i <= 10000; i += 1000)
     {
         var sieveDecider         = new SieveOfEratosthenesDecider(i);
         var sieveProvider        = new SieveOfEratosthenesProvider(i);
         var sieveFactorizer      = new SieveOfEratosthenesFactorizer(i);
         var trialDivisionDecider = new TrialDivisionDecider(i);
         for (int n = 0; n <= i; ++n)
         {
             Assert.AreEqual(NaivePrimeDeciderProviderFactorizer.IsPrime(n), sieveDecider.IsPrime(n));
             Assert.AreEqual(NaivePrimeDeciderProviderFactorizer.IsPrime(n), sieveProvider.IsPrime(n));
             Assert.AreEqual(NaivePrimeDeciderProviderFactorizer.IsPrime(n), sieveFactorizer.IsPrime(n));
             Assert.AreEqual(NaivePrimeDeciderProviderFactorizer.IsPrime(n), trialDivisionDecider.IsPrime(n));
         }
     }
 }
Esempio n. 4
0
 public bool IsPrime(int n)
 => _decider.IsPrime(n);