From cce17c8223460ae0622321dc385ecbc5ba7cfc47 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Fri, 4 Sep 2020 17:34:51 -0400 Subject: [PATCH] Added solution to problem 24 --- ProjectEulerCS/Benchmark.cs | 4 +- ProjectEulerCS/ProblemSelection.cs | 3 +- ProjectEulerCS/Problems/Problem24.cs | 97 ++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 3 deletions(-) create mode 100644 ProjectEulerCS/Problems/Problem24.cs diff --git a/ProjectEulerCS/Benchmark.cs b/ProjectEulerCS/Benchmark.cs index a35b5f8..155e5dc 100644 --- a/ProjectEulerCS/Benchmark.cs +++ b/ProjectEulerCS/Benchmark.cs @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCS/src/ProblemSelection.cs //Matthew Ellison // Created: 08-22-20 -//Modified: 08-22-20 +//Modified: 09-04-20 //This runs the benchmark functions for the Java version of the ProjectEuler project /* Copyright (C) 2020 Matthew Ellison @@ -28,7 +28,7 @@ namespace ProjectEulerCS{ public class Benchmark{ private enum BenchmarkOptions { RunSpecific = 1, RunAllShort, RunAll, Exit, Size }; private static readonly List tooLong = new List() - {5}; + {5, 15, 24}; //The driver function for the benchmark selection public static void BenchmarkMenu(){ diff --git a/ProjectEulerCS/ProblemSelection.cs b/ProjectEulerCS/ProblemSelection.cs index e3d666d..93aa04f 100644 --- a/ProjectEulerCS/ProblemSelection.cs +++ b/ProjectEulerCS/ProblemSelection.cs @@ -32,7 +32,7 @@ namespace ProjectEulerCS{ private static readonly List _PROBLEM_NUMBERS = new List() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, - 20, 21, 22, 23, 67}; + 20, 21, 22, 23, 24, 67}; public static System.Collections.Generic.List PROBLEM_NUMBERS{ get { return _PROBLEM_NUMBERS; } } @@ -64,6 +64,7 @@ namespace ProjectEulerCS{ case 21: problem = new Problem21(); break; case 22: problem = new Problem22(); break; case 23: problem = new Problem23(); break; + case 24: problem = new Problem24(); break; case 67: problem = new Problem67(); break; } return problem; diff --git a/ProjectEulerCS/Problems/Problem24.cs b/ProjectEulerCS/Problems/Problem24.cs new file mode 100644 index 0000000..1106175 --- /dev/null +++ b/ProjectEulerCS/Problems/Problem24.cs @@ -0,0 +1,97 @@ +//ProjectEuler/ProjectEulerCS/src/Problems/Problem24.cs +//Matthew Ellison +// Created: 09-03-20 +//Modified: 09-04-20 +//What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9? +//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses +/* + Copyright (C) 2020 Matthew Ellison + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . +*/ + + +using System.Collections.Generic; + + +namespace ProjectEulerCS.Problems{ + public class Problem24 : Problem{ + //Variables + //Static variables + private const int NEEDED_PERM = 1000000; //The number of the permutation that you need + private static string nums = "0123456789"; //All of the characters that we need to get the permutations of + //Instance variables + private List permutations; //Holds all of the permutations of the string nums + public List PermutationsList{ + get{ + if(!solved){ + throw new Unsolved(); + } + return permutations; + } + } + public string Permutation{ + get{ + if(!solved){ + throw new Unsolved(); + } + return permutations[NEEDED_PERM - 1]; + } + } + public override string Result{ + get{ + if(!solved){ + throw new Unsolved(); + } + return $"The 1 millionth permutation is {Permutation}"; + } + } + + //Functions + //Constructor + public Problem24() : base($"What is the millionth lexicographic permutation of the digits {nums}?"){ + permutations = new List(); + } + //Operational functions + //Solved the problem + public override void Solve(){ + //If the problem has already been solved do nothing and end the function + if(solved){ + return; + } + + //Start the timer + timer.Start(); + + //Get all the permutations of the string + permutations = mee.Algorithms.GetPermutations(nums); + + //Stop the timer + timer.Stop(); + + //Throw a flag to show the problem is solved + solved = true; + } + //Reset the problem so it can be run again + public override void Reset(){ + base.Reset(); + permutations.Clear(); + } + } +} + +/* Results: +The 1 millionth permutation is 2783915460 +It took an average of 5.865 seconds to run this problem through 100 iterations +*/