From 491bbe5d650a976be0371aa3d02384ede60707da Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Mon, 29 Mar 2021 11:31:28 -0400 Subject: [PATCH] Added solution to problem 16 --- ProblemSelection.ts | 4 +- Problems/Problem16.ts | 107 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 Problems/Problem16.ts diff --git a/ProblemSelection.ts b/ProblemSelection.ts index 76eb30a..aacff0b 100644 --- a/ProblemSelection.ts +++ b/ProblemSelection.ts @@ -39,11 +39,12 @@ import { Problem12 } from "./Problems/Problem12"; import { Problem13 } from "./Problems/Problem13"; import { Problem14 } from "./Problems/Problem14"; import { Problem15 } from "./Problems/Problem15"; +import { Problem16 } from "./Problems/Problem16"; 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]; + public static PROBLEM_NUMBERS: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; //Returns the problem corresponding to the given problem number public static getProblem(problemNumber: number): Problem{ @@ -64,6 +65,7 @@ export class ProblemSelection{ case 13: problem = new Problem13(); break; case 14: problem = new Problem14(); break; case 15: problem = new Problem15(); break; + case 16: problem = new Problem16(); break; } return problem; } diff --git a/Problems/Problem16.ts b/Problems/Problem16.ts new file mode 100644 index 0000000..05802b5 --- /dev/null +++ b/Problems/Problem16.ts @@ -0,0 +1,107 @@ +//ProjectEulerTS/Problems/Problem16.ts +//Matthew Ellison +// Created: 03-29-21 +//Modified: 03-29-21 +//What is the sum of the digits of the number 2^1000? +//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 . +*/ + + +import { Unsolved } from "../Unsolved"; +import { Problem } from "./Problem"; + + +export class Problem16 extends Problem{ + //Variables + //Static variables + private static NUM_TO_POWER: bigint = 2n; //The number that is going to be raised to a power + private static POWER: bigint = 1000n; //The power that the number is going to be raised to + //Instance variables + private num: bigint; //The number to be calculated + private sumOfElements: number; //The sum of all digits in the number + + //Functions + //Construcotr + public constructor(){ + super(`What is the sum of the digits of the number ${Problem16.NUM_TO_POWER}^${Problem16.POWER}`); + this.num = 0n; + this.sumOfElements = 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(); + + //Get the number + this.num = Problem16.NUM_TO_POWER ** Problem16.POWER; + + //Get a string of the number + let numString: string = this.num.toString(); + + //Add up the individual characters of the string + for(let cnt: number = 0;cnt < numString.length;++cnt){ + this.sumOfElements += parseInt(numString.charAt(cnt)); + } + + //Stop the timer + this.timer.stop(); + + //Throw a flag to show the porblem is solved + this.solved = true; + } + //Reset the problem so it can be run again + public reset(): void{ + super.reset(); + this.num = 0n; + this.sumOfElements = 0; + } + //Gets + //Returns the result of solving the problem + public getResult(): string{ + if(!this.solved){ + throw new Unsolved(); + } + return `${Problem16.NUM_TO_POWER}^${Problem16.POWER} = ${this.num}\nThe sum of the elements is ${this.sumOfElements}`; + } + //Returns the number that was calculated + public getNumber(): bigint{ + if(!this.solved){ + throw new Unsolved(); + } + return this.num; + } + //Returns the sum o the digits of the number + public getSum(): number{ + if(!this.solved){ + throw new Unsolved(); + } + return this.sumOfElements; + } +} + +/* Results: +2^1000 = 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376 +The sum of the elements is 1366 +It took an average of 7.985 microseconds to run this problem through 100 iterations +*/