Added solution to problem 24

This commit is contained in:
2020-09-04 17:34:51 -04:00
parent 7d5c0ce273
commit cce17c8223
3 changed files with 101 additions and 3 deletions

View File

@@ -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<int> tooLong = new List<int>()
{5};
{5, 15, 24};
//The driver function for the benchmark selection
public static void BenchmarkMenu(){

View File

@@ -32,7 +32,7 @@ namespace ProjectEulerCS{
private static readonly List<int> _PROBLEM_NUMBERS = new List<int>()
{ 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<int> 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;

View File

@@ -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 <https://www.gnu.org/licenses/>.
*/
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<string> permutations; //Holds all of the permutations of the string nums
public List<string> 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<string>();
}
//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
*/