From 9cfd907b7655e7ba173818d157908dd46825e5a7 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Wed, 10 Mar 2021 12:30:04 -0500 Subject: [PATCH] Added solution to problem 7 --- ProblemSelection.ts | 4 +- Problems/Problem7.ts | 88 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 Problems/Problem7.ts diff --git a/ProblemSelection.ts b/ProblemSelection.ts index 7ef1cdc..4d39f72 100644 --- a/ProblemSelection.ts +++ b/ProblemSelection.ts @@ -30,11 +30,12 @@ import { Problem3 } from "./Problems/Problem3"; import { Problem4 } from "./Problems/Problem4"; import { Problem5 } from "./Problems/Problem5"; import { Problem6 } from "./Problems/Problem6"; +import { Problem7 } from "./Problems/Problem7"; export class ProblemSelection{ //Holds the valid problem numbers - public static PROBLEM_NUMBERS: number[] = [1, 2, 3, 4, 5, 6]; + public static PROBLEM_NUMBERS: number[] = [1, 2, 3, 4, 5, 6, 7]; //Returns the problem corresponding to the given problem number public static getProblem(problemNumber: number): Problem{ @@ -46,6 +47,7 @@ export class ProblemSelection{ case 4 : problem = new Problem4(); break; case 5 : problem = new Problem5(); break; case 6 : problem = new Problem6(); break; + case 7 : problem = new Problem7(); break; } return problem; } diff --git a/Problems/Problem7.ts b/Problems/Problem7.ts new file mode 100644 index 0000000..3ca380e --- /dev/null +++ b/Problems/Problem7.ts @@ -0,0 +1,88 @@ +//ProjectEulerTS/Problems/Problem7.ts +//Matthew Ellison +// Created: 03-10-21 +//Modified: 03-10-21 +//What is the 10001th prime number? +//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 { getNumPrimes } from "../../../Typescript/typescriptClasses/Algorithms"; + + +export class Problem7 extends Problem{ + //Variables + //Static variables + private static NUMBER_OF_PRIMES: number = 10001; //The number of primes we are trying to get + //Instance variables + private primes: number[]; + + //Functions + //Constructor + public constructor(){ + super(`What is the ${Problem7.NUMBER_OF_PRIMES}th prime number?`); + } + //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(); + + //Setup the variables + this.primes = getNumPrimes(Problem7.NUMBER_OF_PRIMES); //Holds the prime numbers + + //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.primes = []; + } + //Gets + //Returns the result of solving the problem + public getResult(): string{ + if(!this.solved){ + throw new Unsolved(); + } + return `The ${Problem7.NUMBER_OF_PRIMES}th prime number is ${this.getPrime()}`; + } + //Returns the requested prime number + public getPrime(): number{ + //If the problem hasn't been solved throw an exception + if(!this.solved){ + throw new Unsolved(); + } + return this.primes[this.primes.length - 1]; + } +} + +/* Results: +The 10001th prime number is 104743 +It took an average of 3.534 milliseconds to run this problem through 100 iterations +*/