Added solution to problem 4

This commit is contained in:
2020-10-24 13:24:23 -04:00
parent 180946eff3
commit e08a714687
2 changed files with 116 additions and 1 deletions

View File

@@ -27,11 +27,12 @@ import { Problem } from "./Problems/Problem";
import { Problem1 } from "./Problems/Problem1";
import { Problem2 } from "./Problems/Problem2";
import { Problem3 } from "./Problems/Problem3";
import { Problem4 } from "./Problems/Problem4";
export class ProblemSelection{
//Holds the valid problem numbers
public static PROBLEM_NUMBERS: number[] = [1, 2, 3];
public static PROBLEM_NUMBERS: number[] = [1, 2, 3, 4];
//Returns the problem corresponding to the given problem number
public static getProblem(problemNumber: number): Problem{
@@ -40,6 +41,7 @@ export class ProblemSelection{
case 1 : problem = new Problem1(); break;
case 2 : problem = new Problem2(); break;
case 3 : problem = new Problem3(); break;
case 4 : problem = new Problem4(); break;
}
return problem;
}

113
Problems/Problem4.ts Normal file
View File

@@ -0,0 +1,113 @@
//ProjectEulerTS/Problems/Problem4.ts
//Matthew Ellison
// Created: 10-24-20
//Modified: 10-24-20
//Find the largest palindrome made from the product of two 3-digit numbers
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/typescriptClasses
/*
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/>.
*/
import { Problem } from "./Problem";
import { Unsolved } from "../Unsolved";
export class Problem4 extends Problem{
//Variables
//Static variables
private static START_NUM: number = 100; //The first number to be multiplied
private static END_NUM: number = 999; //The last number to be multiplied
//Instance variables
private palindromes: number[]; //Holds all number that turn out to be palindromes
//Constructor
public constructor(){
super("Find the largest palindrome made from the product of two 3-digit numbers");
}
//Operational functions
//Solve the problem
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();
//Start at the first 3-digit number and check every one up to the last 3-digit number
for(let firstNum: number = Problem4.START_NUM;firstNum <= Problem4.END_NUM;++firstNum){
//You can start at the location of the first number because everything before that has already been tested. (100 * 101 == 101 * 100)
for(let secondNum = firstNum;secondNum < Problem4.END_NUM;++secondNum){
//Get the product
let product: number = firstNum * secondNum;
//Change the number into a string
let productString: string = product.toString();
//Reverse the string
let reverseString: string = productString.split("").reverse().join("");
//If the number and it's reverse are the same it is a palindrome so add it to the list
if(productString == reverseString){
this.palindromes.push(product);
}
//If it's not a palindrome ignore it and move to the next number
}
}
//Sort the palindromes so that the last one is the largest
this.palindromes = this.palindromes.sort((n1, n2) => n1 - n2);
//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.palindromes = [];
}
//Gets
//Returns the result of solving the problem
public getResult(): string{
if(!this.solved){
throw new Unsolved();
}
return `The largest palindrome is ${this.palindromes[this.palindromes.length - 1]}`;
}
//Returns the list of all palindromes
public getPalindromes(): number[]{
if(!this.solved){
throw new Unsolved();
}
return this.palindromes;
}
//Returns the largest palindrome
public getLargestPalindrome(): number{
if(!this.solved){
throw new Unsolved();
}
return this.palindromes[this.palindromes.length - 1];
}
}
/* Results:
The largest palindrome is 906609
It took an average of 121.130 milliseconds to run this problem through 100 iterations
*/