From f56540a67083f2612f4fa50e8af27c2bc5a78d8b Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Fri, 26 Mar 2021 13:38:54 -0400 Subject: [PATCH] Added solution to problem 12 --- ProblemSelection.ts | 4 +- Problems/Problem12.ts | 135 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 Problems/Problem12.ts diff --git a/ProblemSelection.ts b/ProblemSelection.ts index dcf20c8..e57a01c 100644 --- a/ProblemSelection.ts +++ b/ProblemSelection.ts @@ -35,11 +35,12 @@ import { Problem8 } from "./Problems/Problem8"; import { Problem9 } from "./Problems/Problem9"; import { Problem10 } from "./Problems/Problem10"; import { Problem11 } from "./Problems/Problem11"; +import { Problem12 } from "./Problems/Problem12"; export class ProblemSelection{ //Holds the valid problem numbers - public static PROBLEM_NUMBERS: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]; + public static PROBLEM_NUMBERS: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; //Returns the problem corresponding to the given problem number public static getProblem(problemNumber: number): Problem{ @@ -56,6 +57,7 @@ export class ProblemSelection{ case 9 : problem = new Problem9(); break; case 10: problem = new Problem10(); break; case 11: problem = new Problem11(); break; + case 12: problem = new Problem12(); break; } return problem; } diff --git a/Problems/Problem12.ts b/Problems/Problem12.ts new file mode 100644 index 0000000..5c67d62 --- /dev/null +++ b/Problems/Problem12.ts @@ -0,0 +1,135 @@ +//ProjectEulerTS/Problems/Problem12.ts +//Matthew Ellison +// Created: 03-26-21 +//Modified: 03-26-21 +//What is the value of the first triangle number to have over five hundred divisors? +//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 { Problem } from "./Problem"; +import { Unsolved } from "../Unsolved"; +import { getDivisors } from "../../../Typescript/typescriptClasses/Algorithms"; + + +export class Problem12 extends Problem{ + //Variables + //Static variables + private static GOAL_DIVISORS: number = 500; + //Instance variables + private sum: number; //The sum of the numbers up to counter + private counter: number; //The next number to be added to sum + private divisors: number[]; //Holds the divisors of teh triangular number sum + + //Functions + //Constructor + public constructor(){ + super(`What is the value of the first triangle number to have over ${Problem12.GOAL_DIVISORS} divisors?`); + this.sum = 1; + this.counter = 2; + this.divisors = []; + } + //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; + } + + //Setup the other variables + let foundNumber: boolean = false; //To flag whether the number has been found + + //Start the timer + this.timer.start(); + + //Loop until you find the appropriate number + while((!foundNumber) && (this.sum > 0)){ + this.divisors = getDivisors(this.sum); + //If the number of divisors is correct set the flag + if(this.divisors.length > Problem12.GOAL_DIVISORS){ + foundNumber = true; + } + //Otherwise add to the sum and increase the next number + else{ + this.sum += this.counter; + ++this.counter; + } + } + + //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 = 1; + this.counter = 2; + this.divisors = []; + } + //Gets + //Returns the result of solving the problem + public getResult(): string{ + //If the problem hasn't bee solved throw an exception + if(!this.solved){ + throw new Unsolved(); + } + return `The triangular number ${this.sum} is the sum of all numbers >= ${this.counter - 1} and has ${this.divisors.length} divisors`; + } + //Returns the triangular number + public getTriangularNumber(): number{ + //If the problem hasn't bee solved throw an exception + if(!this.solved){ + throw new Unsolved(); + } + return this.sum; + } + //Get the final number that was added to the triangular number + public getLastNumberAdded(): number{ + //If the problem hasn't bee solved throw an exception + if(!this.solved){ + throw new Unsolved(); + } + return this.counter - 1; + } + //Returns the list of divisors of the requested number + public getDivisorsOfTriangularNumber(): number[]{ + //If the problem hasn't bee solved throw an exception + if(!this.solved){ + throw new Unsolved(); + } + return this.divisors; + } + //Returns the number of divisors of the requested number + public getNumberOfDivisors(): number{ + //If the problem hasn't bee solved throw an exception + if(!this.solved){ + throw new Unsolved(); + } + return this.divisors.length; + } +} + +/* Results +The triangular number 76576500 is the sum of all numbers >= 12375 and has 576 divisors +It took an average of 301.951 milliseconds to run this problem through 100 iterations +*/ +