mirror of
https://bitbucket.org/Mattrixwv/projecteulerts.git
synced 2025-12-06 17:43:59 -05:00
Added solution to problem29
This commit is contained in:
@@ -52,12 +52,13 @@ import { Problem25 } from "./Problems/Problem25";
|
||||
import { Problem26 } from "./Problems/Problem26";
|
||||
import { Problem27 } from "./Problems/Problem27";
|
||||
import { Problem28 } from "./Problems/Problem28";
|
||||
import { Problem29 } from "./Problems/Problem29";
|
||||
|
||||
|
||||
export class ProblemSelection{
|
||||
//Holds the valid problem numbers
|
||||
public static PROBLEM_NUMBERS: number[] = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
|
||||
21, 22, 23, 24, 25, 26, 27, 28];
|
||||
21, 22, 23, 24, 25, 26, 27, 28, 29];
|
||||
|
||||
//Returns the problem corresponding to the given problem number
|
||||
public static getProblem(problemNumber: number): Problem{
|
||||
@@ -91,6 +92,7 @@ export class ProblemSelection{
|
||||
case 26: problem = new Problem26(); break;
|
||||
case 27: problem = new Problem27(); break;
|
||||
case 28: problem = new Problem28(); break;
|
||||
case 29: problem = new Problem29(); break;
|
||||
}
|
||||
return problem;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//ProjectEulerTS/Problems/Problem27.ts
|
||||
//ProjectEulerTS/Problems/Problem28.ts
|
||||
//Matthew Ellison
|
||||
// Created: 05-26-21
|
||||
//Modified: 05-26-21
|
||||
|
||||
117
Problems/Problem29.ts
Normal file
117
Problems/Problem29.ts
Normal file
@@ -0,0 +1,117 @@
|
||||
//ProjectEulerTS/Problems/Problem29.ts
|
||||
//Matthew Ellison
|
||||
// Created: 05-28-21
|
||||
//Modified: 05-28-21
|
||||
//What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed by starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral
|
||||
//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 { Unsolved } from "../Unsolved";
|
||||
import { Problem } from "./Problem";
|
||||
|
||||
|
||||
export class Problem29 extends Problem{
|
||||
//Variables
|
||||
//Static variables
|
||||
private static BOTTOM_A: number = 2; //The lowest possible value for a
|
||||
private static TOP_A: number = 100; //The hightest possible value for a
|
||||
private static BOTTOM_B: number = 2; //The lowest possible value for b
|
||||
private static TOP_B: number = 100; //The highest possible value for b
|
||||
//Instance variables
|
||||
private unique: bigint[]; //Holds all unique values generated
|
||||
|
||||
//Functions
|
||||
//Constructor
|
||||
public constructor(){
|
||||
super(`How many distinct terms are in the sequence generated by a^b for ${Problem29.BOTTOM_A} <= a <= ${Problem29.TOP_A} and ${Problem29.BOTTOM_B} <= b <= ${Problem29.TOP_B}?`);
|
||||
this.unique = [];
|
||||
}
|
||||
//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();
|
||||
|
||||
//Start witht he first A and move towards the top
|
||||
for(let currentA = Problem29.BOTTOM_A;currentA <= Problem29.TOP_A;++currentA){
|
||||
//Start with the first B and move towards the top
|
||||
for(let currentB = Problem29.BOTTOM_B;currentB <= Problem29.TOP_B;++currentB){
|
||||
//Get the new number
|
||||
let currentNum: bigint = BigInt(currentA) ** BigInt(currentB);
|
||||
//If the current number is not in the array add it
|
||||
if(!this.unique.includes(currentNum)){
|
||||
this.unique.push(currentNum);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//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.unique = [];
|
||||
}
|
||||
//Gets
|
||||
//Returns the result of solving the problem
|
||||
public getResult(): string{
|
||||
if(!this.solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
return `The number of unique values generated by a^b for ${Problem29.BOTTOM_A} <= a <= ${Problem29.TOP_A} and ${Problem29.BOTTOM_B} <= b <= ${Problem29.TOP_B} is ${this.unique.length}`;
|
||||
}
|
||||
//Returns the lowest possible value for a
|
||||
public static getBottomA(): number{
|
||||
return Problem29.BOTTOM_A;
|
||||
}
|
||||
//Returns the highest possible value for a
|
||||
public static getTopA(): number{
|
||||
return Problem29.TOP_A;
|
||||
}
|
||||
//Returns the lowest possible value for b
|
||||
public static getBottomB(): number{
|
||||
return Problem29.BOTTOM_B;
|
||||
}
|
||||
//Returns the highest possible value for b
|
||||
public static getTopB(): number{
|
||||
return Problem29.TOP_B;
|
||||
}
|
||||
//Returns a list of all the unique values for a^b
|
||||
public getUnique(): bigint[]{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!this.solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
return this.unique;
|
||||
}
|
||||
}
|
||||
|
||||
/* Results:
|
||||
The number of unique values generated by a^b for 2 <= a <= 100 and 2 <= b <= 100 is 9183
|
||||
It took an average of 349.571 milliseconds to run this problem through 100 iterations
|
||||
*/
|
||||
Reference in New Issue
Block a user