mirror of
https://bitbucket.org/Mattrixwv/projecteulerts.git
synced 2025-12-06 17:43:59 -05:00
Added solution to problem 3
This commit is contained in:
@@ -31,7 +31,7 @@ enum BenchmarkOptions{runSpecific = 1, runAllShort, runAll, exit, size};
|
|||||||
|
|
||||||
|
|
||||||
export class Benchmark{
|
export class Benchmark{
|
||||||
private static tooLong = [];
|
private static tooLong: number[] = [];
|
||||||
//The driver function for the benchmark selection
|
//The driver function for the benchmark selection
|
||||||
public static benchmarkMenu(): void{
|
public static benchmarkMenu(): void{
|
||||||
let selection: BenchmarkOptions = BenchmarkOptions.size;
|
let selection: BenchmarkOptions = BenchmarkOptions.size;
|
||||||
|
|||||||
@@ -26,11 +26,12 @@ const readlineSync = require("readline-sync");
|
|||||||
import { Problem } from "./Problems/Problem";
|
import { Problem } from "./Problems/Problem";
|
||||||
import { Problem1 } from "./Problems/Problem1";
|
import { Problem1 } from "./Problems/Problem1";
|
||||||
import { Problem2 } from "./Problems/Problem2";
|
import { Problem2 } from "./Problems/Problem2";
|
||||||
|
import { Problem3 } from "./Problems/Problem3";
|
||||||
|
|
||||||
|
|
||||||
export class ProblemSelection{
|
export class ProblemSelection{
|
||||||
//Holds the valid problem numbers
|
//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
|
//Returns the problem corresponding to the given problem number
|
||||||
public static getProblem(problemNumber: number): Problem{
|
public static getProblem(problemNumber: number): Problem{
|
||||||
@@ -38,6 +39,7 @@ export class ProblemSelection{
|
|||||||
switch(problemNumber){
|
switch(problemNumber){
|
||||||
case 1 : problem = new Problem1(); break;
|
case 1 : problem = new Problem1(); break;
|
||||||
case 2 : problem = new Problem2(); break;
|
case 2 : problem = new Problem2(); break;
|
||||||
|
case 3 : problem = new Problem3(); break;
|
||||||
}
|
}
|
||||||
return problem;
|
return problem;
|
||||||
}
|
}
|
||||||
|
|||||||
99
Problems/Problem3.ts
Normal file
99
Problems/Problem3.ts
Normal file
@@ -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 <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
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:
|
||||||
|
|
||||||
|
*/
|
||||||
@@ -28,6 +28,9 @@ export class Unsolved{
|
|||||||
if(message){
|
if(message){
|
||||||
this.message = message;
|
this.message = message;
|
||||||
}
|
}
|
||||||
|
else{
|
||||||
|
this.message = "";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
public getMessage(){
|
public getMessage(){
|
||||||
return this.message;
|
return this.message;
|
||||||
|
|||||||
Reference in New Issue
Block a user