mirror of
https://bitbucket.org/Mattrixwv/projecteulercs.git
synced 2026-02-03 19:52:36 -05:00
Added solution to problem 24
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
//ProjectEuler/ProjectEulerCS/src/ProblemSelection.cs
|
//ProjectEuler/ProjectEulerCS/src/ProblemSelection.cs
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 08-22-20
|
// 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
|
//This runs the benchmark functions for the Java version of the ProjectEuler project
|
||||||
/*
|
/*
|
||||||
Copyright (C) 2020 Matthew Ellison
|
Copyright (C) 2020 Matthew Ellison
|
||||||
@@ -28,7 +28,7 @@ namespace ProjectEulerCS{
|
|||||||
public class Benchmark{
|
public class Benchmark{
|
||||||
private enum BenchmarkOptions { RunSpecific = 1, RunAllShort, RunAll, Exit, Size };
|
private enum BenchmarkOptions { RunSpecific = 1, RunAllShort, RunAll, Exit, Size };
|
||||||
private static readonly List<int> tooLong = new List<int>()
|
private static readonly List<int> tooLong = new List<int>()
|
||||||
{5};
|
{5, 15, 24};
|
||||||
|
|
||||||
//The driver function for the benchmark selection
|
//The driver function for the benchmark selection
|
||||||
public static void BenchmarkMenu(){
|
public static void BenchmarkMenu(){
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ namespace ProjectEulerCS{
|
|||||||
private static readonly List<int> _PROBLEM_NUMBERS = new List<int>()
|
private static readonly List<int> _PROBLEM_NUMBERS = new List<int>()
|
||||||
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||||
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
|
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{
|
public static System.Collections.Generic.List<int> PROBLEM_NUMBERS{
|
||||||
get { return _PROBLEM_NUMBERS; }
|
get { return _PROBLEM_NUMBERS; }
|
||||||
}
|
}
|
||||||
@@ -64,6 +64,7 @@ namespace ProjectEulerCS{
|
|||||||
case 21: problem = new Problem21(); break;
|
case 21: problem = new Problem21(); break;
|
||||||
case 22: problem = new Problem22(); break;
|
case 22: problem = new Problem22(); break;
|
||||||
case 23: problem = new Problem23(); break;
|
case 23: problem = new Problem23(); break;
|
||||||
|
case 24: problem = new Problem24(); break;
|
||||||
case 67: problem = new Problem67(); break;
|
case 67: problem = new Problem67(); break;
|
||||||
}
|
}
|
||||||
return problem;
|
return problem;
|
||||||
|
|||||||
97
ProjectEulerCS/Problems/Problem24.cs
Normal file
97
ProjectEulerCS/Problems/Problem24.cs
Normal 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
|
||||||
|
*/
|
||||||
Reference in New Issue
Block a user