mirror of
https://bitbucket.org/Mattrixwv/projecteulerts.git
synced 2025-12-06 17:43:59 -05:00
Added solution to problem 34
This commit is contained in:
@@ -58,12 +58,13 @@ import { Problem31 } from "./Problems/Problem31";
|
||||
import { Problem32 } from "./Problems/Problem32";
|
||||
import { Problem33 } from "./Problems/Problem33";
|
||||
import { Problem67 } from "./Problems/Problem67";
|
||||
import { Problem34 } from "./Problems/Problem34";
|
||||
|
||||
|
||||
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, 67];
|
||||
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 67];
|
||||
|
||||
//Returns the problem corresponding to the given problem number
|
||||
public static getProblem(problemNumber: number): Problem{
|
||||
@@ -102,6 +103,7 @@ export class ProblemSelection{
|
||||
case 31: problem = new Problem31(); break;
|
||||
case 32: problem = new Problem32(); break;
|
||||
case 33: problem = new Problem33(); break;
|
||||
case 34: problem = new Problem34(); break;
|
||||
case 67: problem = new Problem67(); break;
|
||||
}
|
||||
return problem;
|
||||
|
||||
123
Problems/Problem34.ts
Normal file
123
Problems/Problem34.ts
Normal file
@@ -0,0 +1,123 @@
|
||||
//ProjectEulerTS/Problems/Problem34.ts
|
||||
//Matthew Ellison
|
||||
// Created: 06-01-21
|
||||
//Modified: 06-01-21
|
||||
//Find the sum of all numbers which are equal to the sum of the factorial of their digits
|
||||
//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 { factorial } from "../../../Typescript/typescriptClasses/Algorithms";
|
||||
import { Unsolved } from "../Unsolved";
|
||||
import { Problem } from "./Problem";
|
||||
|
||||
|
||||
export class Problem34 extends Problem{
|
||||
//Variables
|
||||
//Static variables
|
||||
private static MAX_NUM: number = 1499999; //The largest num that can be the sum of its own digits
|
||||
//Instance variables
|
||||
private factorials: number[]; //Holds the pre-computed factorials of the numbers 0-9
|
||||
private sum: number; //Holds the sum of all numbers equal to the sum of their digit's factorials
|
||||
|
||||
//Functions
|
||||
//Constructor
|
||||
public constructor(){
|
||||
super("Find the sum of all numbers which are equal to the sum of the factorial of their digits");
|
||||
this.sum = 0;
|
||||
this.factorials = [];
|
||||
for(let cnt = 0;cnt <= 9;++cnt){
|
||||
this.factorials.push(0);
|
||||
}
|
||||
}
|
||||
//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();
|
||||
|
||||
//Pre-compute the possible factorials form 0! to 9!
|
||||
for(let cnt = 0;cnt <= 9;++cnt){
|
||||
this.factorials[cnt] = factorial(cnt);
|
||||
}
|
||||
//Run through all possible numbers from 3-MAX_NUM and see if they equal the sum of their digit's factorials
|
||||
for(let cnt = 3;cnt < Problem34.MAX_NUM;++cnt){
|
||||
//Split the number into tis digits and add each one to the sum
|
||||
let numString: string = cnt.toString();
|
||||
let currentSum: number = 0;
|
||||
for(let numCnt = 0;numCnt < numString.length;++numCnt){
|
||||
let digit = numString.charAt(numCnt);
|
||||
currentSum += this.factorials[parseInt(digit)];
|
||||
}
|
||||
//If the number is equal to the sum add the sum to the running sum
|
||||
if(currentSum == cnt){
|
||||
this.sum += currentSum;
|
||||
}
|
||||
}
|
||||
|
||||
//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.sum = 0;
|
||||
this.factorials = [];
|
||||
for(let cnt = 0;cnt <= 9;++cnt){
|
||||
this.factorials.push(0);
|
||||
}
|
||||
}
|
||||
//Gets
|
||||
//Returns the result of solving 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 numbers that are the sum of their digit's factorials is ${this.sum}`;
|
||||
}
|
||||
//Returns the list of factorials from 0-9
|
||||
public getFactorials(): number[]{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!this.solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
return this.factorials;
|
||||
}
|
||||
//Returns the sum of all numbers equal to the sum of their digit's factorials
|
||||
public getSum(): number{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!this.solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
return this.sum;
|
||||
}
|
||||
}
|
||||
|
||||
/* Results:
|
||||
The sum of all numbers that are the sum of their digit's factorials is 40730
|
||||
It took an average of 87.615 milliseconds to run this problem through 100 iterations
|
||||
*/
|
||||
Reference in New Issue
Block a user