예제 #1
0
 public void nextNumber()
 {
     CurrentNumber = random.Next(1, 1000);
     if (IsPrime(CurrentNumber))
     {
         PrimeFound?.Invoke(this, EventArgs.Empty);
     }
 }
예제 #2
0
        // <summary>
        // Starting from 2, keep finding the next larger prime number until cancelled.
        // This method uses the "Sieve of Eratosthenes" as explained here: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
        // 1. Create a list of consecutive integers from 2 through n: (2, 3, 4, ..., n).
        // 2. Initially, let p equal 2, the smallest prime number.
        // 3. Enumerate the multiples of p by counting to n from 2p in increments of p, and mark them in the list (these will be 2p, 3p, 4p, ... ; the p itself should not be marked).
        // 4. Find the first number greater than p in the list that is not marked. If there was no such number, stop.
        // Otherwise, let p now equal this new number(which is the next prime), and repeat from step 3.
        /// </summary>
        public void FindMaxPrime(CancellationTokenSource cts)
        {
            MaxPrime = 2;

            int currentNumber = 0;

            int outerCounter = 0;

            var outerTotal = EndPrimeRange - MaxPrime;

            // Add the first prime
            Primes.Add(MaxPrime);

            while (outerCounter++ <= outerTotal)
            {
                for (currentNumber = MaxPrime; currentNumber <= EndPrimeRange; currentNumber++)
                {
                    if (cts.IsCancellationRequested)
                    {
                        return;
                    }

                    var notPrime = currentNumber * MaxPrime;

                    // Don't add it to the list if it's beyond the maximum range, or less than the already known max prime.
                    if (notPrime > EndPrimeRange)
                    {
                        break;
                    }

                    if (notPrime < MaxPrime)
                    {
                        continue;
                    }

                    // Check against some known prime numbers.
                    if (IsDivisibleByKnownPrimes(notPrime))
                    {
                        continue;
                    }

                    // Add it to the known list of not primes
                    NotPrimes.Add(notPrime);
                }

                // Find the prime candidates greater than MaxPrime
                for (currentNumber = MaxPrime + 1; currentNumber <= EndPrimeRange; currentNumber++)
                {
                    if (cts.IsCancellationRequested)
                    {
                        return;
                    }

                    // Don't add it to the list if it's beyond the maximum range, or less than the already known max prime.
                    if (currentNumber > EndPrimeRange || currentNumber < MaxPrime)
                    {
                        continue;
                    }

                    if (IsDivisibleByKnownPrimes(currentNumber))
                    {
                        continue;
                    }

                    // Check to see if we've found a new maximum prime
                    if (!NotPrimes.Contains(currentNumber))
                    {
                        MaxPrime = currentNumber;

                        // Only keep a limited list of known primes to reduce checking time.
                        if (Primes.Count < 30)
                        {
                            Primes.Add(MaxPrime);
                        }

                        // Notify the view model that it's time for an update.
                        PrimeFound?.Invoke(this, EventArgs.Empty);
                        break;
                    }
                }
            }
        }