public static void TestPrimeNumbersAreCorrect()
        {
            SieveEratosthenes se = new SieveEratosthenes();
            var primeNums        = se.CalculatePrimeNumbers(1000);

            Assert.AreEqual(primeNums.ToList(), expectedPrimes, "SieveEratosThenes did not produce all expected prime numbers under 1000.");
        }
예제 #2
0
        static void Main(string[] args)
        {
            stopwatch = Stopwatch.StartNew();
            var timer = new System.Timers.Timer(1000);

            timer.Elapsed += HandleTimerElapsed;
            timer.Start();

            // Adjusted duration for running the calculation so we stop all execution within 60 seconds
            TimeSpan duration = TimeSpan.FromSeconds(RUNTIME_IN_SECONDS) - stopwatch.Elapsed;

            SieveEratosthenes se = new SieveEratosthenes();

            // using Tasks and CancellationToken to calculate prime numbers and stop by timeout
            CancellationTokenSource cts = new CancellationTokenSource();

            cts.CancelAfter(duration);
            Task.Run(() => se.CalculatePrimeNumbers(PRIME_NUMBER_LIMIT, cts.Token))
            .ContinueWith((t) =>
            {
                stopwatch.Stop();
                timer.Stop();
                timer.Elapsed -= HandleTimerElapsed;

                if (primeOutput != string.Empty)
                {
                    Console.Write(primeOutput);
                }

                Console.WriteLine(string.Format("Highest prime number calculated: {0}", highestPrimeNumber));
            });

            // Consumer task that builds the prime number display
            Task.Factory.StartNew(() =>
            {
                foreach (int value in se.PrimeNumbers.GetConsumingEnumerable(cts.Token))
                {
                    primeOutput       += string.Format("{0},", value);
                    highestPrimeNumber = value;
                }
            });

            Console.ReadLine();
        }
 public static void TestPrimeNumbersWithIntMax()
 {
     SieveEratosthenes se = new SieveEratosthenes();
     var primeNums        = se.CalculatePrimeNumbers(int.MaxValue - 1);
 }