mirror of
https://bitbucket.org/Mattrixwv/projecteulerts.git
synced 2025-12-06 17:43:59 -05:00
Added solution to problem 7
This commit is contained in:
@@ -30,11 +30,12 @@ import { Problem3 } from "./Problems/Problem3";
|
|||||||
import { Problem4 } from "./Problems/Problem4";
|
import { Problem4 } from "./Problems/Problem4";
|
||||||
import { Problem5 } from "./Problems/Problem5";
|
import { Problem5 } from "./Problems/Problem5";
|
||||||
import { Problem6 } from "./Problems/Problem6";
|
import { Problem6 } from "./Problems/Problem6";
|
||||||
|
import { Problem7 } from "./Problems/Problem7";
|
||||||
|
|
||||||
|
|
||||||
export class ProblemSelection{
|
export class ProblemSelection{
|
||||||
//Holds the valid problem numbers
|
//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
|
//Returns the problem corresponding to the given problem number
|
||||||
public static getProblem(problemNumber: number): Problem{
|
public static getProblem(problemNumber: number): Problem{
|
||||||
@@ -46,6 +47,7 @@ export class ProblemSelection{
|
|||||||
case 4 : problem = new Problem4(); break;
|
case 4 : problem = new Problem4(); break;
|
||||||
case 5 : problem = new Problem5(); break;
|
case 5 : problem = new Problem5(); break;
|
||||||
case 6 : problem = new Problem6(); break;
|
case 6 : problem = new Problem6(); break;
|
||||||
|
case 7 : problem = new Problem7(); break;
|
||||||
}
|
}
|
||||||
return problem;
|
return problem;
|
||||||
}
|
}
|
||||||
|
|||||||
88
Problems/Problem7.ts
Normal file
88
Problems/Problem7.ts
Normal file
@@ -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 <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
*/
|
||||||
Reference in New Issue
Block a user