Ejemplo 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));
            }
        }
Ejemplo n.º 2
0
Archivo: APS.cs Proyecto: Dariasz/SPOJ
 static APS()
 {
     _factorizer = new SieveOfEratosthenesFactorizer(_limit);
     _sequence   = new long[_limit + 1];
     for (int n = 2; n <= _limit; ++n)
     {
         _sequence[n] = _sequence[n - 1] + _factorizer.GetFirstPrimeFactor(n);
     }
 }
Ejemplo n.º 3
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();
                }
            }
        }
Ejemplo n.º 4
0
        public void GetDistinctPrimeFactors_AgreesWithNaiveFactorizer()
        {
            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());
            }
        }
Ejemplo n.º 5
0
        public void GetDistinctPrimeFactors_AgreesWithKnownOutput()
        {
            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());
            }
        }
Ejemplo n.º 6
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));
         }
     }
 }
Ejemplo n.º 7
0
    public TrialDivisionFactorizer(int limit)
    {
        Limit = limit;

        _sieveFactorizer = new SieveOfEratosthenesFactorizer(
            limit: Convert.ToInt32(Math.Sqrt(Limit)),
            needsToProvide: true);
    }
Ejemplo n.º 8
0
 static ETF()
 {
     _factorizer = new SieveOfEratosthenesFactorizer(_limit);
 }
Ejemplo n.º 9
0
    public TrialDivisionFactorizer(long limit)
    {
        Limit = limit;

        _sieveFactorizer = new SieveOfEratosthenesFactorizer(Convert.ToInt32(Math.Sqrt(Limit)));
    }
Ejemplo n.º 10
0
 static ETF()
 {
     _factorizer = new SieveOfEratosthenesFactorizer(_limit);
 }
Ejemplo n.º 11
0
    public TrialDivisionFactorizer(long limit)
    {
        Limit = limit;

        _sieveFactorizer = new SieveOfEratosthenesFactorizer(Convert.ToInt32(Math.Sqrt(Limit)));
    }