mirror of
https://bitbucket.org/Mattrixwv/projecteulerts.git
synced 2025-12-06 17:43:59 -05:00
Added solution to problem 20
This commit is contained in:
@@ -43,11 +43,12 @@ import { Problem16 } from "./Problems/Problem16";
|
||||
import { Problem17 } from "./Problems/Problem17";
|
||||
import { Problem18 } from "./Problems/Problem18";
|
||||
import { Problem19 } from "./Problems/Problem19";
|
||||
import { Problem20 } from "./Problems/Problem20";
|
||||
|
||||
|
||||
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];
|
||||
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];
|
||||
|
||||
//Returns the problem corresponding to the given problem number
|
||||
public static getProblem(problemNumber: number): Problem{
|
||||
@@ -72,6 +73,7 @@ export class ProblemSelection{
|
||||
case 17: problem = new Problem17(); break;
|
||||
case 18: problem = new Problem18(); break;
|
||||
case 19: problem = new Problem19(); break;
|
||||
case 20: problem = new Problem20(); break;
|
||||
}
|
||||
return problem;
|
||||
}
|
||||
|
||||
118
Problems/Problem20.ts
Normal file
118
Problems/Problem20.ts
Normal file
@@ -0,0 +1,118 @@
|
||||
//ProjectEulerTS/Problems/Problem19.ts
|
||||
//Matthew Ellison
|
||||
// Created: 04-07-21
|
||||
//Modified: 04-07-21
|
||||
//What is the sum of the digits of 100!?
|
||||
//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 { Unsolved } from "../Unsolved";
|
||||
import { Problem } from "./Problem";
|
||||
|
||||
|
||||
export class Problem20 extends Problem{
|
||||
//Variables
|
||||
//Static variables
|
||||
private static TOP_NUM: number = 100; //The largest number that will be multiplied
|
||||
//Instance variables
|
||||
private num: bigint; //Holds the number 100!
|
||||
private sum: number; //The sum of the digits of num
|
||||
|
||||
//Functions
|
||||
//Constructor
|
||||
public constructor(){
|
||||
super(`What is the sum of the digits of ${Problem20.TOP_NUM}!?`);
|
||||
this.num = 1n;
|
||||
this.sum = 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();
|
||||
|
||||
//Run through every number from 1 to 100 and multiply it by the current num to generate 100!
|
||||
for(let cnt = Problem20.TOP_NUM;cnt > 1;--cnt){
|
||||
this.num *= BigInt(cnt);
|
||||
}
|
||||
|
||||
//Get a string of the number because it is easier to pull aprat the individual characters
|
||||
let numString: string = String(this.num);
|
||||
//Run through every character in the string, convert it back to an integer and add it to the running sum
|
||||
for(let cnt = 0;cnt < numString.length;++cnt){
|
||||
let digit = numString.charAt(cnt);
|
||||
this.sum += parseInt(digit);
|
||||
}
|
||||
|
||||
//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.num = 1n;
|
||||
this.sum = 0;
|
||||
}
|
||||
//Gets
|
||||
//Returns the result of solving the problem
|
||||
public getResult(): string{
|
||||
if(!this.solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
return `${Problem20.TOP_NUM}! = ${this.num}\nThe sum of the digits is: ${this.sum}`;
|
||||
}
|
||||
//Returns the number 100!
|
||||
public getNumber(): bigint{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!this.solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
return this.num;
|
||||
}
|
||||
//Returns the number 100! in a string
|
||||
public getNumberString(): string{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!this.solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
return this.num.toString();
|
||||
}
|
||||
//Returns the sum of the digits of 100!
|
||||
public getSum(): number{
|
||||
//If the porblem hasn't been solved throw an exception
|
||||
if(!this.solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
return this.sum;
|
||||
}
|
||||
}
|
||||
|
||||
/* Results:
|
||||
100! = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
|
||||
The sum of the digits is: 648
|
||||
It took an average of 84.320 microseconds to run this problem through 100 iterations
|
||||
*/
|
||||
Reference in New Issue
Block a user