Exemple #1
0
        protected override void Solve(out string answer)
        {
            int[] numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            Permutations <int> permutations          = new Permutations <int>();
            var  allSequencesToCheck                 = permutations.GeneratePermutations(new List <int>(numbers));
            long iterationCounter                    = 0;
            List <PandigitCandidate> allCombinations = new List <PandigitCandidate>();

            //All sequences of length 9 are ready.
            //foreach (var sequence in allSequencesToCheck)
            allSequencesToCheck.AsParallel().ForAll(sequence =>
            {
                // Now break each sequence into lengths of 2 through 9
                foreach (int i in Enumerable.Range(1, 9))
                {
                    foreach (int j in Enumerable.Range(1, 9))
                    {
                        int totalLength = i + j;
                        if (totalLength > 8)
                        {
                            continue;                  //If 9 or longer, there is no space for product digits, so no point in continuing. This is definitely not a candidate.
                        }
                        lock (this) iterationCounter++;
                        var fistSequence   = sequence.Take(i);
                        var secondSequence = sequence.Skip(i).Take(j);
                        var tester         = new PandigitCandidate(fistSequence, secondSequence);
                        if (tester.IsATruePandigit()) //Test early in parallel, to maximize effort spent in parallel
                        {
                            lock (allCombinations) allCombinations.Add(tester);
                        }
                    }
                }
            }
                                                    );

            //All combinations to check for pandigitability are in allCombinations collection.
            //Select only those that are pandigital.
            var newCollection = from candidate in allCombinations
                                where candidate.IsATruePandigit()
                                select candidate;
            var finalList = newCollection.ToList();
            //Since there are multiple results in the list, group them by product to make sure we include only one distinct product.
            var           uniqueList  = finalList.GroupBy(candidate => candidate.Product);
            StringBuilder answerPart  = new StringBuilder().AppendLine();
            long          sumProducts = 0;

            foreach (var group in uniqueList)
            {
                answerPart.AppendLine(group.First().ToString());
                sumProducts += group.Key;
            }

            answer = string.Format("All sequences = {0}. Loop iterations: {1}. Results: {2}. Sum of unique products: {3}. List of results: {4}", allSequencesToCheck.Count, iterationCounter, finalList.Count, sumProducts, answerPart);
        }
        public static void Main(string[] args)
        {
            bool isFirstRun = true;
            while (true)
            {
                if (isFirstRun)
                {
                    System.Console.WriteLine("UWW CS215 Fano Plane Automorphism Finder" + Environment.NewLine + "Authored by Connor Grady, Grant Jones, and Collin Stolpa");
                }
                else
                {
                    System.Console.WriteLine();
                }
                string input = CollectInput();

                string[] items = new string[7] { "1", "2", "3", "4", "5", "6", "7" };

                Permutations permutations = new Permutations();
                List<List<string>> results = permutations.GeneratePermutations(items.ToList());
                //WritePermutations(results);

                switch (input)
                {
                    case "OutputPerms":
                        WritePermutations(results);
                        break;

                    case "OutputAutos":
                        // Now that we have the Permutations, let's check for the automorphisms
                        List<List<string>> automorphisms = results.Where(IsAutomorphism).ToList();

                        // Convert to Cycle Notation
                        List<string> automorphicCycles = automorphisms.Select(perm => permutations.ToCycleNotation(results.FirstOrDefault(), perm)).ToList();

                        // Write cycles to the Console
                        WriteCycles(automorphicCycles, automorphisms.Select(auto => string.Join("", auto)).ToList());
                        break;

                    default:
                        System.Console.WriteLine("ERROR: No valid selection detected. Please try again...");
                        break;
                }

                isFirstRun = false;
            }
        }
        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}";
        }