diff --git a/Problems/Problem.ts b/Problems/Problem.ts index 25c0902..ced80be 100644 --- a/Problems/Problem.ts +++ b/Problems/Problem.ts @@ -1,11 +1,11 @@ //ProjectEulerTS/Problems/Problem.ts //Matthew Ellison // Created: 10-18-20 -//Modified: 10-18-20 +//Modified: 07-14-21 //What is the sum of all the multiples of 3 or 5 that are less than 1000 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/typescriptClasses /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -21,6 +21,7 @@ along with this program. If not, see . */ + import { Stopwatch } from "../../../Typescript/typescriptClasses/Stopwatch"; import { Unsolved } from "../Unsolved"; @@ -36,24 +37,26 @@ export abstract class Problem{ this.description = description; this.solved = false; } + //Make sure the problem has been solved and throw an exception if not + protected solvedCheck(str: string): void{ + if(!this.solved){ + throw new Unsolved("You must solve the problem before you can see the " + str); + } + } //Gets - getDescription(): string{ + public getDescription(): string{ return this.description; } //Returns the result of solving the problem public abstract getResult(): string; //Returns the time taken to run the problem as a string public getTime(): string{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("time it took to run the algorithm"); return this.timer.getStr(); } //Returns the timer as a stopwatch public getTimer(): Stopwatch{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("timer"); return this.timer; } //Solve the problem @@ -63,4 +66,4 @@ export abstract class Problem{ this.timer.reset(); this.solved = false; } -} \ No newline at end of file +} diff --git a/Problems/Problem1.ts b/Problems/Problem1.ts index 2648980..b9754f3 100644 --- a/Problems/Problem1.ts +++ b/Problems/Problem1.ts @@ -1,11 +1,11 @@ //ProjectEulerTS/Problems/Problem1.ts //Matthew Ellison // Created: 10-18-20 -//Modified: 10-26-20 +//Modified: 07-14-21 //What is the sum of all the multiples of 3 or 5 that are less than 1000 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/typescriptClasses /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -25,6 +25,7 @@ import { Unsolved } from "../Unsolved"; import {Problem} from "./Problem"; + export class Problem1 extends Problem{ //Variables //Static variables @@ -48,9 +49,11 @@ export class Problem1 extends Problem{ //Start the timer this.timer.start(); + //Get the sum of the progressions of 3 and 5 and remove the sum of progressions of the overlap this.fullSum = this.sumOfProgression(3) + this.sumOfProgression(5) - this.sumOfProgression(3 * 5); + //Stop the timer this.timer.stop(); @@ -71,20 +74,17 @@ export class Problem1 extends Problem{ //Gets //Returns the result of solving the problem public getResult(): string{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("result"); return `The sum of all numbers < ${Problem1.TOP_NUM + 1} is ${this.fullSum}`; } //Returns the requested sum public getSum(): number{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("sum"); return this.fullSum; } } + /* Results: The sum of all numbers < 1000 is 233168 It took an average of 1.921 microseconds to run this problem through 100 iterations diff --git a/Problems/Problem10.ts b/Problems/Problem10.ts index 5b28657..6bb704b 100644 --- a/Problems/Problem10.ts +++ b/Problems/Problem10.ts @@ -1,7 +1,7 @@ //ProjectEulerTS/Problems/Problem10.ts //Matthew Ellison // Created: 03-24-21 -//Modified: 03-24-21 +//Modified: 07-14-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 /* @@ -23,8 +23,8 @@ import { Problem } from "./Problem"; -import { Unsolved } from "../Unsolved"; -import { getPrimes, getSum } from "../../../Typescript/typescriptClasses/Algorithms"; +import { getSum } from "../../../Typescript/typescriptClasses/ArrayAlgorithms"; +import { getPrimes } from "../../../Typescript/typescriptClasses/NumberAlgorithms"; export class Problem10 extends Problem{ @@ -51,9 +51,11 @@ export class Problem10 extends Problem{ //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(); @@ -68,22 +70,17 @@ export class Problem10 extends Problem{ //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(); - } + this.solvedCheck("result"); 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(); - } + this.solvedCheck("sum"); 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 diff --git a/Problems/Problem11.ts b/Problems/Problem11.ts index 279cc8c..65bc07b 100644 --- a/Problems/Problem11.ts +++ b/Problems/Problem11.ts @@ -1,7 +1,7 @@ //ProjectEulerTS/Problems/Problem11.ts //Matthew Ellison // Created: 03-24-21 -//Modified: 03-24-21 +//Modified: 07-14-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 /* @@ -23,8 +23,7 @@ import { Problem } from "./Problem"; -import { Unsolved } from "../Unsolved"; -import { getProd } from "../../../Typescript/typescriptClasses/Algorithms"; +import { getProd } from "../../../Typescript/typescriptClasses/ArrayAlgorithms"; export class Problem11 extends Problem{ @@ -80,6 +79,7 @@ export class Problem11 extends Problem{ //Start the timer this.timer.start(); + //Loop through every row and column for(let row = 0;row < Problem11.grid.length;++row){ for(let col = 0;col < Problem11.grid[row].length;++col){ @@ -155,6 +155,7 @@ export class Problem11 extends Problem{ } } + //Stop the timer this.timer.stop(); @@ -169,30 +170,22 @@ export class Problem11 extends Problem{ //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(); - } + this.solvedCheck("result"); return `The greatest product of 4 numbers in a line is ${getProd(this.greatestProduct)}\nThe numbers are ${this.greatestProduct}`; } //Returns the numbers that were being searched public getNumbers(): number[]{ - //If the problem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("numbers"); return this.greatestProduct; } //Returns teh product that was requested public getProduct(): number{ - //If the problem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("product of the numbers"); return getProd(this.greatestProduct); } } + /* Results: The greatest product of 4 numbers in a line is 70600674 The numbers are 89,94,97,87 diff --git a/Problems/Problem12.ts b/Problems/Problem12.ts index 5c67d62..0cc8c5d 100644 --- a/Problems/Problem12.ts +++ b/Problems/Problem12.ts @@ -1,7 +1,7 @@ //ProjectEulerTS/Problems/Problem12.ts //Matthew Ellison // Created: 03-26-21 -//Modified: 03-26-21 +//Modified: 07-14-21 //What is the value of the first triangle number to have over five hundred divisors? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/typescriptClasses /* @@ -23,8 +23,7 @@ import { Problem } from "./Problem"; -import { Unsolved } from "../Unsolved"; -import { getDivisors } from "../../../Typescript/typescriptClasses/Algorithms"; +import { getDivisors } from "../../../Typescript/typescriptClasses/NumberAlgorithms"; export class Problem12 extends Problem{ @@ -58,6 +57,7 @@ export class Problem12 extends Problem{ //Start the timer this.timer.start(); + //Loop until you find the appropriate number while((!foundNumber) && (this.sum > 0)){ this.divisors = getDivisors(this.sum); @@ -72,6 +72,7 @@ export class Problem12 extends Problem{ } } + //Stop the timer this.timer.stop(); @@ -88,46 +89,32 @@ export class Problem12 extends Problem{ //Gets //Returns the result of solving the problem public getResult(): string{ - //If the problem hasn't bee solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("result"); return `The triangular number ${this.sum} is the sum of all numbers >= ${this.counter - 1} and has ${this.divisors.length} divisors`; } //Returns the triangular number public getTriangularNumber(): number{ - //If the problem hasn't bee solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("triangular number"); return this.sum; } //Get the final number that was added to the triangular number public getLastNumberAdded(): number{ - //If the problem hasn't bee solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("last number added to get the triangular number"); return this.counter - 1; } //Returns the list of divisors of the requested number public getDivisorsOfTriangularNumber(): number[]{ - //If the problem hasn't bee solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("divisors of the triangular number"); return this.divisors; } //Returns the number of divisors of the requested number public getNumberOfDivisors(): number{ - //If the problem hasn't bee solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("number of divisors of the triangular number"); return this.divisors.length; } } + /* Results The triangular number 76576500 is the sum of all numbers >= 12375 and has 576 divisors It took an average of 301.951 milliseconds to run this problem through 100 iterations diff --git a/Problems/Problem13.ts b/Problems/Problem13.ts index 6cb21d2..125ec48 100644 --- a/Problems/Problem13.ts +++ b/Problems/Problem13.ts @@ -1,7 +1,7 @@ //ProjectEulerTS/Problems/Problem13.ts //Matthew Ellison // Created: 03-26-21 -//Modified: 03-26-21 +//Modified: 07-14-21 //Work out the first ten digits of the sum of the following one-hundred 50-digit numbers /* 37107287533902102798797998220837590246510135740250 @@ -125,8 +125,7 @@ import { Problem } from "./Problem"; -import { Unsolved } from "../Unsolved"; -import { getSumBig } from "../../../Typescript/typescriptClasses/Algorithms"; +import { getSumBig } from "../../../Typescript/typescriptClasses/ArrayAlgorithms"; export class Problem13 extends Problem{ @@ -252,9 +251,11 @@ export class Problem13 extends Problem{ //Start the timer this.timer.start(); + //Get the sum of all the numbers this.sum = getSumBig(Problem13.nums); + //Stop the timer this.timer.stop(); @@ -269,30 +270,22 @@ export class Problem13 extends Problem{ //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(); - } + this.solvedCheck("result"); return `The sum of all ${Problem13.nums.length} numbers is ${this.sum}\nThe first 10 digits of the sum of the numbers is ${this.sum.toString().substr(0, 10)}`; } //Returns the list of 50-digit numbers public getNumbers(): bigint[]{ - //If the problem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("numbers"); return Problem13.nums; } //Returns the sum of the 50-digit numbers public getSum(): bigint{ - //If the problem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("sum"); return this.sum; } } + /* Results: The sum of all 100 numbers is 5537376230390876637302048746832985971773659831892672 The first 10 digits of the sum of the numbers is 5537376230 diff --git a/Problems/Problem14.ts b/Problems/Problem14.ts index 613116e..c310df5 100644 --- a/Problems/Problem14.ts +++ b/Problems/Problem14.ts @@ -1,7 +1,7 @@ //ProjectEulerTS/Problems/Problem14.ts //Matthew Ellison // Created: 03-26-21 -//Modified: 03-26-21 +//Modified: 07-14-21 /* The following iterative sequence is defined for the set of positive integers: n → n/2 (n is even) @@ -27,7 +27,6 @@ Which starting number, under one million, produces the longest chain? */ -import { Unsolved } from "../Unsolved"; import { Problem } from "./Problem"; @@ -57,6 +56,7 @@ export class Problem14 extends Problem{ //Start the timer this.timer.start(); + //Loop through all numbers <= MAX_NUM and check them against the series for(let currentNum = 1;currentNum <= Problem14.MAX_NUM;++currentNum){ let currentLength: number = this.checkSeries(currentNum); @@ -67,6 +67,7 @@ export class Problem14 extends Problem{ } } + //Stop the timer this.timer.stop(); @@ -99,30 +100,22 @@ export class Problem14 extends Problem{ } //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(); - } + this.solvedCheck("result"); return `The number ${this.maxNum} produced a chain of ${this.maxLength} steps`; } //Returns the length of the requested chain public getLength(): number{ - //If the problem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("length of the longest chain"); return this.maxLength; } //Returns the starting number of the requested chain public getStartingNumber(): number{ - //If the problem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("starting number of the longest chain"); return this.maxNum; } } + /* Results: The number 837799 produced a chain of 525 steps It took an average of 1.178 seconds to run this problem through 100 iterations diff --git a/Problems/Problem15.ts b/Problems/Problem15.ts index 33bb09b..1f1d72d 100644 --- a/Problems/Problem15.ts +++ b/Problems/Problem15.ts @@ -1,7 +1,7 @@ //ProjectEulerTS/Problems/Problem15.ts //Matthew Ellison // Created: 03-29-21 -//Modified: 03-29-21 +//Modified: 07-14-21 //How many routes from the top left corner to the bottom right corner are there through a 20×20 grid if you can only move right and down? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/typescriptClasses /* @@ -22,7 +22,6 @@ */ -import { Unsolved } from "../Unsolved"; import { Problem } from "./Problem"; @@ -51,10 +50,12 @@ export class Problem15 extends Problem{ //Start the timer this.timer.start(); + //We write this as a recursive function //When in a location it always move right first, then down this.move(0, 0); + //Stop the timer this.timer.stop(); @@ -88,20 +89,17 @@ export class Problem15 extends Problem{ //Gets //Returns the result of solving the problem public getResult(): string{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("result"); return `The number of routes is ${this.numOfRoutes}`; } //Returns the number of routes found public getNumberOfRoutes(): number{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("number of routes"); return this.numOfRoutes; } } + /* Results: The number of routes is 137846528820 It took 37.058 minutes to solve this problem. diff --git a/Problems/Problem16.ts b/Problems/Problem16.ts index 05802b5..9e94f1c 100644 --- a/Problems/Problem16.ts +++ b/Problems/Problem16.ts @@ -1,7 +1,7 @@ //ProjectEulerTS/Problems/Problem16.ts //Matthew Ellison // Created: 03-29-21 -//Modified: 03-29-21 +//Modified: 07-14-21 //What is the sum of the digits of the number 2^1000? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/typescriptClasses /* @@ -22,7 +22,6 @@ */ -import { Unsolved } from "../Unsolved"; import { Problem } from "./Problem"; @@ -53,6 +52,7 @@ export class Problem16 extends Problem{ //Start the timer this.timer.start(); + //Get the number this.num = Problem16.NUM_TO_POWER ** Problem16.POWER; @@ -64,6 +64,7 @@ export class Problem16 extends Problem{ this.sumOfElements += parseInt(numString.charAt(cnt)); } + //Stop the timer this.timer.stop(); @@ -79,27 +80,22 @@ export class Problem16 extends Problem{ //Gets //Returns the result of solving the problem public getResult(): string{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("result"); return `${Problem16.NUM_TO_POWER}^${Problem16.POWER} = ${this.num}\nThe sum of the elements is ${this.sumOfElements}`; } //Returns the number that was calculated public getNumber(): bigint{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("number"); return this.num; } //Returns the sum o the digits of the number public getSum(): number{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("sum"); return this.sumOfElements; } } + /* Results: 2^1000 = 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376 The sum of the elements is 1366 diff --git a/Problems/Problem17.ts b/Problems/Problem17.ts index 5f1bc37..b7eea93 100644 --- a/Problems/Problem17.ts +++ b/Problems/Problem17.ts @@ -1,7 +1,7 @@ //ProjectEulerTS/Problems/Problem17.ts //Matthew Ellison // Created: 03-29-21 -//Modified: 03-29-21 +//Modified: 07-14-21 //If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/typescriptClasses /* @@ -51,6 +51,7 @@ export class Problem17 extends Problem{ //Start the timer this.timer.start(); + //Start with 1 and increment for(let num: number = Problem17.START_NUM;num <= Problem17.STOP_NUM;++num){ //Pass the number to a function that will create a string for the number @@ -59,6 +60,7 @@ export class Problem17 extends Problem{ this.letterCount += this.getNumberChars(currentNumString); } + //Stop the timer this.timer.stop(); @@ -185,20 +187,17 @@ export class Problem17 extends Problem{ //Gets //Returns the result of solving the problem public getResult(): string{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("result"); return `The sum of all the letters in all the numbers ${Problem17.START_NUM}-${Problem17.STOP_NUM} is ${this.letterCount}`; } //Returns the number of letters asked for public getLetterCount(): number{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("letter count"); return this.letterCount; } } + /* Results: The sum of all the letters in all the numbers 1-1000 is 21124 It took an average of 485.744 microseconds to run this problem through 100 iterations diff --git a/Problems/Problem18.ts b/Problems/Problem18.ts index 2ad76d5..27c5729 100644 --- a/Problems/Problem18.ts +++ b/Problems/Problem18.ts @@ -1,7 +1,7 @@ //ProjectEulerTS/Problems/Problem18.ts //Matthew Ellison // Created: 04-05-21 -//Modified: 04-05-21 +//Modified: 07-14-21 //Find the maximum total from top to bottom /* 75 @@ -40,7 +40,6 @@ */ -import { Unsolved } from "../Unsolved"; import { Problem } from "./Problem"; @@ -103,6 +102,7 @@ export class Problem18 extends Problem{ //Start the timer this.timer.start(); + //Invert the list this.invert(workingList); @@ -154,6 +154,7 @@ export class Problem18 extends Problem{ //Get the correct total which wil be the inversion of the current one this.actualTotal = ((100 * workingList.length) - this.foundPoints[this.foundPoints.length - 1].total); + //Stop the timer this.timer.stop(); @@ -170,25 +171,23 @@ export class Problem18 extends Problem{ //Gets //Returns the result of solving the problem public getResult(): string{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("result"); return `The value of the longest path is ${this.actualTotal}`; } //Returns the pyramid that was traversed as a string public getPyramid(): string{ + this.solvedCheck("pyramid of numbers"); return Problem18.list.toString(); } //Returns the trail the algorithm took as a string public getTrail(): string{ + this.solvedCheck("trail of the shortest path"); //TODO: return ""; } //Returns the total that was asked for public getTotal(): number{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("total"); return this.actualTotal; } } @@ -208,6 +207,7 @@ class location{ } } + /* Results: The value of the longest path is 1074 It took an average of 134.407 microseconds to run this problem through 100 iterations diff --git a/Problems/Problem19.ts b/Problems/Problem19.ts index 0662550..11c4693 100644 --- a/Problems/Problem19.ts +++ b/Problems/Problem19.ts @@ -1,7 +1,7 @@ //ProjectEulerTS/Problems/Problem18.ts //Matthew Ellison // Created: 04-06-21 -//Modified: 04-06-21 +//Modified: 07-14-21 //How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)? /* You are given the following information, but you may prefer to do some research for yourself. @@ -33,7 +33,6 @@ A leap year occurs on any year evenly divisible by 4, but not on a century unles */ -import { Unsolved } from "../Unsolved"; import { Problem } from "./Problem"; @@ -66,6 +65,7 @@ export class Problem19 extends Problem{ //Start the timer this.timer.start(); + //Run for all years 1901-2000 for(let year = Problem19.START_YEAR;year <= Problem19.END_YEAR;++year){ //Run for all months in the year @@ -80,6 +80,7 @@ export class Problem19 extends Problem{ } } + //Stop the timer this.timer.stop(); @@ -194,20 +195,17 @@ export class Problem19 extends Problem{ //Gets //Returns the result of solving the problem public getResult(): string{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("result"); return `There are ${this.totalSundays} Sundays that landed on the first of the months from ${Problem19.START_YEAR} to ${Problem19.END_YEAR}`; } //Returns the total sundays that were asked for public getTotalSundays(): number{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("total number of sundays"); return this.totalSundays; } } + /* Results: There are 171 Sundays that landed on the first of the months from 1901 to 2000 It took an average of 4.314 milliseconds to run this problem through 100 iterations diff --git a/Problems/Problem2.ts b/Problems/Problem2.ts index 41c55a0..b9bcfe6 100644 --- a/Problems/Problem2.ts +++ b/Problems/Problem2.ts @@ -1,11 +1,11 @@ //ProjectEulerTS/Problems/Problem2.ts //Matthew Ellison // Created: 10-19-20 -//Modified: 10-19-20 +//Modified: 07-14-21 //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 + 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 @@ -23,8 +23,7 @@ import { Problem } from "./Problem"; -import { getAllFib } from "../../../Typescript/typescriptClasses/Algorithms"; -import { Unsolved } from "../Unsolved"; +import { getAllFib } from "../../../Typescript/typescriptClasses/NumberAlgorithms"; export class Problem2 extends Problem{ @@ -51,6 +50,7 @@ export class Problem2 extends Problem{ //Start the timer this.timer.start(); + //Get a list of all fibonacci numbers <= TOP_NUM let fibNums = getAllFib(Problem2.TOP_NUM); //Setp through every element in the list checking ifit is even @@ -61,6 +61,7 @@ export class Problem2 extends Problem{ } }); + //Stop the timer this.timer.stop(); @@ -75,20 +76,17 @@ export class Problem2 extends Problem{ //Gets //Returns the result of solving the problem public getResult(): string{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("result"); return `The sum of all even fibonacci numbers <= ${Problem2.TOP_NUM} is ${this.fullSum}`; } //Returns the requested sum public getSum(): number{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("sum"); return this.fullSum; } } + /* Results: The sum of all even fibonacci numbers <= 3999999 is 4613732 It took an average of 6.330 microseconds to run this problem through 100 iterations diff --git a/Problems/Problem20.ts b/Problems/Problem20.ts index bff3fbd..d0847da 100644 --- a/Problems/Problem20.ts +++ b/Problems/Problem20.ts @@ -1,7 +1,7 @@ //ProjectEulerTS/Problems/Problem20.ts //Matthew Ellison // Created: 04-07-21 -//Modified: 04-07-21 +//Modified: 07-14-21 //What is the sum of the digits of 100!? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/typescriptClasses /* @@ -22,7 +22,6 @@ */ -import { Unsolved } from "../Unsolved"; import { Problem } from "./Problem"; @@ -52,6 +51,7 @@ export class Problem20 extends Problem{ //Start the timer this.timer.start(); + //Run through every number from 1 to 100 and multiply it by the current num to generate 100! for(let cnt = Problem20.TOP_NUM;cnt > 1;--cnt){ this.num *= BigInt(cnt); @@ -65,6 +65,7 @@ export class Problem20 extends Problem{ this.sum += parseInt(digit); } + //Stop the timer this.timer.stop(); @@ -80,37 +81,27 @@ export class Problem20 extends Problem{ //Gets //Returns the result of solving the problem public getResult(): string{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("result"); return `${Problem20.TOP_NUM}! = ${this.num}\nThe sum of the digits is: ${this.sum}`; } //Returns the number 100! public getNumber(): bigint{ - //If the problem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("number"); return this.num; } //Returns the number 100! in a string public getNumberString(): string{ - //If the problem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("number as a string"); return this.num.toString(); } //Returns the sum of the digits of 100! public getSum(): number{ - //If the porblem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("sum of the digits"); return this.sum; } } + /* Results: 100! = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 The sum of the digits is: 648 diff --git a/Problems/Problem21.ts b/Problems/Problem21.ts index 902e2d2..174555e 100644 --- a/Problems/Problem21.ts +++ b/Problems/Problem21.ts @@ -1,7 +1,7 @@ //ProjectEulerTS/Problems/Problem21.ts //Matthew Ellison // Created: 04-08-21 -//Modified: 04-08-21 +//Modified: 07-14-21 //Evaluate the sum of all the amicable numbers under 10000 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/typescriptClasses /* @@ -22,8 +22,8 @@ */ -import { getDivisors, getSum } from "../../../Typescript/typescriptClasses/Algorithms"; -import { Unsolved } from "../Unsolved"; +import { getSum } from "../../../Typescript/typescriptClasses/ArrayAlgorithms"; +import { getDivisors } from "../../../Typescript/typescriptClasses/NumberAlgorithms"; import { Problem } from "./Problem"; @@ -62,6 +62,7 @@ export class Problem21 extends Problem{ //Start the timer this.timer.start(); + //Generate the divisors of all numbers < 10000, get their sum, and add it to the list for(let cnt = 1;cnt < Problem21.LIMIT;++cnt){ let divisors: number[] = getDivisors(cnt); //Get all the divisors of a number @@ -91,6 +92,7 @@ export class Problem21 extends Problem{ //Sort the array for neatness this.amicable.sort((n1, n2) => n1 - n2); + //Stop the timer this.timer.stop(); @@ -107,9 +109,7 @@ export class Problem21 extends Problem{ //Gets //Returns the result of solving the problem public getResult(): string{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("result"); let result: string = `All amicable numbers less than ${Problem21.LIMIT} are\n`; for(let cnt = 0;cnt < this.amicable.length;++cnt){ result += `${this.amicable[cnt]}\n`; @@ -120,22 +120,17 @@ export class Problem21 extends Problem{ } //Returns a vector with all of the amicable numbers calculated public getAmicable(): number[]{ - //If the problem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("amicable numbers"); return this.amicable; } //Returns the sum of all of the amicable numbers public getSum(): number{ - //If the problem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("sum of the amicable numbers"); return getSum(this.amicable); } } + /* Results: The sum of all of these amicable numbers is 31626 It took an average of 7.454 milliseconds to run this problem through 100 iterations diff --git a/Problems/Problem22.ts b/Problems/Problem22.ts index e4dfc9d..09d3e0a 100644 --- a/Problems/Problem22.ts +++ b/Problems/Problem22.ts @@ -1,7 +1,7 @@ //ProjectEulerTS/Problems/Problem22.ts //Matthew Ellison // Created: 04-08-21 -//Modified: 04-08-21 +//Modified: 07-14-21 //What is the total of all the name scores in this file? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/typescriptClasses /* @@ -21,8 +21,8 @@ along with this program. If not, see . */ -import { getSum } from "../../../Typescript/typescriptClasses/Algorithms"; -import { Unsolved } from "../Unsolved"; + +import { getSum } from "../../../Typescript/typescriptClasses/ArrayAlgorithms"; import { Problem } from "./Problem"; @@ -422,6 +422,7 @@ export class Problem22 extends Problem{ //Start the timer this.timer.start(); + //Sort all the names Problem22.names.sort(); //Step through every name adding up the values of the characters @@ -440,6 +441,7 @@ export class Problem22 extends Problem{ //Get the sum of all the numbers this.sum = getSum(this.prod); + //Stop the timer this.timer.stop(); @@ -456,9 +458,7 @@ export class Problem22 extends Problem{ //Gets //Returns the result of solving the problem public getResult(): string{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("result"); return `The answer to the question is ${this.sum}`; } //Returns the vector of the names being scored @@ -467,13 +467,12 @@ export class Problem22 extends Problem{ } //Returns the sum of the names scores public getNameScoreSum(): number{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("score of the names"); return this.sum; } } + /* Results: The answer to the question is 871198282 It took an average of 473.266 microseconds to run this problem through 100 iterations diff --git a/Problems/Problem23.ts b/Problems/Problem23.ts index 95a5513..8f999e5 100644 --- a/Problems/Problem23.ts +++ b/Problems/Problem23.ts @@ -1,7 +1,7 @@ //ProjectEulerTS/Problems/Problem23.ts //Matthew Ellison // Created: 04-08-21 -//Modified: 04-08-21 +//Modified: 07-14-21 //Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/typescriptClasses /* @@ -22,8 +22,8 @@ */ -import { getDivisors, getSum } from "../../../Typescript/typescriptClasses/Algorithms"; -import { Unsolved } from "../Unsolved"; +import { getSum } from "../../../Typescript/typescriptClasses/ArrayAlgorithms"; +import { getDivisors } from "../../../Typescript/typescriptClasses/NumberAlgorithms"; import { Problem } from "./Problem"; @@ -122,20 +122,17 @@ export class Problem23 extends Problem{ //Gets //Returns the result of solving the problem public getResult(): string{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("result"); return `The answer is ${this.sum}`; } //Returns the sum of the numbers asked for public getSum(): number{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("sum"); return this.sum; } } + /* Results: The answer is 4179871 It took an average of 7.798 seconds to run this problem through 100 iterations diff --git a/Problems/Problem24.ts b/Problems/Problem24.ts index 6d245c6..baf65ac 100644 --- a/Problems/Problem24.ts +++ b/Problems/Problem24.ts @@ -1,7 +1,7 @@ //ProjectEulerTS/Problems/Problem24.ts //Matthew Ellison // Created: 04-08-21 -//Modified: 04-08-21 +//Modified: 07-14-21 //What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/typescriptClasses /* @@ -22,8 +22,7 @@ */ -import { getPermutations } from "../../../Typescript/typescriptClasses/Algorithms"; -import { Unsolved } from "../Unsolved"; +import { getPermutations } from "../../../Typescript/typescriptClasses/StringAlgorithms"; import { Problem } from "./Problem"; @@ -52,9 +51,11 @@ export class Problem24 extends Problem{ //Start the timer this.timer.start(); + //Get all the permutations of the string this.permutations = getPermutations(Problem24.nums); + //Stop the timer this.timer.stop(); @@ -69,28 +70,22 @@ export class Problem24 extends Problem{ //Gets //Returns the result of solving the problem public getResult(): string{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("result"); return `The 1 millionth permutation is ${this.permutations[Problem24.NEEDED_PERM - 1]}`; } //Returns an array with all of the permutations public getPermutationsList(): string[]{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("permutations"); return this.permutations; } //Returns the requested permutation public getPermutation(){ - //If the problem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("1,000,000th permutation"); return this.permutations[Problem24.NEEDED_PERM - 1]; } } + /* Results: The 1 millionth permutation is 2783915460 It took an average of 1.946 seconds to run this problem through 100 iterations diff --git a/Problems/Problem25.ts b/Problems/Problem25.ts index 91595d1..0f3a654 100644 --- a/Problems/Problem25.ts +++ b/Problems/Problem25.ts @@ -1,7 +1,7 @@ //ProjectEulerTS/Problems/Problem25.ts //Matthew Ellison // Created: 04-08-21 -//Modified: 04-08-21 +//Modified: 07-14-21 //What is the index of the first term in the Fibonacci sequence to contain 1000 digits? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/typescriptClasses /* @@ -22,8 +22,7 @@ */ -import { getFibBig } from "../../../Typescript/typescriptClasses/Algorithms"; -import { Unsolved } from "../Unsolved"; +import { getFibBig } from "../../../Typescript/typescriptClasses/NumberAlgorithms"; import { Problem } from "./Problem"; @@ -53,12 +52,14 @@ export class Problem25 extends Problem{ //Start the timer this.timer.start(); + //Move through all Fibonacci numbers until you reach the one with at least NUM_DIGITS digits while(this.number.toString().length < Problem25.NUM_DIGITS){ this.index += 1n; //Increase the index number. Doing this at the beginning keeps the index correct at the end of the loop this.number = getFibBig(this.index); //Calculate the number } + //Stop the timer this.timer.stop(); @@ -74,48 +75,37 @@ export class Problem25 extends Problem{ //Gets //Returns the result of solving the problem public getResult(): string{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("result"); return `The first Fibonacci number with ${Problem25.NUM_DIGITS} digits is ${this.number}\nIts index is ${this.index}`; } //Returns the Fibonacci number asked for public getNumber(): bigint{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("fibonacci number"); return this.number; } //Returns the Fibonacci number asked for as a string public getNumberString(): string{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("fibonacci number as a string"); return this.number.toString(); } //Returns the index of the requested Fibonacci number public getIndex(): bigint{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("index of the fibonacci number"); return this.index; } //Returns the index of the requested Fibonacci number as a string public getIndexString(): string{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("index of the fibonacci number as a string"); return this.index.toString(); } //Returns the index of the requested Fibonacci number as a number public getIndexInt(): number{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("index of the fibonacci number as an int"); return Number(this.index); } } + /* Results: The first Fibonacci number with 1000 digits is 1070066266382758936764980584457396885083683896632151665013235203375314520604694040621889147582489792657804694888177591957484336466672569959512996030461262748092482186144069433051234774442750273781753087579391666192149259186759553966422837148943113074699503439547001985432609723067290192870526447243726117715821825548491120525013201478612965931381792235559657452039506137551467837543229119602129934048260706175397706847068202895486902666185435124521900369480641357447470911707619766945691070098024393439617474103736912503231365532164773697023167755051595173518460579954919410967778373229665796581646513903488154256310184224190259846088000110186255550245493937113651657039447629584714548523425950428582425306083544435428212611008992863795048006894330309773217834864543113205765659868456288616808718693835297350643986297640660000723562917905207051164077614812491885830945940566688339109350944456576357666151619317753792891661581327159616877487983821820492520348473874384736771934512787029218636250627816 Its index is 4782 diff --git a/Problems/Problem26.ts b/Problems/Problem26.ts index ba730fe..360546e 100644 --- a/Problems/Problem26.ts +++ b/Problems/Problem26.ts @@ -1,7 +1,7 @@ //ProjectEulerTS/Problems/Problem26.ts //Matthew Ellison // Created: 04-18-21 -//Modified: 04-18-21 +//Modified: 07-14-21 //Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part. //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/typescriptClasses /* @@ -22,7 +22,6 @@ */ -import { Unsolved } from "../Unsolved"; import { Problem } from "./Problem"; @@ -52,6 +51,7 @@ export class Problem26 extends Problem{ //Start the timer this.timer.start(); + //Start with 1/2 and find out how long the logest cycle is by checking the reainders //Loop through every number from 2-999 and use it for the denominator for(let denominator: number = 2;denominator <= Problem26.TOP_NUM;++denominator){ @@ -88,6 +88,7 @@ export class Problem26 extends Problem{ } } + //Stop the timer this.timer.stop(); @@ -103,27 +104,22 @@ export class Problem26 extends Problem{ //Gets //Returns the result of solving the problem public getResult(): string{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("result"); return `The longest cycle is ${this.longestCycle} digits long\nIt started with the number ${this.longestNumber}`; } //Returns the length of the longest cycle public getLongestCycle(): number{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("length of the longest cycle"); return this.longestCycle; } //Returns the denominator that start the longest cycle public getLongestNumber(){ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("denominator that starts the longest cycle"); return this.longestNumber; } } + /* Results: The longest cycle is 982 digits long It started with the number 983 diff --git a/Problems/Problem27.ts b/Problems/Problem27.ts index 3a9ebbf..99124d0 100644 --- a/Problems/Problem27.ts +++ b/Problems/Problem27.ts @@ -1,7 +1,7 @@ //ProjectEulerTS/Problems/Problem26.ts //Matthew Ellison // Created: 05-24-21 -//Modified: 05-24-21 +//Modified: 07-14-21 //Find the product of the coefficients, |a| < 1000 and |b| <= 1000, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n=0. //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/typescriptClasses /* @@ -22,8 +22,7 @@ */ -import { isPrime } from "../../../Typescript/typescriptClasses/Algorithms"; -import { Unsolved } from "../Unsolved"; +import { isPrime } from "../../../Typescript/typescriptClasses/NumberAlgorithms"; import { Problem } from "./Problem"; @@ -56,6 +55,7 @@ export class Problem27 extends Problem{ //Start the timer this.timer.start(); + //Start with the lowest possible A and check all possibilities after that for(let a: number = -Problem27.LARGEST_POSSIBLE_A; a <= Problem27.LARGEST_POSSIBLE_A;++a){ //Start witht he lowest possible B and check all possibilities after that @@ -78,6 +78,7 @@ export class Problem27 extends Problem{ } } + //Stop the timer this.timer.stop(); @@ -94,45 +95,32 @@ export class Problem27 extends Problem{ //Gets //Returns the result of solving the problem public getResult(): string{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("result"); return `The greatest number of primes found is ${this.topN}\nIt was found with A = ${this.topA}, B = ${this.topB}\nThe product of A and B is ${this.getProduct()}`; } //Returns the top A that was generated public getTopA(): number{ - //If the problem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("largest A"); return this.topA; } //Returns the top B that was generated public getTopB(): number{ - //If the problem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("largest B"); return this.topB; } //Returns the top N that was generated public getTopN(): number{ - //If the problem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("largest N"); return this.topN; } //Returns the product of A and B for the answer public getProduct(): number{ - //If the problem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("product of A and B"); return this.topA * this.topB; } } + /* Results: The greatest number of primes found is 70 It was found with A = -61, B = 971 diff --git a/Problems/Problem28.ts b/Problems/Problem28.ts index 1825b39..c8e0ba9 100644 --- a/Problems/Problem28.ts +++ b/Problems/Problem28.ts @@ -1,7 +1,7 @@ //ProjectEulerTS/Problems/Problem28.ts //Matthew Ellison // Created: 05-26-21 -//Modified: 05-26-21 +//Modified: 07-14-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 /* @@ -22,7 +22,6 @@ */ -import { Unsolved } from "../Unsolved"; import { Problem } from "./Problem"; @@ -122,11 +121,13 @@ export class Problem28 extends Problem{ //Start the timer this.timer.start(); + //Setup the grid this.setupGrid(); //FInd the sum of the diagonals in the grid this.findSum(); + //Stop the timer this.timer.stop(); @@ -141,29 +142,22 @@ export class Problem28 extends Problem{ //Gets //Returns the result of solving the problem public getResult(): string{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("result"); return `The sum of the diagonals in the given grid is ${this.sumOfDiagonals}`; } //Returns the grid public getGrid(): number[][]{ - //IF the problem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("grid"); return Problem28.grid; } //Returns the sum of the diagonals public getSum(): number{ - //If the problem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("sum"); return this.sumOfDiagonals; } } + /* Results: The sum of the diagonals in the given grid is 669171001 It took an average of 16.924 milliseconds to run this problem through 100 iterations diff --git a/Problems/Problem29.ts b/Problems/Problem29.ts index f26096b..a0f9f3d 100644 --- a/Problems/Problem29.ts +++ b/Problems/Problem29.ts @@ -1,7 +1,7 @@ //ProjectEulerTS/Problems/Problem29.ts //Matthew Ellison // Created: 05-28-21 -//Modified: 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 /* @@ -22,7 +22,6 @@ */ -import { Unsolved } from "../Unsolved"; import { Problem } from "./Problem"; @@ -53,6 +52,7 @@ export class Problem29 extends Problem{ //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 @@ -66,6 +66,7 @@ export class Problem29 extends Problem{ } } + //Stop the timer this.timer.stop(); @@ -80,9 +81,7 @@ export class Problem29 extends Problem{ //Gets //Returns the result of solving the problem public getResult(): string{ - if(!this.solved){ - throw new Unsolved(); - } + 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 @@ -103,14 +102,17 @@ export class Problem29 extends Problem{ } //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(); - } + 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 diff --git a/Problems/Problem3.ts b/Problems/Problem3.ts index 8900aa8..b769988 100644 --- a/Problems/Problem3.ts +++ b/Problems/Problem3.ts @@ -1,11 +1,11 @@ //ProjectEulerTS/Problems/Problem3.ts //Matthew Ellison // Created: 10-19-20 -//Modified: 10-19-20 +//Modified: 07-14-21 //The largest prime factor of 600851475143 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/typescriptClasses /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -23,8 +23,7 @@ import { Problem } from "./Problem"; -import { getFactors } from "../../../Typescript/typescriptClasses/Algorithms"; -import { Unsolved } from "../Unsolved"; +import { getFactors } from "../../../Typescript/typescriptClasses/NumberAlgorithms"; export class Problem3 extends Problem{ @@ -51,10 +50,12 @@ export class Problem3 extends Problem{ //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(); @@ -69,23 +70,17 @@ export class Problem3 extends Problem{ //Gets //Returns the result of solving the problem public getResult(): string{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("result"); 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(); - } + this.solvedCheck("factors"); return this.factors; } //Returns the largest factor of the number public getLargestFactor(): number{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("largest factor"); return this.factors[this.factors.length]; } //Returns the number for which we are getting the factor @@ -94,6 +89,7 @@ export class Problem3 extends Problem{ } } + /* Results: */ diff --git a/Problems/Problem30.ts b/Problems/Problem30.ts index 83a544e..7890d29 100644 --- a/Problems/Problem30.ts +++ b/Problems/Problem30.ts @@ -1,7 +1,7 @@ //ProjectEulerTS/Problems/Problem30.ts //Matthew Ellison // Created: 05-28-21 -//Modified: 05-28-21 +//Modified: 07-14-21 //Find the sum of all the numbers that can be written as the sum of the fifth powers of their digits. //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/typescriptClasses /* @@ -22,8 +22,7 @@ */ -import { getSum } from "../../../Typescript/typescriptClasses/Algorithms"; -import { Unsolved } from "../Unsolved"; +import { getSum } from "../../../Typescript/typescriptClasses/ArrayAlgorithms"; import { Problem } from "./Problem"; @@ -66,6 +65,7 @@ export class Problem30 extends Problem{ //Start the timer this.timer.start(); + //Start with the lowest number and increment until you reach the largest number for(let currentNum = Problem30.BOTTOM_NUM;currentNum <= Problem30.TOP_NUM;++currentNum){ //Get the digits of the number @@ -85,6 +85,7 @@ export class Problem30 extends Problem{ //Get the sum of the numbers this.sum = getSum(this.sumOfFifthNumbers); + //Stop the timer this.timer.stop(); @@ -99,9 +100,7 @@ export class Problem30 extends Problem{ //Gets //Returns the result of solving the problem public getResult(): string{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("result"); return `This sum of all the numbers that can be written as the sum of the fifth powers of their digits is ${this.sum}`; } //This returns the top number to be checked @@ -110,22 +109,17 @@ export class Problem30 extends Problem{ } //This returns a copy of the vector holding all the numbers that are the sum of the fifth power of their digits public getListOfSumOfFifths(): number[]{ - //If the problem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("list of all numbers that are the sum of the 5th power of their digits"); return this.sumOfFifthNumbers; } //This returns the sum of all entries in sumOfFifthNumbers public getSumOfList(): number{ - //If the problem hasn't been solved throw an eception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("sum of all numbers that are the sum of the 5th power of their digits"); return this.sum; } } + /* Results: This sum of all the numbers that can be written as the sum of the fifth powers of their digits is 443839 It took an average of 180.976 milliseconds to run this problem through 100 iterations diff --git a/Problems/Problem31.ts b/Problems/Problem31.ts index 7007001..75a3cb1 100644 --- a/Problems/Problem31.ts +++ b/Problems/Problem31.ts @@ -1,7 +1,7 @@ //ProjectEulerTS/Problems/Problem31.ts //Matthew Ellison // Created: 05-28-21 -//Modified: 05-28-21 +//Modified: 07-14-21 //How many different ways can £2 be made using any number of coins? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/typescriptClasses /* @@ -22,7 +22,6 @@ */ -import { Unsolved } from "../Unsolved"; import { Problem } from "./Problem"; @@ -50,6 +49,7 @@ export class Problem31 extends Problem{ //Start the timer this.timer.start(); + //Start with 200p and remove the necessary coins with each loop for(let pound2 = Problem31.DESIRED_VALUE;pound2 >= 0;pound2 -= 200){ for(let pound1 = pound2;pound1 >= 0;pound1 -= 100){ @@ -67,6 +67,7 @@ export class Problem31 extends Problem{ } } + //Stop the timer this.timer.stop(); @@ -81,21 +82,17 @@ export class Problem31 extends Problem{ //Gets //Returns the result of solving the proble public getResult(): string{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("result"); return `There are ${this.permutations} ways to make 2 pounds with the given denominations of coins`; } //Returns the number of correct permutations of the coins public getPermutations(): number{ - //If the problem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("number of correct permutations of the coins"); return this.permutations; } } + /* Results: There are 73682 ways to make 2 pounds with the given denominations of coins It took an average of 197.293 microseconds to run this problem through 100 iterations diff --git a/Problems/Problem32.ts b/Problems/Problem32.ts index cf8dbb9..50957c6 100644 --- a/Problems/Problem32.ts +++ b/Problems/Problem32.ts @@ -1,7 +1,7 @@ //ProjectEulerTS/Problems/Problem32.ts //Matthew Ellison // Created: 05-28-21 -//Modified: 05-28-21 +//Modified: 07-14-21 //Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital. //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/typescriptClasses /* @@ -22,8 +22,7 @@ */ -import { findNumOccurrence } from "../../../Typescript/typescriptClasses/Algorithms"; -import { Unsolved } from "../Unsolved"; +import { findNumOccurrence } from "../../../Typescript/typescriptClasses/StringAlgorithms"; import { Problem } from "./Problem"; @@ -85,6 +84,7 @@ export class Problem32 extends Problem{ //Start the timer this.timer.start(); + //Create the multiplicand and start working your way up for(let multiplicand = 1;multiplicand <= Problem32.TOP_MULTIPLICAND;++multiplicand){ //Run through all possible multipliers @@ -108,6 +108,7 @@ export class Problem32 extends Problem{ this.sumOfPandigitals += prod.getProduct(); } + //Stop the timer this.timer.stop(); @@ -140,21 +141,17 @@ export class Problem32 extends Problem{ } //Gets public getResult(): string{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("result"); return `There are ${this.listOfProducts.length} unique 1-9 pandigitals\nThe sum of the products of the pandigitals is ${this.sumOfPandigitals}` } //Returns the sum of pandigitals public getSumOfPandigitlas(): number{ - //If the problem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("sum of the pandigitals"); return this.sumOfPandigitals; } } + /* Results: There are 7 unique 1-9 pandigitals The sum of the products of the pandigitals is 45228 diff --git a/Problems/Problem33.ts b/Problems/Problem33.ts index ca001ba..55bbb0e 100644 --- a/Problems/Problem33.ts +++ b/Problems/Problem33.ts @@ -1,7 +1,7 @@ //ProjectEulerTS/Problems/Problem33.ts //Matthew Ellison // Created: 05-28-21 -//Modified: 05-28-21 +//Modified: 07-14-21 /* The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s We shall consider fractions like, 30/50 = 3/5, to be trivial examples @@ -27,8 +27,8 @@ If the product of these four fractions is given in its lowest common terms, find */ -import { gcd, getProd } from "../../../Typescript/typescriptClasses/Algorithms"; -import { Unsolved } from "../Unsolved"; +import { getProd } from "../../../Typescript/typescriptClasses/ArrayAlgorithms"; +import { gcd } from "../../../Typescript/typescriptClasses/NumberAlgorithms"; import { Problem } from "./Problem"; @@ -63,6 +63,7 @@ export class Problem33 extends Problem{ //Start the timer this.timer.start(); + //Search every possible numerator/denominator pair for(let denominator = Problem33.MIN_DENOMINATOR;denominator <= Problem33.MAX_DENOMINATOR;++denominator){ for(let numerator = Problem33.MIN_NUMERATOR;(numerator < denominator) && (numerator <= Problem33.MAX_NUMERATOR);++numerator){ @@ -109,6 +110,7 @@ export class Problem33 extends Problem{ //Save the denominator this.prodDenominator = denomProd / curGcd; + //Stop the timer this.timer.stop(); @@ -124,37 +126,27 @@ export class Problem33 extends Problem{ //Gets //Returns the reult of solving the problem public getResult(): string{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("result"); return `The denominator of the product is ${this.prodDenominator}`; } //Returns the list of numerators public getNumerators(): number[]{ - //If the problem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("list of numerators"); return this.numerators; } //Returns the list of denominators public getDenominators(): number[]{ - //If the porblem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("list of denominators"); return this.denominators; } //Returns the answer to the question public getProdDenominator(){ - //If the problem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("denominator"); return this.prodDenominator; } } + /* Results: The denominator of the product is 100 It took an average of 508.730 microseconds to run this problem through 100 iterations diff --git a/Problems/Problem34.ts b/Problems/Problem34.ts index 2c96e35..85ad7fb 100644 --- a/Problems/Problem34.ts +++ b/Problems/Problem34.ts @@ -1,7 +1,7 @@ //ProjectEulerTS/Problems/Problem34.ts //Matthew Ellison // Created: 06-01-21 -//Modified: 06-01-21 +//Modified: 07-14-21 //Find the sum of all numbers which are equal to the sum of the factorial of their digits //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/typescriptClasses /* @@ -22,8 +22,7 @@ */ -import { factorial } from "../../../Typescript/typescriptClasses/Algorithms"; -import { Unsolved } from "../Unsolved"; +import { factorial } from "../../../Typescript/typescriptClasses/NumberAlgorithms"; import { Problem } from "./Problem"; @@ -56,6 +55,7 @@ export class Problem34 extends Problem{ //Start the timer this.timer.start(); + //Pre-compute the possible factorials form 0! to 9! for(let cnt = 0;cnt <= 9;++cnt){ this.factorials[cnt] = factorial(cnt); @@ -75,6 +75,7 @@ export class Problem34 extends Problem{ } } + //Stop the timer this.timer.stop(); @@ -93,30 +94,22 @@ export class Problem34 extends Problem{ //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(); - } + this.solvedCheck("result"); return `The sum of all numbers that are the sum of their digit's factorials is ${this.sum}`; } //Returns the list of factorials from 0-9 public getFactorials(): number[]{ - //If the problem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("list of factorials"); return this.factorials; } //Returns the sum of all numbers equal to the sum of their digit's factorials public getSum(): number{ - //If the problem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("sum"); return this.sum; } } + /* Results: The sum of all numbers that are the sum of their digit's factorials is 40730 It took an average of 87.615 milliseconds to run this problem through 100 iterations diff --git a/Problems/Problem35.ts b/Problems/Problem35.ts index 5999307..2091b73 100644 --- a/Problems/Problem35.ts +++ b/Problems/Problem35.ts @@ -1,7 +1,7 @@ //ProjectEulerTS/Problems/Problem35.ts //Matthew Ellison // Created: 06-06-21 -//Modified: 06-06-21 +//Modified: 07-14-21 //How many circular primes are there below one million? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/typescriptClasses /* @@ -22,8 +22,7 @@ */ -import { getPrimes } from "../../../Typescript/typescriptClasses/Algorithms"; -import { Unsolved } from "../Unsolved"; +import { getPrimes } from "../../../Typescript/typescriptClasses/NumberAlgorithms"; import { Problem } from "./Problem"; @@ -62,6 +61,7 @@ export class Problem35 extends Problem{ //Start the timer this.timer.start(); + //Get all primes under 1,000,000 this.primes = getPrimes(Problem35.MAX_NUM); //Go through all primes, get all their rotations, and check if htose numbers are also primes @@ -84,6 +84,7 @@ export class Problem35 extends Problem{ } } + //Stop the timer this.timer.stop(); @@ -99,38 +100,27 @@ export class Problem35 extends Problem{ //Gets //Returns a string with the solution to the problem public getResult(): string{ - //If the problem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("result"); return `The number of all circular prime numbers under ${Problem35.MAX_NUM} is ${this.circularPrimes.length}`; } //Returns the array of primes < MAX_NUM public getPrimes(): number[]{ - //If the problem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("list of primes"); return this.primes; } //Returns the array of circular primes < MAX_NUM public getCircularPrimes(): number[]{ - //If the problem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("list of circular primes"); return this.circularPrimes; } //Returns the number of circular primes < MAX_NUM public getNumCircularPrimes(): number{ - //If the problem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("number oc circular primes"); return this.circularPrimes.length; } } + /* Results: The number of all circular prime numbers under 999999 is 55 It took an average of 7.204 seconds to run this problem through 100 iterations diff --git a/Problems/Problem36.ts b/Problems/Problem36.ts index fd0aa41..dd0388b 100644 --- a/Problems/Problem36.ts +++ b/Problems/Problem36.ts @@ -1,7 +1,7 @@ //ProjectEulerTS/Problems/Problem36.ts //Matthew Ellison // Created: 06-29-21 -//Modified: 06-29-21 +//Modified: 07-14-21 //Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2. //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/typescriptClasses /* @@ -22,8 +22,9 @@ */ -import { getSum, isPalindrome, toBin } from "../../../Typescript/typescriptClasses/Algorithms"; -import { Unsolved } from "../Unsolved"; +import { getSum } from "../../../Typescript/typescriptClasses/ArrayAlgorithms"; +import { toBin } from "../../../Typescript/typescriptClasses/NumberAlgorithms"; +import { isPalindrome } from "../../../Typescript/typescriptClasses/StringAlgorithms"; import { Problem } from "./Problem"; @@ -53,6 +54,7 @@ export class Problem36 extends Problem{ //Start the timer this.timer.start(); + //Start with 1, check if it is a palindrome in base 10 and 2, and continue to MAX_NUM for(let num = 1;num < Problem36.MAX_NUM;++num){ //Check if num is a palindrome @@ -68,6 +70,7 @@ export class Problem36 extends Problem{ //Get the sum of all palindromes in the list this.sum = getSum(this.palindromes); + //Stop the timer this.timer.stop(); @@ -83,30 +86,22 @@ export class Problem36 extends Problem{ //Gets //Returns a string with the solution to the problem public getResult(): string{ - //If the problem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("result"); return `The sum of all base 10 and base 2 palindromic numbers < ${Problem36.MAX_NUM} is ${this.sum}`; } //Return the array of palindromes < MAX_NUM public getPalindromes(){ - //If the porblem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("list of palindromes"); return this.palindromes; } //Returns the sum of all elements in the array of palindromes public getSumOfPalindromes(){ - //If the problem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("sum of all palindromes"); return this.sum; } } + /* Results: The sum of all base 10 and base 2 palindromic numbers < 999999 is 872187 It took an average of 297.957 milliseconds to run this problem through 100 iterations diff --git a/Problems/Problem37.ts b/Problems/Problem37.ts index 4943016..e0a3524 100644 --- a/Problems/Problem37.ts +++ b/Problems/Problem37.ts @@ -1,7 +1,7 @@ //ProjectEulerTS/Problems/Problem37.ts //Matthew Ellison // Created: 07-01-21 -//Modified: 07-01-21 +//Modified: 07-14-21 //Find the sum of the only eleven primes that are both truncatable from left to right and right to left (2, 3, 5, and 7 are not counted). //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/typescriptClasses /* @@ -21,10 +21,13 @@ along with this program. If not, see . */ -import { getSum, isPrime, sieveOfEratosthenes } from "../../../Typescript/typescriptClasses/Algorithms"; + +import { getSum } from "../../../Typescript/typescriptClasses/ArrayAlgorithms"; +import { isPrime, sieveOfEratosthenes } from "../../../Typescript/typescriptClasses/NumberAlgorithms"; import { Unsolved } from "../Unsolved"; import { Problem } from "./Problem"; + export class Problem37 extends Problem{ //Variables //Static variables @@ -51,6 +54,7 @@ export class Problem37 extends Problem{ //Start the timer this.timer.start(); + //Create the sieve and get the first prime number let sieve = sieveOfEratosthenes(); let tempPrime = sieve.next().value; @@ -126,6 +130,7 @@ export class Problem37 extends Problem{ //Get the sum of all elements in the truncPrimes list this.sum = getSum(this.truncPrimes); + //Stop the timer this.timer.stop(); @@ -141,28 +146,22 @@ export class Problem37 extends Problem{ //Gets //Returns a string with the solution to the problem public getResult(): string{ - //If the problem hasn't been solve dthrow an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("result"); return `The sum of all left and right truncatable primes is ${this.sum}`; } //Returns the list of primes that can be truncated public getTruncatablePrimes(): number[]{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("list of truncatable primes"); return this.truncPrimes; } //Returns the sum of all elements in truncPrimes public getSumOfTruncatablePrimes(): number{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("sum of truncatable primes"); return this.sum; } } + /* Results: The sum of all left and right truncatable primes is 748317 It took an average of 104.007 milliseconds to run this problem through 100 iterations diff --git a/Problems/Problem4.ts b/Problems/Problem4.ts index 4188aa6..580e8f5 100644 --- a/Problems/Problem4.ts +++ b/Problems/Problem4.ts @@ -1,11 +1,11 @@ //ProjectEulerTS/Problems/Problem4.ts //Matthew Ellison // Created: 10-24-20 -//Modified: 10-24-20 +//Modified: 07-14-21 //Find the largest palindrome made from the product of two 3-digit numbers //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/typescriptClasses /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -23,7 +23,6 @@ import { Problem } from "./Problem"; -import { Unsolved } from "../Unsolved"; export class Problem4 extends Problem{ @@ -49,6 +48,7 @@ export class Problem4 extends Problem{ //Start the timer this.timer.start(); + //Start at the first 3-digit number and check every one up to the last 3-digit number for(let firstNum: number = Problem4.START_NUM;firstNum <= Problem4.END_NUM;++firstNum){ //You can start at the location of the first number because everything before that has already been tested. (100 * 101 == 101 * 100) @@ -71,6 +71,7 @@ export class Problem4 extends Problem{ //Sort the palindromes so that the last one is the largest this.palindromes = this.palindromes.sort((n1, n2) => n1 - n2); + //Stop the timer this.timer.stop(); @@ -86,27 +87,22 @@ export class Problem4 extends Problem{ //Gets //Returns the result of solving the problem public getResult(): string{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("result"); return `The largest palindrome is ${this.palindromes[this.palindromes.length - 1]}`; } //Returns the list of all palindromes public getPalindromes(): number[]{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("palindromes"); return this.palindromes; } //Returns the largest palindrome public getLargestPalindrome(): number{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("largest palindrome"); return this.palindromes[this.palindromes.length - 1]; } } + /* Results: The largest palindrome is 906609 It took an average of 121.130 milliseconds to run this problem through 100 iterations diff --git a/Problems/Problem5.ts b/Problems/Problem5.ts index 77c2e48..b175ad5 100644 --- a/Problems/Problem5.ts +++ b/Problems/Problem5.ts @@ -1,11 +1,11 @@ //ProjectEulerTS/Problems/Problem5.ts //Matthew Ellison // Created: 10-26-20 -//Modified: 10-26-20 +//Modified: 07-14-21 //What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/typescriptClasses /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -23,7 +23,6 @@ import { Problem } from "./Problem"; -import { Unsolved } from "../Unsolved"; export class Problem5 extends Problem{ @@ -48,6 +47,7 @@ export class Problem5 extends Problem{ //Start the timer this.timer.start(); + //Start at 20 because it must at least be divisible by 20. Increment by 2 because it must be an even number to be divisible by 2 let numFound: boolean = false; //A flag for finding the divisible number let currentNum: number = 20; //The number that it is currently checking against @@ -67,9 +67,10 @@ export class Problem5 extends Problem{ currentNum += 2; } } - + //Save the current number as the smallest this.smallestNum = currentNum + //Stop the timer this.timer.stop(); @@ -84,20 +85,17 @@ export class Problem5 extends Problem{ //Gets //Returns the result of solving the problem public getResult(): string{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("result"); return `The smallest positive number evenly divisible by all number 1-20 is ${this.smallestNum}`; } //Returns the requested number public getNumber(): number{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("number"); return this.smallestNum; } } + /* Results: The smallest positive number evenly divisible by all number 1-20 is 232792560 It took an average of 892.378 milliseconds to run this problem through 100 iterations diff --git a/Problems/Problem6.ts b/Problems/Problem6.ts index 25fbd07..4fc661c 100644 --- a/Problems/Problem6.ts +++ b/Problems/Problem6.ts @@ -1,7 +1,7 @@ //ProjectEulerTS/Problems/Problem6.ts //Matthew Ellison // Created: 03-10-21 -//Modified: 03-10-21 +//Modified: 07-14-21 //Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum. //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/typescriptClasses /* @@ -23,7 +23,6 @@ import { Problem } from "./Problem"; -import { Unsolved } from "../Unsolved"; export class Problem6 extends Problem{ @@ -53,6 +52,7 @@ export class Problem6 extends Problem{ //Start the timer this.timer.start(); + //Run through all numbers and add them to the appropriate sums for(let currentNum: number = Problem6.START_NUM;currentNum <= Problem6.END_NUM;++currentNum){ this.sumOfSquares += (currentNum * currentNum); //Square the number and add it to the variable @@ -61,6 +61,7 @@ export class Problem6 extends Problem{ //Square the sum of all the numbers this.squareOfSum *= this.squareOfSum; + //Stop the timer this.timer.stop(); @@ -76,34 +77,27 @@ export class Problem6 extends Problem{ //Gets //Returns the result of solving the problem public getResult(): string{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("result"); return `The difference between the sum of the squares and the square of the sum of all numbers from ${Problem6.START_NUM}-${Problem6.END_NUM} is ${this.getDifference()}`; } //Returns the sum of all the squares public getSumOfSquares(): number{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("sum of the squares"); return this.sumOfSquares; } //Returns the square of all the sums public getSquareOfSum(){ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("square of the sums"); return this.squareOfSum; } //Returns the requested difference public getDifference(): number{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("difference between the two numbers"); return Math.abs(this.sumOfSquares - this.squareOfSum); } } + /* Results: The difference between the sum of the squares and the square of the sum of all numbers from 1-100 is 25164150 It took an average of 344.001 nanoseconds to run this problem through 100 iterations diff --git a/Problems/Problem67.ts b/Problems/Problem67.ts index b1364c1..cf31ac0 100644 --- a/Problems/Problem67.ts +++ b/Problems/Problem67.ts @@ -1,7 +1,7 @@ //ProjectEulerTS/Problems/Problem67.ts //Matthew Ellison // Created: 05-28-21 -//Modified: 05-28-21 +//Modified: 07-14-21 //Find the maximum total from top to bottom /* 59 @@ -233,6 +233,7 @@ export class Problem67 extends Problem18{ [23, 33, 44, 81, 80, 92, 93, 75, 94, 88, 23, 61, 39, 76, 22, 3, 28, 94, 32, 6, 49, 65, 41, 34, 18, 23, 8, 47, 62, 60, 3, 63, 33, 13, 80, 52, 31, 54, 73, 43, 70, 26, 16, 69, 57, 87, 83, 31, 3, 93, 70, 81, 47, 95, 77, 44, 29, 68, 39, 51, 56, 59, 63, 7, 25, 70, 7, 77, 43, 53, 64, 3, 94, 42, 95, 39, 18, 1, 66, 21, 16, 97, 20, 50, 90, 16, 70, 10, 95, 69, 29, 6, 25, 61, 41, 26, 15, 59, 63, 35]] } + /* Results: The value of the longest path is 7273 It took an average of 413.002 milliseconds to run this problem through 100 iterations diff --git a/Problems/Problem7.ts b/Problems/Problem7.ts index b65433e..359656c 100644 --- a/Problems/Problem7.ts +++ b/Problems/Problem7.ts @@ -1,7 +1,7 @@ //ProjectEulerTS/Problems/Problem7.ts //Matthew Ellison // Created: 03-10-21 -//Modified: 03-10-21 +//Modified: 07-14-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 /* @@ -23,8 +23,7 @@ import { Problem } from "./Problem"; -import { Unsolved } from "../Unsolved"; -import { getNumPrimes } from "../../../Typescript/typescriptClasses/Algorithms"; +import { getNumPrimes } from "../../../Typescript/typescriptClasses/NumberAlgorithms"; export class Problem7 extends Problem{ @@ -50,9 +49,11 @@ export class Problem7 extends Problem{ //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(); @@ -67,21 +68,17 @@ export class Problem7 extends Problem{ //Gets //Returns the result of solving the problem public getResult(): string{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("result"); 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(); - } + this.solvedCheck("prime"); 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 diff --git a/Problems/Problem8.ts b/Problems/Problem8.ts index 4f4e3e3..7c771b3 100644 --- a/Problems/Problem8.ts +++ b/Problems/Problem8.ts @@ -1,7 +1,7 @@ //ProjectEulerTS/Problems/Problem8.ts //Matthew Ellison // Created: 03-10-21 -//Modified: 03-10-21 +//Modified: 07-14-21 //Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product? /* 73167176531330624919225119674426574742355349194934 @@ -45,7 +45,6 @@ import { Problem } from "./Problem"; -import { Unsolved } from "../Unsolved"; export class Problem8 extends Problem{ @@ -74,6 +73,7 @@ export class Problem8 extends Problem{ //Start the timer this.timer.start(); + //Cycle through the string of numbers looking for the maximum product for(let cnt: number = 12;cnt < Problem8.NUMBER.length;++cnt){ let currentProduct: number = parseInt(Problem8.NUMBER.substring(cnt - 12, cnt - 11)) * parseInt(Problem8.NUMBER.substring(cnt - 11, cnt - 10)) * parseInt(Problem8.NUMBER.substring(cnt - 10, cnt - 9)) * parseInt(Problem8.NUMBER.substring(cnt - 9, cnt - 8)) * parseInt(Problem8.NUMBER.substring(cnt - 8, cnt - 7)) * parseInt(Problem8.NUMBER.substring(cnt - 7, cnt - 6)) * parseInt(Problem8.NUMBER.substring(cnt - 6, cnt - 5)) * parseInt(Problem8.NUMBER.substring(cnt - 5, cnt - 4)) * parseInt(Problem8.NUMBER.substring(cnt - 4, cnt - 3)) * parseInt(Problem8.NUMBER.substring(cnt - 3, cnt - 2)) * parseInt(Problem8.NUMBER.substring(cnt - 2, cnt - 1)) * parseInt(Problem8.NUMBER.substring(cnt - 1, cnt)) * parseInt(Problem8.NUMBER.substring(cnt, cnt + 1)); @@ -85,6 +85,7 @@ export class Problem8 extends Problem{ } } + //Stop the timer this.timer.stop(); @@ -98,29 +99,22 @@ export class Problem8 extends Problem{ //Gets //Returns the result of solving the problem public getResult(): string{ - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("result"); return `The greatest product is ${this.maxProduct}\nThe numbers are ${this.maxNums}`; } //Returns the string of numbers that produces the largest product public getLargestNums(): string{ - //If the problem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("numbers that make the largest product"); return this.maxNums; } //Returns the requested product public getLargestProduct(): number{ - //If the problem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("product of the numbers"); return this.maxProduct; } } + /* Results: The greatest product is 23514624000 The numbers are 5576689664895 diff --git a/Problems/Problem9.ts b/Problems/Problem9.ts index f5ebd37..7a99804 100644 --- a/Problems/Problem9.ts +++ b/Problems/Problem9.ts @@ -1,7 +1,7 @@ //ProjectEulerTS/Problems/Problem9.ts //Matthew Ellison // Created: 03-24-21 -//Modified: 03-24-21 +//Modified: 07-14-21 //There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc. //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/typescriptClasses /* @@ -22,8 +22,8 @@ */ -import { Problem } from "./Problem"; import { Unsolved } from "../Unsolved"; +import { Problem } from "./Problem"; export class Problem9 extends Problem{ @@ -56,6 +56,7 @@ export class Problem9 extends Problem{ //Start the timer this.timer.start(); + //Loop through all possible a's while((this.a < Problem9.GOAL_SUM) && (!this.found)){ this.b = this.a + 1; //b must be larger than a @@ -76,6 +77,7 @@ export class Problem9 extends Problem{ } } + //Stop the timer this.timer.stop(); @@ -98,46 +100,32 @@ export class Problem9 extends Problem{ //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(); - } + this.solvedCheck("result"); return `The Pythagorean triplet is ${this.a} + ${this.b} + ${this.c}\nThe numbers' product is ${this.getProduct()}`; } //Returns the length of the first side public getSideA(): number{ - //If the problem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("first side"); return this.a; } //Returns the length of the second side public getSideB(): number{ - //If the problem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("second side"); return this.b; } //Returns the length of the hyp public getSideC(): number{ - //If the problem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("third side"); return this.c; } //Returns the product of the 3 sides public getProduct(): number{ - //If the problem hasn't been solved throw an exception - if(!this.solved){ - throw new Unsolved(); - } + this.solvedCheck("product of all three sides"); return this.a * this.b * this.c; } } + /* Results: The Pythagorean triplet is 200 + 375 + 425 The numbers' product is 31875000