mirror of
https://bitbucket.org/Mattrixwv/projecteulerts.git
synced 2025-12-06 17:43:59 -05:00
Added solution to problem 38
This commit is contained in:
116
Problems/Problem38.ts
Normal file
116
Problems/Problem38.ts
Normal file
@@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
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
|
||||
*/
|
||||
Reference in New Issue
Block a user