Exemplo n.º 1
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}";
        }
Exemplo n.º 2
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="pGrid">
        /// Parent grid.
        /// </param>
        /// <param name="AggregationCells">
        /// Coarse cells which build up the fine cells.
        /// - 1st index: coarse (i.e. this) grid cell index
        /// - 2nd index: enumeration
        /// - content: local cell index into the parent grid <paramref name="pGrid"/>.
        /// </param>
        public AggregationGrid(IGridData pGrid, int[][] AggregationCells)
        {
            ParentGrid = pGrid;

            int JlocFine  = pGrid.iLogicalCells.NoOfLocalUpdatedCells;
            int JElocFine = pGrid.iLogicalCells.NoOfCells;

            m_GeomCellData = new GeomCellData()
            {
                m_Owner = this
            };
            m_LogicalCellData = new LogicalCellData()
            {
                m_Owner = this
            };
            m_GeomEdgeData = new GeomEdgeData()
            {
                m_Owner = this
            };
            m_LogEdgeData = new LogEdgeData();
            m_VertexData  = new VertexData();
            m_Parallel    = new Parallelization()
            {
                m_owner = this
            };

            CellPartitioning = new Partitioning(AggregationCells.Length, pGrid.CellPartitioning.MPI_Comm);

            int j0Coarse = CellPartitioning.i0;

            BuildNeighborship(AggregationCells);
            DefineCellParts();
            CollectEdges();
        }
Exemplo n.º 3
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}";
        }
Exemplo n.º 4
0
        protected override void Solve(out string answer)
        {
            PentagonManager pentagonManager = new PentagonManager();

            //Generate numbers up to 5000
            long maxCount = 5000;

            foreach (long j in Enumerable64.Range(1, maxCount))
            {
                PentagonNumber pj = new PentagonNumber(j);
                pentagonManager.Add(pj);
            }
            long count        = maxCount * maxCount;
            long progressDone = 0;

            //Create all pairs 5000x5000
            Parallelization.GetParallelRanges(1, maxCount, 50).ForAll(sequence =>
            {
                foreach (long j in sequence)
                {
                    foreach (long k in Enumerable64.Range(1, maxCount))
                    {
                        PentagonPair pjk = pentagonManager.CreatePair(j, k);
                        if (pentagonManager.IsSumPentagonal(pjk))
                        {
                            if (pentagonManager.IsDifPentagonal(pjk))
                            {
                                lock (pentagonManager)
                                {
                                    pentagonManager.StorePair(pjk);
                                }
                            }
                        }
                        lock (this)
                        {
                            progressDone++;
                            if (progressDone % 100_000 == 0)
                            {
                                int percent = (int)(progressDone * 100.0 / count);
                                UpdateProgress($"Pairs checked out of {count}: Done {percent}%. Hits: {pentagonManager.storedPairs.Count}...");
                            }
                        }
                    }
                }
            });
            var solution = pentagonManager.storedPairs.OrderBy(pair => pair.AbsDifValue).First();

            answer = $"Candiates = {pentagonManager.storedPairs.Count}, D = {solution.AbsDifValue}.";
        }
Exemplo n.º 5
0
        private static void Main()
        {
            const int count = 1000000; //1 million

            var start           = DateTime.Now;
            var listOfUniverses =
                Parallelization.DistributeParallel(chunkCount => TestDataGeneratorFactory.Create().CreateMany <Universe> (chunkCount), count).ToList();

            Console.WriteLine(
                "Took {0}s to generate {1} universes",
                (DateTime.Now - start).TotalSeconds,
                listOfUniverses.Count);

            Console.Read();
        }
Exemplo n.º 6
0
        protected override void Solve(out string answer)
        {
            var triangles = new List <RightTriangle>();

            var test      = new RightTriangle(3, 4);
            var isInteger = test.IsInteger;

            test      = new RightTriangle(4, 7);
            isInteger = test.IsInteger;

            //Check all triangles with a ≤ 1000
            Parallelization.GetParallelRanges(1, 1_000, 4).ForAll(sequence =>
            {
                foreach (int a in sequence)
                {
                    //Check all triangles with b ≤ 1000
                    foreach (int b in Enumerable.Range(1, 1_000))
                    {
                        var triangle = new RightTriangle(a, b);
                        if (triangle.IsInteger)
                        {
                            if (triangle.Perimeter <= 1000)
                            {
                                lock (this) triangles.Add(triangle);
                            }
                        }
                    }
                }
            });
            //All right triangles are in the triangles list.
            //Group them by perimeter
            var groups = triangles.GroupBy(triangle => triangle.Perimeter);
            //Sort to find the highest count of solutions.
            var orderedGroups = groups.OrderByDescending(group => group.Count());
            var solution      = orderedGroups.First();

            answer = $"Computing... Count={triangles.Count}. Solution = P={solution.Key} (possible solutions: {solution.Count()}).";
        }
Exemplo n.º 7
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}";
        }
Exemplo n.º 8
0
        protected override void Solve(out string answer)
        {
            var list = new List <Representation>();

            Parallelization.GetParallelRanges(1, 10_000, 4).ForAll(sequence =>
            {
                foreach (int number in sequence)
                {
                    foreach (int maxMultiplier in Enumerable.Range(2, 99))
                    {
                        var candidate = new Representation(number, maxMultiplier);
                        if (candidate.IsPandigital())
                        {
                            lock (this) list.Add(candidate);
                            break;  //If pandigit is found, multiplying by higher multipliers definitely will fail.
                        }
                    }
                }
            });
            var result = list.OrderByDescending(item => item.Pandigits);
            var max    = result.First();

            answer = $"Count = {list.Count}, Largest pandigital: {max}";
        }
Exemplo n.º 9
0
        protected override void Solve(out string answer)
        {
            PalindromeManipulator       manipulator = new PalindromeManipulator();
            List <NumberRepresentation> list        = new List <NumberRepresentation>();
            int counter = 0;
            int sum     = 0;

            //Create Parallel stuff
            var parallels = Parallelization.GetParallelRanges(1, 999_999, 32);

            //var ranges = Enumerable.Range(1, 999_999).AsParallel();
            //foreach (var i in range)
            //ranges.ForAll(i =>
            parallels.ForAll(intrange =>
            {
                foreach (int i in intrange)
                {
                    if (i < 1_000_000)
                    {
                        NumberRepresentation number = new NumberRepresentation(i);
                        if (number.IsPalindrome)
                        {
                            lock (this)
                            {
                                list.Add(number);
                                counter++;
                                sum += number.Value;
                            }
                        }
                    }
                }
            }
                             );

            answer = $"Computing... Sum = {sum}. Count = {counter}.";
        }