mirror of
https://bitbucket.org/Mattrixwv/projecteulerts.git
synced 2025-12-06 17:43:59 -05:00
Added solution to problem 36
This commit is contained in:
113
Problems/Problem36.ts
Normal file
113
Problems/Problem36.ts
Normal file
@@ -0,0 +1,113 @@
|
||||
//ProjectEulerTs/Problems/Problem36.ts
|
||||
//Matthew Ellison
|
||||
// Created: 06-29-21
|
||||
//Modified: 06-29-21
|
||||
//Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.
|
||||
//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 { getSum, isPalindrome, toBin } from "../../../Typescript/typescriptClasses/Algorithms";
|
||||
import { Unsolved } from "../Unsolved";
|
||||
import { Problem } from "./Problem";
|
||||
|
||||
|
||||
export class Problem36 extends Problem{
|
||||
//Variables
|
||||
//Static variables
|
||||
private static MAX_NUM: number = 999999; //The largest number that will be checked
|
||||
//Instance variables
|
||||
private palindromes: number[]; //All numbers that are aplindromes in base 10 and 2
|
||||
private sum: number; //The sum of all elements in the list of palindromes
|
||||
|
||||
//Functions
|
||||
//Constructor
|
||||
public constructor(){
|
||||
super("Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.");
|
||||
this.palindromes = [];
|
||||
this.sum = 0;
|
||||
}
|
||||
//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();
|
||||
|
||||
//Start with 1, check if it is a palindrome in base 10 and 2, and continue to MAX_NUM
|
||||
for(let num = 1;num < Problem36.MAX_NUM;++num){
|
||||
//Check if num is a palindrome
|
||||
if(isPalindrome(num.toString())){
|
||||
//Convert num to base 2 and see if that is a palindrome
|
||||
let binNum: string = toBin(num);
|
||||
if(isPalindrome(binNum)){
|
||||
//Add num to the list of palindromes
|
||||
this.palindromes.push(num);
|
||||
}
|
||||
}
|
||||
}
|
||||
//Get the sum of all palindromes in the list
|
||||
this.sum = getSum(this.palindromes);
|
||||
|
||||
//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 = [];
|
||||
this.sum = 0;
|
||||
}
|
||||
//Gets
|
||||
//Returns a string with the solution to the problem
|
||||
public getResult(): string{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!this.solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
return `The sum of all base 10 and base 2 palindromic numbers < ${Problem36.MAX_NUM} is ${this.sum}`;
|
||||
}
|
||||
//Return the array of palindromes < MAX_NUM
|
||||
public getPalindromes(){
|
||||
//If the porblem hasn't been solved throw an exception
|
||||
if(!this.solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
return this.palindromes;
|
||||
}
|
||||
//Returns the sum of all elements in the array of palindromes
|
||||
public getSumOfPalindromes(){
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!this.solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
return this.sum;
|
||||
}
|
||||
}
|
||||
|
||||
/* Results:
|
||||
The sum of all base 10 and base 2 palindromic numbers < 999999 is 872187
|
||||
It took an average of 297.957 milliseconds to run this problem through 100 iterations
|
||||
*/
|
||||
Reference in New Issue
Block a user