Пример #1
0
        private async Task ProcessQueueMessage(CloudQueueMessage msg)
        {
            Trace.TraceInformation("Processing queue message {0}", msg);

            var workPackage = JsonConvert.DeserializeObject <PrimeNumberWorkPackage>(msg.AsString);

            if (workPackage.WorkType == WorkType.NewWork)
            {
                _numItems     = workPackage.NumEntries;
                _numProcessed = 0;
                this._primesQueue.DeleteMessage(msg);
                return;
            }

            var numberToTest = workPackage.Number;
            var isPrime      = await Task.Run(() => PrimeSolver.IsPrime(numberToTest));

            var primeNumberCandidate = new PrimeNumberCandidate(numberToTest)
            {
                IsPrime = isPrime
            };
            await _repository.Add(primeNumberCandidate);

            _numProcessed++;
            await CommunicateResult(primeNumberCandidate);

            //var percent = (int)Math.Round((double)(100 * _numProcessed) / _numItems);
            await CommunicateProgress();

            // Remove message from queue.
            this._primesQueue.DeleteMessage(msg);
        }
Пример #2
0
        /// <summary>Gets a list of all factors of specified numbers.</summary>
        /// <param name="inputNumber"></param>
        private IEnumerable <long> GetAllPrimeFactors(long inputNumber)
        {
            var  primeDivisors           = new List <long>(); //The return value
            var  currentlyAnalysedNumber = inputNumber;
            bool exitflag = true;

            while (exitflag)
            {
                var allFactors = primeSolver.GetDivisors(currentlyAnalysedNumber);

                var onlyPrimeFactors = from x in allFactors
                                       where primeSolver.IsPrime(x)
                                       select x;

                primeDivisors.AddRange(onlyPrimeFactors);

                var mul = primeDivisors.Aggregate((x, mult) => x * mult); //Multiply all found primes

                long temp = inputNumber / mul;
                if (temp > 1)
                {
                    currentlyAnalysedNumber = temp;
                }
                else //If remainder is 1 (or less - unlikely) break the loop;
                {
                    exitflag = false;
                }
                // || (mul == inputNumber)
            }
            return(primeDivisors);
        }
Пример #3
0
        protected override void Solve(out string answer)
        {
            #region Setting up initial variables
            long        testedNumber    = 600851475143;
            long        maxSolution     = testedNumber - 1;
            long        minSolution     = 2;
            List <long> listOfSolutions = new List <long>();
            var         upperBound      = (long)Math.Sqrt(testedNumber) + 1;
            #endregion

            var         currentSolution = minSolution;
            PrimeSolver primes          = new PrimeSolver();

            while (currentSolution < upperBound)
            {
                if (primes.IsDivisor(testedNumber, currentSolution))
                {
                    listOfSolutions.Add(currentSolution);
                }
                currentSolution++;
            }

            var allPrimes = listOfSolutions.Where(t => primes.IsPrime(t)).ToList();
            allPrimes.Sort();
            var largestFactor = allPrimes.Last();
            answer = string.Format("The largest prime factor of 600851475143 is: {0}", largestFactor);
        }
Пример #4
0
        protected override void Solve(out string answer)
        {
            PrimeSolver prime       = new PrimeSolver();
            long        border      = 2000000;
            long        sumOfPrimes = 0;

            Enumerable.Range(0, (int)border).AsParallel().ForAll(a => prime.IsPrime(a));
            sumOfPrimes = prime.KnownPrimes.Aggregate((a, b) => a + b);

            answer = string.Format("The sum of all primes below {0} is equal to {1}.", border, sumOfPrimes);
        }
Пример #5
0
        protected override void Solve(out string answer)
        {
            List <int> primes = new List <int>();

            Parallelization.GetParallelRanges(2, 999_998, 100).ForAll(sequence =>
            {
                foreach (int number in sequence)
                {
                    if (primesolver.IsPrime(number))
                    {
                        if (IsCircularPrime(number))
                        {
                            lock (this) primes.Add(number);
                        }
                    }
                }
            }
                                                                      );
            StringBuilder DEBUGString = new StringBuilder().AppendLine();

            primes.ForEach(prime => DEBUGString.AppendLine($"{prime}"));
            answer = $"Computing... Primes {primes.Count}. Primes: {DEBUGString}";
        }
Пример #6
0
        protected override void Solve(out string answer)
        {
            PrimeSolver primeSolver = new PrimeSolver();
            long        value       = 2;
            long        whichPrime  = 1; //2 is the 1st prime according to euler

            while (whichPrime < 10001)
            {
                while (!primeSolver.IsPrime(++value))
                {
                    ;
                }
                whichPrime++;
            }
            answer = string.Format("Checked {0}th answer. The answer is: {1}", whichPrime, value);
        }
Пример #7
0
        /// <summary>
        /// Finds primes that can be generated by this pair.
        /// </summary>
        internal void FindPrimes()
        {
            bool        stayInLoop  = true;
            long        n           = 0; //Starting value for n
            PrimeSolver primeSolver = new PrimeSolver();

            while (stayInLoop)
            {
                long quadratic = Quadratic(a: A, b: B, n: n);
                bool isPrime   = primeSolver.IsPrime(quadratic);
                if (isPrime)
                {
                    primes.Add(quadratic);
                    n++;
                    continue;
                }
                stayInLoop = false; // If current quadratic is not a prime, exit the loop.
            }
        }
Пример #8
0
 /// <summary>
 /// Checks for primeness of this number.
 /// </summary>
 internal bool IsPrime() => solver.IsPrime(Number);