From ffae2713a1406589595f787bd7d4a7a6e96fe46d Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Thu, 11 Nov 2021 21:54:02 -0500 Subject: [PATCH] Added solution to problem 38 --- ProblemSelection.ts | 4 +- Problems/Problem38.ts | 116 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 Problems/Problem38.ts diff --git a/ProblemSelection.ts b/ProblemSelection.ts index dd589b3..b0a96fe 100644 --- a/ProblemSelection.ts +++ b/ProblemSelection.ts @@ -61,13 +61,14 @@ import { Problem34 } from "./Problems/Problem34"; import { Problem35 } from "./Problems/Problem35"; import { Problem36 } from "./Problems/Problem36"; import { Problem37 } from "./Problems/Problem37"; +import { Problem38 } from "./Problems/Problem38"; import { Problem67 } from "./Problems/Problem67"; 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, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 67]; + 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 67]; //Returns the problem corresponding to the given problem number public static getProblem(problemNumber: number): Problem{ @@ -110,6 +111,7 @@ export class ProblemSelection{ case 35: problem = new Problem35(); break; case 36: problem = new Problem36(); break; case 37: problem = new Problem37(); break; + case 38: problem = new Problem38(); break; case 67: problem = new Problem67(); break; } return problem; diff --git a/Problems/Problem38.ts b/Problems/Problem38.ts new file mode 100644 index 0000000..a3182aa --- /dev/null +++ b/Problems/Problem38.ts @@ -0,0 +1,116 @@ +//ProjectEulerTS/Problems/Problem38.ts +//Matthew Ellison +// Created: 11-11-21 +//Modified: 11-11-21 +//What is the largest 1-9 pandigital number that can be formed as the concatenated product of an integer with 1, 2, ... n where n > 1 +//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 { isPandigital } from "../../../Typescript/typescriptClasses/StringAlgorithms"; +import { Problem } from "./Problem"; + + +export class Problem38 extends Problem{ + //Variables + //Static variables + private static HIGHEST_POSSIBLE_NUMBER: number = 9999; //The highest number that needs to be checked for a 1-9 pandigital + //Instance variables + private largestNum: number; //The number passed to the executeFormula function that returns the largest pandigital + private pandigital: number; //The largest pandigital number found + + //Functions + //Constructor + public constructor(){ + super("What is the largest 1-9 pandigital number that can be formed as the concatenated product of an integer with 1, 2, ... n where n > 1"); + this.largestNum = 0; + this.pandigital = 0; + } + //Operational functions + //Take the number and add its multiples to a string to return + private executeFormula(num: number): string{ + //Turn the current number into a string + let numStr = num.toString(); + let cnt = 2; + //Multiply the number and append the product to the string until you have one long enough + do{ + numStr += (num * cnt).toString(); + ++cnt; + }while(numStr.length < 9); + + return numStr; + } + //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(); + + + //Loop from 1 -> HIGHEST_POSSIBLE_NUM checking for pandigitals + for(let cnt = 1;cnt <= Problem38.HIGHEST_POSSIBLE_NUMBER;++cnt){ + //Get the string from the formula + let numStr = this.executeFormula(cnt); + let panNum = parseInt(numStr); + //If the number is pandigital save it as the highest number + if(isPandigital(numStr) && (panNum > this.pandigital)){ + this.largestNum = cnt; + this.pandigital = panNum; + } + } + + + //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.largestNum = 0; + this.pandigital = 0; + } + + //Gets + //Returns a string with the solution tot he problem + public getResult(): string{ + this.solvedCheck("results"); + return `The largest appended product pandigital is ${this.pandigital}`; + } + //Returns the largest number + public getLargestNum(): number{ + this.solvedCheck("largest number"); + return this.largestNum; + } + //Returns the pandigital of the number + public getPandigital(): number{ + this.solvedCheck("pandigital"); + return this.pandigital; + } +} + +/* Results: +The largest appended product pandigital is 932718654 +It took an average of 3.051 milliseconds to run this problem through 100 iterations +*/