Beispiel #1
0
        protected void GeneratePrimes()
        {
            IIsPrime checker = new IsPrimeV7();

            Stopwatch sw = new Stopwatch();

            sw.Restart();

            int          number       = int.MaxValue;
            List <ulong> primeNumbers = new List <ulong>();

            this.Primes[0] = false;
            this.Primes[1] = false;

            if (this.UseParallel)
            {
                int maxTestNumber = (int)Math.Sqrt(number) + 1;
                Parallel.For(2, maxTestNumber, i =>
                {
                    if ((this.Primes[i]) &&
                        (checker.CheckIsPrime((uint)i)))
                    {
                        for (int j = (i * 2); j <= number && j > 0; j = j + i)
                        {
                            this.Primes[j] = false;
                        }
                    }
                });

                primeNumbers.Sort();
            }
            else
            {
                int maxTestNumber = (int)Math.Sqrt(number) + 1;
                for (int i = 2; i <= maxTestNumber; i++)
                {
                    if ((this.Primes[i]) &&
                        (checker.CheckIsPrime((uint)i)))
                    {
                        for (int j = (i * 2); j <= number && j > 0; j = j + i)
                        {
                            this.Primes[j] = false;
                        }
                    }
                }
            }

            this.Results = primeNumbers;

            sw.Stop();

            TIsPrimeV9M1.Logger.Info($"Prime Generation Took {Utilities.ElaspedTimeFormatter(sw)}");
        }
Beispiel #2
0
        /// <summary>
        ///
        /// </summary>
        protected void GeneratePrimes(ulong findPrimesTo)
        {
            if (findPrimesTo > (ulong)uint.MaxValue + 1)
            {
                throw new ArgumentOutOfRangeException("findPrimesTo");
            }

            IIsPrime checker = new IsPrimeV7();

            Stopwatch sw = new Stopwatch();

            sw.Restart();

            long startNumber = (this.Primes.Count > 0 ? (long)this.Primes.Last() : 2);

            ConcurrentBag <ulong> primeNumbers = new ConcurrentBag <ulong>();

            if (this.UseParallel)
            {
                Parallel.For(startNumber, (long)findPrimesTo + 1, i =>
                {
                    if (checker.CheckIsPrime((ulong)i))
                    {
                        primeNumbers.Add((ulong)i);
                    }
                });
            }
            else
            {
                if (startNumber == 2)
                {
                    primeNumbers.Add(2);
                }

                for (ulong i = (ulong)(startNumber % 2 == 0 ? startNumber + 1 : startNumber); i <= findPrimesTo; i = i + 2)
                {
                    if (checker.CheckIsPrime(i))
                    {
                        primeNumbers.Add(i);
                    }
                }
            }

            this.Primes.AddRange(primeNumbers.OrderBy(x => x));

            sw.Stop();

            IsPrimeV9.Logger.Info($"PrimeNumber Generation Took {Utilities.ElaspedTimeFormatter(sw)}");
        }