//ProjectEulerTS/Problems/Problem29.ts //Matthew Ellison // Created: 05-28-21 //Modified: 07-14-21 //How many distinct terms are in the sequence generated by a^b for 2 <= a <= 100 and 2 <= b <= 100? //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"; 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{ this.solvedCheck("result"); 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[]{ this.solvedCheck("unique values for a^b"); return this.unique; } //Returns the number of unique values for a^b public getNumUnique(): number{ this.solvedCheck("number of unique values for a^b"); return this.unique.length; } } /* 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 */