From 180946eff306e17a7dbfe47177a8694d4d7c23b9 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Mon, 19 Oct 2020 18:13:46 -0400 Subject: [PATCH] Added solution to problem 3 --- Benchmark.ts | 2 +- ProblemSelection.ts | 4 +- Problems/Problem3.ts | 99 ++++++++++++++++++++++++++++++++++++++++++++ Unsolved.ts | 3 ++ 4 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 Problems/Problem3.ts diff --git a/Benchmark.ts b/Benchmark.ts index e55b628..8768dfa 100644 --- a/Benchmark.ts +++ b/Benchmark.ts @@ -31,7 +31,7 @@ enum BenchmarkOptions{runSpecific = 1, runAllShort, runAll, exit, size}; export class Benchmark{ - private static tooLong = []; + private static tooLong: number[] = []; //The driver function for the benchmark selection public static benchmarkMenu(): void{ let selection: BenchmarkOptions = BenchmarkOptions.size; diff --git a/ProblemSelection.ts b/ProblemSelection.ts index 4ae8db5..a65968a 100644 --- a/ProblemSelection.ts +++ b/ProblemSelection.ts @@ -26,11 +26,12 @@ const readlineSync = require("readline-sync"); import { Problem } from "./Problems/Problem"; import { Problem1 } from "./Problems/Problem1"; import { Problem2 } from "./Problems/Problem2"; +import { Problem3 } from "./Problems/Problem3"; export class ProblemSelection{ //Holds the valid problem numbers - public static PROBLEM_NUMBERS: number[] = [1, 2]; + public static PROBLEM_NUMBERS: number[] = [1, 2, 3]; //Returns the problem corresponding to the given problem number public static getProblem(problemNumber: number): Problem{ @@ -38,6 +39,7 @@ export class ProblemSelection{ switch(problemNumber){ case 1 : problem = new Problem1(); break; case 2 : problem = new Problem2(); break; + case 3 : problem = new Problem3(); break; } return problem; } diff --git a/Problems/Problem3.ts b/Problems/Problem3.ts new file mode 100644 index 0000000..0d8698a --- /dev/null +++ b/Problems/Problem3.ts @@ -0,0 +1,99 @@ +//ProjectEulerTS/Problems/Problem3.ts +//Matthew Ellison +// Created: 10-19-20 +//Modified: 10-19-20 +//The sum of the even Fibonacci numbers less than 4,000,000 +//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/typescriptClasses +/* + Copyright (C) 2020 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 { getFactors } from "../../../Typescript/typescriptClasses/Algorithms"; +import { Unsolved } from "../Unsolved"; + + +export class Problem3 extends Problem{ + //Variables + //Static variables + private static GOAL_NUMBER: number = 600851475143; //The number that needs factored + //Instance variables + private factors: number[]; //Holds the factors of goalNumber + + //Functions + //Constructor + public constructor(){ + super(`What is the largest prime factor of ${Problem3.GOAL_NUMBER}`); + this.factors = []; + } + //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 all the factors of the number + this.factors = getFactors(Problem3.GOAL_NUMBER); + //The last element should be the largest factor + + //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.factors = []; + } + //Gets + //Returns the result of solving the problem + public getResult(): string{ + if(!this.solved){ + throw new Unsolved(); + } + return `The largest factor of the number ${Problem3.GOAL_NUMBER} is ${this.factors[this.factors.length - 1]}`; + } + //Returns the list of factors of the number + public getFactors(): number[]{ + if(!this.solved){ + throw new Unsolved(); + } + return this.factors; + } + //Returns the largest factor of the number + public getLargestFactor(): number{ + if(!this.solved){ + throw new Unsolved(); + } + return this.factors[this.factors.length]; + } + //Returns the number for which we are getting the factor + public getGoalNumber(): number{ + return Problem3.GOAL_NUMBER; + } +} + +/* Results: + +*/ diff --git a/Unsolved.ts b/Unsolved.ts index af57c10..bc61754 100644 --- a/Unsolved.ts +++ b/Unsolved.ts @@ -28,6 +28,9 @@ export class Unsolved{ if(message){ this.message = message; } + else{ + this.message = ""; + } } public getMessage(){ return this.message;