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);
        }
Beispiel #2
0
        protected override void Solve(out string answer)
        {
            int                   sum    = 0;
            PrimeSolver           solver = new PrimeSolver();
            List <Representation> list   = new List <Representation>();

            Parallelization.GetParallelRanges(1, 10_000_000, 100).ForAll(sequence =>
            {
                foreach (int number in sequence)
                {
                    var rep = new Representation(solver, number);
                    if (rep.IsPrime())
                    {
                        if (rep.IsTruncatable())
                        {
                            lock (this) list.Add(rep);
                        }
                    }
                }
            }
                                                                         );
            sum = list.Sum(el => el.Number);
            StringBuilder DEBUGString = new StringBuilder().AppendLine();

            list.ForEach(item => DEBUGString.Append($"{item}, "));
            answer = $"Computing... Sum = {sum}. List size = {list.Count}. {DEBUGString}";
        }
Beispiel #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);
        }
Beispiel #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);
        }
Beispiel #5
0
        protected override void Solve(out string answer)
        {
            //Of course we will assume that this number will start with 9
            var  list         = new List <PrimeRep>();
            var  primeSolver  = new PrimeSolver();
            var  pandigitizer = new Pandigits('1', 7);
            long start        = 0_000_000;
            long count        = 7_654_321;
            long progressDone = 0;

            Parallelization.GetParallelRanges(start, count, 200).ForAll(sequence =>
            {
                foreach (var num in sequence.Reverse()) // Go from largest to smallest
                {
                    if (pandigitizer.IsPandigital(num))
                    {
                        var rep = new PrimeRep(primeSolver, num);
                        if (rep.IsPrime)
                        {
                            if (pandigitizer.IsPandigital(rep.Value))
                            {
                                lock (list) list.Add(rep);
                                //break; //Because this is the largest by definition, no need to search other values.
                            }
                        }
                    }
                    #region Update progress
                    lock (this)
                    {
                        progressDone++;
                        if (progressDone % 100_000 == 0)
                        {
                            var percent = progressDone * 100.0 / count;
                            UpdateProgress($"Range {start}-{start + count}: Done {percent}%. Hits: {list.Count}...");
                        }
                    }
                    #endregion
                }
            });
            var maxItem  = list.OrderByDescending(x => x.Value);
            var finalRep = maxItem.FirstOrDefault(); //If no prime found, this will be 'null'

            long value = 0;                          //Default
            if (finalRep != null)
            {
                value = finalRep.Value;                   //Override if exists
            }
            answer = $"Largest n-digit pandigital prime in range {start} - {start+count}: {value}";
        }
Beispiel #6
0
        protected override void Solve(out string answer)
        {
            long        currentTriangle      = 0;
            long        currentNaturalNumber = 0;
            int         divisors             = 0;
            PrimeSolver primeSolver          = new PrimeSolver();

            while (divisors < 501)
            {
                currentNaturalNumber++;
                currentTriangle += currentNaturalNumber;
                divisors         = primeSolver.CalculateDivisors(currentTriangle);
            }
            answer = string.Format("Sequence: {0}, Triangle number: {1}, Divisors: {2}", currentNaturalNumber, currentTriangle, divisors);
        }
Beispiel #7
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);
        }
Beispiel #8
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.
            }
        }
Beispiel #9
0
 public PrimeRep(PrimeSolver primeSolver, long num)
 {
     this.primeSolver = primeSolver;
     Number           = num;
 }
Beispiel #10
0
 public EulerProblem35()
 {
     primesolver = new PrimeSolver();
 }
Beispiel #11
0
 public PrimeSolverToPrimeDirectiveAdapter()
 {
     _primeSolver = new PrimeSolver();
 }
Beispiel #12
0
 public Representation(PrimeSolver solverInstance, int number)
 {
     solver = solverInstance;
     Number = number;
 }
Beispiel #13
0
 public PrimeHub() : this(PrimeSolver.GetPrimeSolver())
 {
 }
Beispiel #14
0
 public PrimeHub(PrimeSolver solver)
 {
     _solver = solver;
 }