示例#1
0
        public void VerifiesDecidersAgainstKnownOutput()
        {
            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));
            }
        }
示例#2
0
        public TrialDivisionFactorizer(int limit)
        {
            Limit = limit;

            _sieveFactorizer = new SieveOfEratosthenesFactorizer(
                Convert.ToInt32(Math.Sqrt(Limit)), needsToProvide: true);
        }
        public TrialDivisionFactorizer(int limit)
        {
            Limit = limit;

            _sieveFactorizer = new SieveOfEratosthenesFactorizer(
                limit: Convert.ToInt32(Math.Sqrt(Limit)),
                needsToProvide: true);
        }
示例#4
0
 public void VerifiesDistinctPrimeFactorsAgainstNaiveFactorizer()
 {
     var sieveFactorizer = new SieveOfEratosthenesFactorizer(3481);
     var trialDivisionFactorizer = new TrialDivisionFactorizer(3481);
     for (int n = 0; n <= 3481; ++n)
     {
         CollectionAssert.AreEquivalent(
             NaivePrimeDeciderProviderFactorizer.GetDistinctPrimeFactors(n).ToArray(),
             sieveFactorizer.GetDistinctPrimeFactors(n).ToArray());
         CollectionAssert.AreEquivalent(
             NaivePrimeDeciderProviderFactorizer.GetDistinctPrimeFactors(n).ToArray(),
             trialDivisionFactorizer.GetDistinctPrimeFactors(n).ToArray());
     }
 }
示例#5
0
        public void VerifiesDistinctPrimeFactorsAgainstKnownOutput()
        {
            var sieveFactorizer = new SieveOfEratosthenesFactorizer(1000);
            var trialDivisionFactorizer = new TrialDivisionFactorizer(1000);

            foreach (var numberPrimeFactorsPair in numberPrimeFactorsPairs)
            {
                int number = numberPrimeFactorsPair.Item1;
                int[] distinctPrimeFactors = numberPrimeFactorsPair.Item2.Distinct().ToArray();

                CollectionAssert.AreEquivalent(distinctPrimeFactors, sieveFactorizer.GetDistinctPrimeFactors(number).ToArray());
                CollectionAssert.AreEquivalent(distinctPrimeFactors, trialDivisionFactorizer.GetDistinctPrimeFactors(number).ToArray());
                CollectionAssert.AreEquivalent(distinctPrimeFactors, NaivePrimeDeciderProviderFactorizer.GetDistinctPrimeFactors(number).ToArray());
            }
        }
示例#6
0
 public void VerifiesDecidersAgainstNaiveDecider()
 {
     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));
         }
     }
 }
示例#7
0
        private void SieveOfEratosthenesFactorizer(int limit, int passes, int start, int end)
        {
            var factorizer = new SieveOfEratosthenesFactorizer(limit);

            for (int p = 0; p < passes; ++p)
            {
                for (int n = start; n <= end; ++n)
                {
                    factorizer.GetPrimeFactors(n).ToArray();
                }
            }
        }