예제 #1
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}";
        }
예제 #2
0
        protected override void Solve(out string answer)
        {
            Pandigits pandigitizer = new Pandigits('0', 10);
            List <NumberRepresentation> pandigitNumbersWithProperty = new List <NumberRepresentation>();

            #region Fast - permutation based approach
            Permutations <char> permutations = new Permutations <char>();
            var list = permutations.GeneratePermutations(new List <char> {
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
            });
            list.AsParallel().ForAll(item =>
            {
                var numRep = new NumberRepresentation(MoreMath.IntFromDigits(item));
                if (pandigitizer.IsPandigital(numRep))
                {
                    if (numRep.IsSubstringDivisible())
                    {
                        lock (pandigitNumbersWithProperty)
                        {
                            pandigitNumbersWithProperty.Add(numRep);
                        }
                    }
                }
            }
                                     );
            #endregion

            #region Slow - brute force approach
            //long start = 0_000_000_000;
            //long count = 9_999_999_999;
            //long progressDone = 0;
            //Parallelization.GetParallelRanges(start, count, 200).ForAll(sequence =>
            //{
            //    var repSequence = sequence.Select(v => new NumberRepresentation(v));
            //    foreach (var rep in repSequence)
            //    {
            //        if (pandigitizer.IsPandigital(rep))
            //        {
            //            if (rep.IsSubstringDivisible())
            //            {
            //                lock (pandigitNumbersWithProperty)
            //                {
            //                    pandigitNumbersWithProperty.Add(rep);
            //                }
            //            }
            //        }
            //        #region Update progress
            //        lock (this)
            //        {
            //            progressDone++;
            //            if (progressDone % 1_000_000 == 0)
            //            {
            //                var percent = progressDone * 100.0 / count;
            //                UpdateProgress($"Range {start}-{start + count}: Done {percent}%. Hits: {pandigitNumbersWithProperty.Count}...");
            //            }
            //        }
            //        #endregion
            //    }
            //});
            #endregion
            var sum = pandigitNumbersWithProperty.Select(rep => rep.Value).Aggregate((total, num) => total + num);
            answer = $"Count = {pandigitNumbersWithProperty.Count}, Sum = {sum}";
        }