Added solution to problem 10

This commit is contained in:
2021-03-24 18:04:03 -04:00
parent 2f0161e4aa
commit 6a95030e72
2 changed files with 102 additions and 10 deletions

View File

@@ -24,20 +24,21 @@
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";
import { Problem4 } from "./Problems/Problem4";
import { Problem5 } from "./Problems/Problem5";
import { Problem6 } from "./Problems/Problem6";
import { Problem7 } from "./Problems/Problem7";
import { Problem8 } from "./Problems/Problem8";
import { Problem9 } from "./Problems/Problem9";
import { Problem1 } from "./Problems/Problem1";
import { Problem2 } from "./Problems/Problem2";
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";
import { Problem8 } from "./Problems/Problem8";
import { Problem9 } from "./Problems/Problem9";
import { Problem10 } from "./Problems/Problem10";
export class ProblemSelection{
//Holds the valid problem numbers
public static PROBLEM_NUMBERS: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9];
public static PROBLEM_NUMBERS: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
//Returns the problem corresponding to the given problem number
public static getProblem(problemNumber: number): Problem{
@@ -52,6 +53,7 @@ export class ProblemSelection{
case 7 : problem = new Problem7(); break;
case 8 : problem = new Problem8(); break;
case 9 : problem = new Problem9(); break;
case 10: problem = new Problem10(); break;
}
return problem;
}

90
Problems/Problem10.ts Normal file
View File

@@ -0,0 +1,90 @@
//ProjectEulerTS/Problems/Problem10.ts
//Matthew Ellison
// Created: 03-24-21
//Modified: 03-24-21
//Find the sum of all the primes below two million
//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 { getPrimes, getSum } from "../../../Typescript/typescriptClasses/Algorithms";
export class Problem10 extends Problem{
//Variables
//Static variables
private static GOAL_NUMBER = 2000000 - 1; //The largest number to check for primes
//Instance variables
private sum: number; //The sum of all of the prime numbers
//Functions
//Constructor
public constructor(){
super(`Find the sum of all the primes below ${Problem10.GOAL_NUMBER + 1}`);
this.sum = 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 sum of all prime numbers < GOAL_NUMBER
this.sum = getSum(getPrimes(Problem10.GOAL_NUMBER));
//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.sum = 0;
}
//Gets
//Returns the result of solving the problem
public getResult(): string{
//If the problem hasn't been solved throw an exception
if(!this.solved){
throw new Unsolved();
}
return `The sum of all the primes < ${Problem10.GOAL_NUMBER + 1} is ${this.sum}`;
}
//Returns the sum that was requested
public getSum(): number{
//If the problem hasn't been solved throw an exception
if(!this.solved){
throw new Unsolved();
}
return this.sum;
}
}
/* Results:
The sum of all the primes < 2000000 is 142913828922
It took an average of 170.840 milliseconds to run this problem through 100 iterations
*/