Example #1
0
        public void TestMultiplePrimeChecks()
        {
            // expected list provided by http://www.primos.mat.br/primeiros_10000_primos.txt
            var generator = new SieveOfEratosthenesPrimeNumberGenerator();

            Assert.AreEqual(new[] { 7901, 7907, 7919 },
                            generator.generate(7900, 7920),
                            "Prime number results for the range of 7900<=>7920 did not match expected");
            Assert.AreEqual(new[] { 7901, 7907, 7919, 7927, 7933, 7937, 7949, 7951, 7963, 7993, 8009 },
                            generator.generate(7900, 8010),
                            "Prime number results for the range of 7900<=>8010 did not match expected");
            Assert.AreEqual(new[] { 7901, 7907, 7919 },
                            generator.generate(7900, 7920),
                            "Prime number results for the range of 7900<=>7920 did not match expected");
        }
Example #2
0
        internal static void Execute(Action <string, object, object> output, int first, int second, string third)
        {
            // should use an IoC container for picking the algorithms instead of using `new`, but skipping that step for simplicity of this demo

            var stopwatch = new Stopwatch();

            if (third == "race")
            {
                // for fun, let's race algorithms:
                var generator  = new BasicPrimeNumberGenerator();
                var generator2 = new SieveOfEratosthenesPrimeNumberGenerator();
                var stopwatch2 = new Stopwatch();
                stopwatch.Start();
                var results = generator.generate(first, second);
                stopwatch.Stop();
                stopwatch2.Start();
                var results2 = generator2.generate(first, second);
                stopwatch2.Stop();
                output("\nBasicPrimeNumberGenerator               generated in {0}ms ({1} ticks)", stopwatch.ElapsedMilliseconds, stopwatch.ElapsedTicks);
                output("SieveOfEratosthenesPrimeNumberGenerator generated in {0}ms ({1} ticks)", stopwatch2.ElapsedMilliseconds, stopwatch2.ElapsedTicks);
            }
            else
            {
                // pick a generator
                var generator = third == "alternate" || third == "a" || third == "eratosthenes"
                                ? (IPrimeNumberGenerator) new SieveOfEratosthenesPrimeNumberGenerator()
                                : new BasicPrimeNumberGenerator();

                stopwatch.Start();
                // start generating
                var results = generator.generate(first, second);
                stopwatch.Stop();

                output("Results: {0}{1}", string.Join(", ", results), "");
                output("\nResults generated in {0}ms ({1} ticks)", stopwatch.ElapsedMilliseconds, stopwatch.ElapsedTicks);
            }
        }