diff --git a/Benchmark.ts b/Benchmark.ts index 00e021d..a70a342 100644 --- a/Benchmark.ts +++ b/Benchmark.ts @@ -32,7 +32,7 @@ enum BenchmarkOptions{runSpecific = 1, runAllShort, runAll, exit, size}; export class Benchmark{ - private static tooLong: number[] = [14, 15, 23]; + private static tooLong: number[] = [14, 15, 23, 24]; //The driver function for the benchmark selection public static benchmarkMenu(): void{ let selection: BenchmarkOptions = BenchmarkOptions.size; diff --git a/ProblemSelection.ts b/ProblemSelection.ts index 6b6e341..dcf90c5 100644 --- a/ProblemSelection.ts +++ b/ProblemSelection.ts @@ -47,12 +47,13 @@ import { Problem20 } from "./Problems/Problem20"; import { Problem21 } from "./Problems/Problem21"; import { Problem22 } from "./Problems/Problem22"; import { Problem23 } from "./Problems/Problem23"; +import { Problem24 } from "./Problems/Problem24"; export class ProblemSelection{ //Holds the valid problem numbers public static PROBLEM_NUMBERS: number[] = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, - 21, 22, 23]; + 21, 22, 23, 24]; //Returns the problem corresponding to the given problem number public static getProblem(problemNumber: number): Problem{ @@ -81,6 +82,7 @@ export class ProblemSelection{ case 21: problem = new Problem21(); break; case 22: problem = new Problem22(); break; case 23: problem = new Problem23(); break; + case 24: problem = new Problem24(); break; } return problem; } diff --git a/Problems/Problem24.ts b/Problems/Problem24.ts new file mode 100644 index 0000000..6d245c6 --- /dev/null +++ b/Problems/Problem24.ts @@ -0,0 +1,97 @@ +//ProjectEulerTS/Problems/Problem24.ts +//Matthew Ellison +// Created: 04-08-21 +//Modified: 04-08-21 +//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/typescriptClasses +/* + Copyright (C) 2021 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 . +*/ + + +import { getPermutations } from "../../../Typescript/typescriptClasses/Algorithms"; +import { Unsolved } from "../Unsolved"; +import { Problem } from "./Problem"; + + +export class Problem24 extends Problem{ + //Variables + //Static variables + private static NEEDED_PERM: number = 1000000; //The number of the permutation that you need + private static nums: string = "0123456789"; //All of the characters that we need to go the permutations of + //Instance variables + private permutations: string[]; //Holds all of the permutations of the string nums + + //Functions + //Constructor + public constructor(){ + super(`What is the millionth lexicographic permutation of the digits ${Problem24.nums}?`); + this.permutations = []; + } + //Operational functions + //Solve the problems + public solve(): void{ + //If the problem has already been solved do nothing and end the function + if(this.solved){ + return; + } + + //Start the timer + this.timer.start(); + + //Get all the permutations of the string + this.permutations = getPermutations(Problem24.nums); + + //Stop the timer + this.timer.stop(); + + //Throw a flag to show the problem is solved + this.solved = true; + } + //Reset the problem so it can be run again + public reset(): void{ + super.reset(); + this.permutations = []; + } + //Gets + //Returns the result of solving the problem + public getResult(): string{ + if(!this.solved){ + throw new Unsolved(); + } + return `The 1 millionth permutation is ${this.permutations[Problem24.NEEDED_PERM - 1]}`; + } + //Returns an array with all of the permutations + public getPermutationsList(): string[]{ + if(!this.solved){ + throw new Unsolved(); + } + return this.permutations; + } + //Returns the requested permutation + public getPermutation(){ + //If the problem hasn't been solved throw an exception + if(!this.solved){ + throw new Unsolved(); + } + return this.permutations[Problem24.NEEDED_PERM - 1]; + } +} + +/* Results: +The 1 millionth permutation is 2783915460 +It took an average of 1.946 seconds to run this problem through 100 iterations +*/