Updated to use new library layout

This commit is contained in:
2021-07-14 15:38:17 -04:00
parent ad2e948a42
commit b5c1df010f
39 changed files with 316 additions and 493 deletions

View File

@@ -1,11 +1,11 @@
//ProjectEulerTS/Problems/Problem.ts //ProjectEulerTS/Problems/Problem.ts
//Matthew Ellison //Matthew Ellison
// Created: 10-18-20 // 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 //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 //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 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 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 <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import { Stopwatch } from "../../../Typescript/typescriptClasses/Stopwatch"; import { Stopwatch } from "../../../Typescript/typescriptClasses/Stopwatch";
import { Unsolved } from "../Unsolved"; import { Unsolved } from "../Unsolved";
@@ -36,24 +37,26 @@ export abstract class Problem{
this.description = description; this.description = description;
this.solved = false; 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 //Gets
getDescription(): string{ public getDescription(): string{
return this.description; return this.description;
} }
//Returns the result of solving the problem //Returns the result of solving the problem
public abstract getResult(): string; public abstract getResult(): string;
//Returns the time taken to run the problem as a string //Returns the time taken to run the problem as a string
public getTime(): string{ public getTime(): string{
if(!this.solved){ this.solvedCheck("time it took to run the algorithm");
throw new Unsolved();
}
return this.timer.getStr(); return this.timer.getStr();
} }
//Returns the timer as a stopwatch //Returns the timer as a stopwatch
public getTimer(): Stopwatch{ public getTimer(): Stopwatch{
if(!this.solved){ this.solvedCheck("timer");
throw new Unsolved();
}
return this.timer; return this.timer;
} }
//Solve the problem //Solve the problem
@@ -63,4 +66,4 @@ export abstract class Problem{
this.timer.reset(); this.timer.reset();
this.solved = false; this.solved = false;
} }
} }

View File

@@ -1,11 +1,11 @@
//ProjectEulerTS/Problems/Problem1.ts //ProjectEulerTS/Problems/Problem1.ts
//Matthew Ellison //Matthew Ellison
// Created: 10-18-20 // 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 //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 //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 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 it under the terms of the GNU Lesser General Public License as published by
@@ -25,6 +25,7 @@
import { Unsolved } from "../Unsolved"; import { Unsolved } from "../Unsolved";
import {Problem} from "./Problem"; import {Problem} from "./Problem";
export class Problem1 extends Problem{ export class Problem1 extends Problem{
//Variables //Variables
//Static variables //Static variables
@@ -48,9 +49,11 @@ export class Problem1 extends Problem{
//Start the timer //Start the timer
this.timer.start(); this.timer.start();
//Get the sum of the progressions of 3 and 5 and remove the sum of progressions of the overlap //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); this.fullSum = this.sumOfProgression(3) + this.sumOfProgression(5) - this.sumOfProgression(3 * 5);
//Stop the timer //Stop the timer
this.timer.stop(); this.timer.stop();
@@ -71,20 +74,17 @@ export class Problem1 extends Problem{
//Gets //Gets
//Returns the result of solving the problem //Returns the result of solving the problem
public getResult(): string{ public getResult(): string{
if(!this.solved){ this.solvedCheck("result");
throw new Unsolved();
}
return `The sum of all numbers < ${Problem1.TOP_NUM + 1} is ${this.fullSum}`; return `The sum of all numbers < ${Problem1.TOP_NUM + 1} is ${this.fullSum}`;
} }
//Returns the requested sum //Returns the requested sum
public getSum(): number{ public getSum(): number{
if(!this.solved){ this.solvedCheck("sum");
throw new Unsolved();
}
return this.fullSum; return this.fullSum;
} }
} }
/* Results: /* Results:
The sum of all numbers < 1000 is 233168 The sum of all numbers < 1000 is 233168
It took an average of 1.921 microseconds to run this problem through 100 iterations It took an average of 1.921 microseconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
//ProjectEulerTS/Problems/Problem10.ts //ProjectEulerTS/Problems/Problem10.ts
//Matthew Ellison //Matthew Ellison
// Created: 03-24-21 // Created: 03-24-21
//Modified: 03-24-21 //Modified: 07-14-21
//Find the sum of all the primes below two million //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 //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 { Problem } from "./Problem";
import { Unsolved } from "../Unsolved"; import { getSum } from "../../../Typescript/typescriptClasses/ArrayAlgorithms";
import { getPrimes, getSum } from "../../../Typescript/typescriptClasses/Algorithms"; import { getPrimes } from "../../../Typescript/typescriptClasses/NumberAlgorithms";
export class Problem10 extends Problem{ export class Problem10 extends Problem{
@@ -51,9 +51,11 @@ export class Problem10 extends Problem{
//Start the timer //Start the timer
this.timer.start(); this.timer.start();
//Get the sum of all prime numbers < GOAL_NUMBER //Get the sum of all prime numbers < GOAL_NUMBER
this.sum = getSum(getPrimes(Problem10.GOAL_NUMBER)); this.sum = getSum(getPrimes(Problem10.GOAL_NUMBER));
//Stop the timer //Stop the timer
this.timer.stop(); this.timer.stop();
@@ -68,22 +70,17 @@ export class Problem10 extends Problem{
//Gets //Gets
//Returns the result of solving the problem //Returns the result of solving the problem
public getResult(): string{ public getResult(): string{
//If the problem hasn't been solved throw an exception this.solvedCheck("result");
if(!this.solved){
throw new Unsolved();
}
return `The sum of all the primes < ${Problem10.GOAL_NUMBER + 1} is ${this.sum}`; return `The sum of all the primes < ${Problem10.GOAL_NUMBER + 1} is ${this.sum}`;
} }
//Returns the sum that was requested //Returns the sum that was requested
public getSum(): number{ public getSum(): number{
//If the problem hasn't been solved throw an exception this.solvedCheck("sum");
if(!this.solved){
throw new Unsolved();
}
return this.sum; return this.sum;
} }
} }
/* Results: /* Results:
The sum of all the primes < 2000000 is 142913828922 The sum of all the primes < 2000000 is 142913828922
It took an average of 170.840 milliseconds to run this problem through 100 iterations It took an average of 170.840 milliseconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
//ProjectEulerTS/Problems/Problem11.ts //ProjectEulerTS/Problems/Problem11.ts
//Matthew Ellison //Matthew Ellison
// Created: 03-24-21 // Created: 03-24-21
//Modified: 03-24-21 //Modified: 07-14-21
//Find the sum of all the primes below two million //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 //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 { Problem } from "./Problem";
import { Unsolved } from "../Unsolved"; import { getProd } from "../../../Typescript/typescriptClasses/ArrayAlgorithms";
import { getProd } from "../../../Typescript/typescriptClasses/Algorithms";
export class Problem11 extends Problem{ export class Problem11 extends Problem{
@@ -80,6 +79,7 @@ export class Problem11 extends Problem{
//Start the timer //Start the timer
this.timer.start(); this.timer.start();
//Loop through every row and column //Loop through every row and column
for(let row = 0;row < Problem11.grid.length;++row){ for(let row = 0;row < Problem11.grid.length;++row){
for(let col = 0;col < Problem11.grid[row].length;++col){ for(let col = 0;col < Problem11.grid[row].length;++col){
@@ -155,6 +155,7 @@ export class Problem11 extends Problem{
} }
} }
//Stop the timer //Stop the timer
this.timer.stop(); this.timer.stop();
@@ -169,30 +170,22 @@ export class Problem11 extends Problem{
//Gets //Gets
//Returns the result of solving the problem //Returns the result of solving the problem
public getResult(): string{ public getResult(): string{
//If the problem hasn't been solved throw an exception this.solvedCheck("result");
if(!this.solved){
throw new Unsolved();
}
return `The greatest product of 4 numbers in a line is ${getProd(this.greatestProduct)}\nThe numbers are ${this.greatestProduct}`; 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 //Returns the numbers that were being searched
public getNumbers(): number[]{ public getNumbers(): number[]{
//If the problem hasn't been solved throw an exception this.solvedCheck("numbers");
if(!this.solved){
throw new Unsolved();
}
return this.greatestProduct; return this.greatestProduct;
} }
//Returns teh product that was requested //Returns teh product that was requested
public getProduct(): number{ public getProduct(): number{
//If the problem hasn't been solved throw an exception this.solvedCheck("product of the numbers");
if(!this.solved){
throw new Unsolved();
}
return getProd(this.greatestProduct); return getProd(this.greatestProduct);
} }
} }
/* Results: /* Results:
The greatest product of 4 numbers in a line is 70600674 The greatest product of 4 numbers in a line is 70600674
The numbers are 89,94,97,87 The numbers are 89,94,97,87

View File

@@ -1,7 +1,7 @@
//ProjectEulerTS/Problems/Problem12.ts //ProjectEulerTS/Problems/Problem12.ts
//Matthew Ellison //Matthew Ellison
// Created: 03-26-21 // 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? //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 //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 { Problem } from "./Problem";
import { Unsolved } from "../Unsolved"; import { getDivisors } from "../../../Typescript/typescriptClasses/NumberAlgorithms";
import { getDivisors } from "../../../Typescript/typescriptClasses/Algorithms";
export class Problem12 extends Problem{ export class Problem12 extends Problem{
@@ -58,6 +57,7 @@ export class Problem12 extends Problem{
//Start the timer //Start the timer
this.timer.start(); this.timer.start();
//Loop until you find the appropriate number //Loop until you find the appropriate number
while((!foundNumber) && (this.sum > 0)){ while((!foundNumber) && (this.sum > 0)){
this.divisors = getDivisors(this.sum); this.divisors = getDivisors(this.sum);
@@ -72,6 +72,7 @@ export class Problem12 extends Problem{
} }
} }
//Stop the timer //Stop the timer
this.timer.stop(); this.timer.stop();
@@ -88,46 +89,32 @@ export class Problem12 extends Problem{
//Gets //Gets
//Returns the result of solving the problem //Returns the result of solving the problem
public getResult(): string{ public getResult(): string{
//If the problem hasn't bee solved throw an exception this.solvedCheck("result");
if(!this.solved){
throw new Unsolved();
}
return `The triangular number ${this.sum} is the sum of all numbers >= ${this.counter - 1} and has ${this.divisors.length} divisors`; 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 //Returns the triangular number
public getTriangularNumber(): number{ public getTriangularNumber(): number{
//If the problem hasn't bee solved throw an exception this.solvedCheck("triangular number");
if(!this.solved){
throw new Unsolved();
}
return this.sum; return this.sum;
} }
//Get the final number that was added to the triangular number //Get the final number that was added to the triangular number
public getLastNumberAdded(): number{ public getLastNumberAdded(): number{
//If the problem hasn't bee solved throw an exception this.solvedCheck("last number added to get the triangular number");
if(!this.solved){
throw new Unsolved();
}
return this.counter - 1; return this.counter - 1;
} }
//Returns the list of divisors of the requested number //Returns the list of divisors of the requested number
public getDivisorsOfTriangularNumber(): number[]{ public getDivisorsOfTriangularNumber(): number[]{
//If the problem hasn't bee solved throw an exception this.solvedCheck("divisors of the triangular number");
if(!this.solved){
throw new Unsolved();
}
return this.divisors; return this.divisors;
} }
//Returns the number of divisors of the requested number //Returns the number of divisors of the requested number
public getNumberOfDivisors(): number{ public getNumberOfDivisors(): number{
//If the problem hasn't bee solved throw an exception this.solvedCheck("number of divisors of the triangular number");
if(!this.solved){
throw new Unsolved();
}
return this.divisors.length; return this.divisors.length;
} }
} }
/* Results /* Results
The triangular number 76576500 is the sum of all numbers >= 12375 and has 576 divisors 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 It took an average of 301.951 milliseconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
//ProjectEulerTS/Problems/Problem13.ts //ProjectEulerTS/Problems/Problem13.ts
//Matthew Ellison //Matthew Ellison
// Created: 03-26-21 // 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 //Work out the first ten digits of the sum of the following one-hundred 50-digit numbers
/* /*
37107287533902102798797998220837590246510135740250 37107287533902102798797998220837590246510135740250
@@ -125,8 +125,7 @@
import { Problem } from "./Problem"; import { Problem } from "./Problem";
import { Unsolved } from "../Unsolved"; import { getSumBig } from "../../../Typescript/typescriptClasses/ArrayAlgorithms";
import { getSumBig } from "../../../Typescript/typescriptClasses/Algorithms";
export class Problem13 extends Problem{ export class Problem13 extends Problem{
@@ -252,9 +251,11 @@ export class Problem13 extends Problem{
//Start the timer //Start the timer
this.timer.start(); this.timer.start();
//Get the sum of all the numbers //Get the sum of all the numbers
this.sum = getSumBig(Problem13.nums); this.sum = getSumBig(Problem13.nums);
//Stop the timer //Stop the timer
this.timer.stop(); this.timer.stop();
@@ -269,30 +270,22 @@ export class Problem13 extends Problem{
//Gets //Gets
//Returns the result of solving the problem //Returns the result of solving the problem
public getResult(): string{ public getResult(): string{
//If the problem hasn't been solved throw an exception this.solvedCheck("result");
if(!this.solved){
throw new Unsolved();
}
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)}`; 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 //Returns the list of 50-digit numbers
public getNumbers(): bigint[]{ public getNumbers(): bigint[]{
//If the problem hasn't been solved throw an exception this.solvedCheck("numbers");
if(!this.solved){
throw new Unsolved();
}
return Problem13.nums; return Problem13.nums;
} }
//Returns the sum of the 50-digit numbers //Returns the sum of the 50-digit numbers
public getSum(): bigint{ public getSum(): bigint{
//If the problem hasn't been solved throw an exception this.solvedCheck("sum");
if(!this.solved){
throw new Unsolved();
}
return this.sum; return this.sum;
} }
} }
/* Results: /* Results:
The sum of all 100 numbers is 5537376230390876637302048746832985971773659831892672 The sum of all 100 numbers is 5537376230390876637302048746832985971773659831892672
The first 10 digits of the sum of the numbers is 5537376230 The first 10 digits of the sum of the numbers is 5537376230

View File

@@ -1,7 +1,7 @@
//ProjectEulerTS/Problems/Problem14.ts //ProjectEulerTS/Problems/Problem14.ts
//Matthew Ellison //Matthew Ellison
// Created: 03-26-21 // Created: 03-26-21
//Modified: 03-26-21 //Modified: 07-14-21
/* /*
The following iterative sequence is defined for the set of positive integers: The following iterative sequence is defined for the set of positive integers:
n → n/2 (n is even) 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"; import { Problem } from "./Problem";
@@ -57,6 +56,7 @@ export class Problem14 extends Problem{
//Start the timer //Start the timer
this.timer.start(); this.timer.start();
//Loop through all numbers <= MAX_NUM and check them against the series //Loop through all numbers <= MAX_NUM and check them against the series
for(let currentNum = 1;currentNum <= Problem14.MAX_NUM;++currentNum){ for(let currentNum = 1;currentNum <= Problem14.MAX_NUM;++currentNum){
let currentLength: number = this.checkSeries(currentNum); let currentLength: number = this.checkSeries(currentNum);
@@ -67,6 +67,7 @@ export class Problem14 extends Problem{
} }
} }
//Stop the timer //Stop the timer
this.timer.stop(); this.timer.stop();
@@ -99,30 +100,22 @@ export class Problem14 extends Problem{
} }
//Returns the result of solving the problem //Returns the result of solving the problem
public getResult(): string{ public getResult(): string{
//If the problem hasn't been solved throw an exception this.solvedCheck("result");
if(!this.solved){
throw new Unsolved();
}
return `The number ${this.maxNum} produced a chain of ${this.maxLength} steps`; return `The number ${this.maxNum} produced a chain of ${this.maxLength} steps`;
} }
//Returns the length of the requested chain //Returns the length of the requested chain
public getLength(): number{ public getLength(): number{
//If the problem hasn't been solved throw an exception this.solvedCheck("length of the longest chain");
if(!this.solved){
throw new Unsolved();
}
return this.maxLength; return this.maxLength;
} }
//Returns the starting number of the requested chain //Returns the starting number of the requested chain
public getStartingNumber(): number{ public getStartingNumber(): number{
//If the problem hasn't been solved throw an exception this.solvedCheck("starting number of the longest chain");
if(!this.solved){
throw new Unsolved();
}
return this.maxNum; return this.maxNum;
} }
} }
/* Results: /* Results:
The number 837799 produced a chain of 525 steps The number 837799 produced a chain of 525 steps
It took an average of 1.178 seconds to run this problem through 100 iterations It took an average of 1.178 seconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
//ProjectEulerTS/Problems/Problem15.ts //ProjectEulerTS/Problems/Problem15.ts
//Matthew Ellison //Matthew Ellison
// Created: 03-29-21 // 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? //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 //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"; import { Problem } from "./Problem";
@@ -51,10 +50,12 @@ export class Problem15 extends Problem{
//Start the timer //Start the timer
this.timer.start(); this.timer.start();
//We write this as a recursive function //We write this as a recursive function
//When in a location it always move right first, then down //When in a location it always move right first, then down
this.move(0, 0); this.move(0, 0);
//Stop the timer //Stop the timer
this.timer.stop(); this.timer.stop();
@@ -88,20 +89,17 @@ export class Problem15 extends Problem{
//Gets //Gets
//Returns the result of solving the problem //Returns the result of solving the problem
public getResult(): string{ public getResult(): string{
if(!this.solved){ this.solvedCheck("result");
throw new Unsolved();
}
return `The number of routes is ${this.numOfRoutes}`; return `The number of routes is ${this.numOfRoutes}`;
} }
//Returns the number of routes found //Returns the number of routes found
public getNumberOfRoutes(): number{ public getNumberOfRoutes(): number{
if(!this.solved){ this.solvedCheck("number of routes");
throw new Unsolved();
}
return this.numOfRoutes; return this.numOfRoutes;
} }
} }
/* Results: /* Results:
The number of routes is 137846528820 The number of routes is 137846528820
It took 37.058 minutes to solve this problem. It took 37.058 minutes to solve this problem.

View File

@@ -1,7 +1,7 @@
//ProjectEulerTS/Problems/Problem16.ts //ProjectEulerTS/Problems/Problem16.ts
//Matthew Ellison //Matthew Ellison
// Created: 03-29-21 // Created: 03-29-21
//Modified: 03-29-21 //Modified: 07-14-21
//What is the sum of the digits of the number 2^1000? //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 //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"; import { Problem } from "./Problem";
@@ -53,6 +52,7 @@ export class Problem16 extends Problem{
//Start the timer //Start the timer
this.timer.start(); this.timer.start();
//Get the number //Get the number
this.num = Problem16.NUM_TO_POWER ** Problem16.POWER; this.num = Problem16.NUM_TO_POWER ** Problem16.POWER;
@@ -64,6 +64,7 @@ export class Problem16 extends Problem{
this.sumOfElements += parseInt(numString.charAt(cnt)); this.sumOfElements += parseInt(numString.charAt(cnt));
} }
//Stop the timer //Stop the timer
this.timer.stop(); this.timer.stop();
@@ -79,27 +80,22 @@ export class Problem16 extends Problem{
//Gets //Gets
//Returns the result of solving the problem //Returns the result of solving the problem
public getResult(): string{ public getResult(): string{
if(!this.solved){ this.solvedCheck("result");
throw new Unsolved();
}
return `${Problem16.NUM_TO_POWER}^${Problem16.POWER} = ${this.num}\nThe sum of the elements is ${this.sumOfElements}`; return `${Problem16.NUM_TO_POWER}^${Problem16.POWER} = ${this.num}\nThe sum of the elements is ${this.sumOfElements}`;
} }
//Returns the number that was calculated //Returns the number that was calculated
public getNumber(): bigint{ public getNumber(): bigint{
if(!this.solved){ this.solvedCheck("number");
throw new Unsolved();
}
return this.num; return this.num;
} }
//Returns the sum o the digits of the number //Returns the sum o the digits of the number
public getSum(): number{ public getSum(): number{
if(!this.solved){ this.solvedCheck("sum");
throw new Unsolved();
}
return this.sumOfElements; return this.sumOfElements;
} }
} }
/* Results: /* Results:
2^1000 = 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376 2^1000 = 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376
The sum of the elements is 1366 The sum of the elements is 1366

View File

@@ -1,7 +1,7 @@
//ProjectEulerTS/Problems/Problem17.ts //ProjectEulerTS/Problems/Problem17.ts
//Matthew Ellison //Matthew Ellison
// Created: 03-29-21 // 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? //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 //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 //Start the timer
this.timer.start(); this.timer.start();
//Start with 1 and increment //Start with 1 and increment
for(let num: number = Problem17.START_NUM;num <= Problem17.STOP_NUM;++num){ 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 //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); this.letterCount += this.getNumberChars(currentNumString);
} }
//Stop the timer //Stop the timer
this.timer.stop(); this.timer.stop();
@@ -185,20 +187,17 @@ export class Problem17 extends Problem{
//Gets //Gets
//Returns the result of solving the problem //Returns the result of solving the problem
public getResult(): string{ public getResult(): string{
if(!this.solved){ this.solvedCheck("result");
throw new Unsolved();
}
return `The sum of all the letters in all the numbers ${Problem17.START_NUM}-${Problem17.STOP_NUM} is ${this.letterCount}`; 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 //Returns the number of letters asked for
public getLetterCount(): number{ public getLetterCount(): number{
if(!this.solved){ this.solvedCheck("letter count");
throw new Unsolved();
}
return this.letterCount; return this.letterCount;
} }
} }
/* Results: /* Results:
The sum of all the letters in all the numbers 1-1000 is 21124 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 It took an average of 485.744 microseconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
//ProjectEulerTS/Problems/Problem18.ts //ProjectEulerTS/Problems/Problem18.ts
//Matthew Ellison //Matthew Ellison
// Created: 04-05-21 // Created: 04-05-21
//Modified: 04-05-21 //Modified: 07-14-21
//Find the maximum total from top to bottom //Find the maximum total from top to bottom
/* /*
75 75
@@ -40,7 +40,6 @@
*/ */
import { Unsolved } from "../Unsolved";
import { Problem } from "./Problem"; import { Problem } from "./Problem";
@@ -103,6 +102,7 @@ export class Problem18 extends Problem{
//Start the timer //Start the timer
this.timer.start(); this.timer.start();
//Invert the list //Invert the list
this.invert(workingList); 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 //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); this.actualTotal = ((100 * workingList.length) - this.foundPoints[this.foundPoints.length - 1].total);
//Stop the timer //Stop the timer
this.timer.stop(); this.timer.stop();
@@ -170,25 +171,23 @@ export class Problem18 extends Problem{
//Gets //Gets
//Returns the result of solving the problem //Returns the result of solving the problem
public getResult(): string{ public getResult(): string{
if(!this.solved){ this.solvedCheck("result");
throw new Unsolved();
}
return `The value of the longest path is ${this.actualTotal}`; return `The value of the longest path is ${this.actualTotal}`;
} }
//Returns the pyramid that was traversed as a string //Returns the pyramid that was traversed as a string
public getPyramid(): string{ public getPyramid(): string{
this.solvedCheck("pyramid of numbers");
return Problem18.list.toString(); return Problem18.list.toString();
} }
//Returns the trail the algorithm took as a string //Returns the trail the algorithm took as a string
public getTrail(): string{ public getTrail(): string{
this.solvedCheck("trail of the shortest path");
//TODO: //TODO:
return ""; return "";
} }
//Returns the total that was asked for //Returns the total that was asked for
public getTotal(): number{ public getTotal(): number{
if(!this.solved){ this.solvedCheck("total");
throw new Unsolved();
}
return this.actualTotal; return this.actualTotal;
} }
} }
@@ -208,6 +207,7 @@ class location{
} }
} }
/* Results: /* Results:
The value of the longest path is 1074 The value of the longest path is 1074
It took an average of 134.407 microseconds to run this problem through 100 iterations It took an average of 134.407 microseconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
//ProjectEulerTS/Problems/Problem18.ts //ProjectEulerTS/Problems/Problem18.ts
//Matthew Ellison //Matthew Ellison
// Created: 04-06-21 // 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)? //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. 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"; import { Problem } from "./Problem";
@@ -66,6 +65,7 @@ export class Problem19 extends Problem{
//Start the timer //Start the timer
this.timer.start(); this.timer.start();
//Run for all years 1901-2000 //Run for all years 1901-2000
for(let year = Problem19.START_YEAR;year <= Problem19.END_YEAR;++year){ for(let year = Problem19.START_YEAR;year <= Problem19.END_YEAR;++year){
//Run for all months in the year //Run for all months in the year
@@ -80,6 +80,7 @@ export class Problem19 extends Problem{
} }
} }
//Stop the timer //Stop the timer
this.timer.stop(); this.timer.stop();
@@ -194,20 +195,17 @@ export class Problem19 extends Problem{
//Gets //Gets
//Returns the result of solving the problem //Returns the result of solving the problem
public getResult(): string{ public getResult(): string{
if(!this.solved){ this.solvedCheck("result");
throw new Unsolved();
}
return `There are ${this.totalSundays} Sundays that landed on the first of the months from ${Problem19.START_YEAR} to ${Problem19.END_YEAR}`; 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 //Returns the total sundays that were asked for
public getTotalSundays(): number{ public getTotalSundays(): number{
if(!this.solved){ this.solvedCheck("total number of sundays");
throw new Unsolved();
}
return this.totalSundays; return this.totalSundays;
} }
} }
/* Results: /* Results:
There are 171 Sundays that landed on the first of the months from 1901 to 2000 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 It took an average of 4.314 milliseconds to run this problem through 100 iterations

View File

@@ -1,11 +1,11 @@
//ProjectEulerTS/Problems/Problem2.ts //ProjectEulerTS/Problems/Problem2.ts
//Matthew Ellison //Matthew Ellison
// Created: 10-19-20 // Created: 10-19-20
//Modified: 10-19-20 //Modified: 07-14-21
//The sum of the even Fibonacci numbers less than 4,000,000 //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 //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 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 it under the terms of the GNU Lesser General Public License as published by
@@ -23,8 +23,7 @@
import { Problem } from "./Problem"; import { Problem } from "./Problem";
import { getAllFib } from "../../../Typescript/typescriptClasses/Algorithms"; import { getAllFib } from "../../../Typescript/typescriptClasses/NumberAlgorithms";
import { Unsolved } from "../Unsolved";
export class Problem2 extends Problem{ export class Problem2 extends Problem{
@@ -51,6 +50,7 @@ export class Problem2 extends Problem{
//Start the timer //Start the timer
this.timer.start(); this.timer.start();
//Get a list of all fibonacci numbers <= TOP_NUM //Get a list of all fibonacci numbers <= TOP_NUM
let fibNums = getAllFib(Problem2.TOP_NUM); let fibNums = getAllFib(Problem2.TOP_NUM);
//Setp through every element in the list checking ifit is even //Setp through every element in the list checking ifit is even
@@ -61,6 +61,7 @@ export class Problem2 extends Problem{
} }
}); });
//Stop the timer //Stop the timer
this.timer.stop(); this.timer.stop();
@@ -75,20 +76,17 @@ export class Problem2 extends Problem{
//Gets //Gets
//Returns the result of solving the problem //Returns the result of solving the problem
public getResult(): string{ public getResult(): string{
if(!this.solved){ this.solvedCheck("result");
throw new Unsolved();
}
return `The sum of all even fibonacci numbers <= ${Problem2.TOP_NUM} is ${this.fullSum}`; return `The sum of all even fibonacci numbers <= ${Problem2.TOP_NUM} is ${this.fullSum}`;
} }
//Returns the requested sum //Returns the requested sum
public getSum(): number{ public getSum(): number{
if(!this.solved){ this.solvedCheck("sum");
throw new Unsolved();
}
return this.fullSum; return this.fullSum;
} }
} }
/* Results: /* Results:
The sum of all even fibonacci numbers <= 3999999 is 4613732 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 It took an average of 6.330 microseconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
//ProjectEulerTS/Problems/Problem20.ts //ProjectEulerTS/Problems/Problem20.ts
//Matthew Ellison //Matthew Ellison
// Created: 04-07-21 // Created: 04-07-21
//Modified: 04-07-21 //Modified: 07-14-21
//What is the sum of the digits of 100!? //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 //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"; import { Problem } from "./Problem";
@@ -52,6 +51,7 @@ export class Problem20 extends Problem{
//Start the timer //Start the timer
this.timer.start(); this.timer.start();
//Run through every number from 1 to 100 and multiply it by the current num to generate 100! //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){ for(let cnt = Problem20.TOP_NUM;cnt > 1;--cnt){
this.num *= BigInt(cnt); this.num *= BigInt(cnt);
@@ -65,6 +65,7 @@ export class Problem20 extends Problem{
this.sum += parseInt(digit); this.sum += parseInt(digit);
} }
//Stop the timer //Stop the timer
this.timer.stop(); this.timer.stop();
@@ -80,37 +81,27 @@ export class Problem20 extends Problem{
//Gets //Gets
//Returns the result of solving the problem //Returns the result of solving the problem
public getResult(): string{ public getResult(): string{
if(!this.solved){ this.solvedCheck("result");
throw new Unsolved();
}
return `${Problem20.TOP_NUM}! = ${this.num}\nThe sum of the digits is: ${this.sum}`; return `${Problem20.TOP_NUM}! = ${this.num}\nThe sum of the digits is: ${this.sum}`;
} }
//Returns the number 100! //Returns the number 100!
public getNumber(): bigint{ public getNumber(): bigint{
//If the problem hasn't been solved throw an exception this.solvedCheck("number");
if(!this.solved){
throw new Unsolved();
}
return this.num; return this.num;
} }
//Returns the number 100! in a string //Returns the number 100! in a string
public getNumberString(): string{ public getNumberString(): string{
//If the problem hasn't been solved throw an exception this.solvedCheck("number as a string");
if(!this.solved){
throw new Unsolved();
}
return this.num.toString(); return this.num.toString();
} }
//Returns the sum of the digits of 100! //Returns the sum of the digits of 100!
public getSum(): number{ public getSum(): number{
//If the porblem hasn't been solved throw an exception this.solvedCheck("sum of the digits");
if(!this.solved){
throw new Unsolved();
}
return this.sum; return this.sum;
} }
} }
/* Results: /* Results:
100! = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 100! = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
The sum of the digits is: 648 The sum of the digits is: 648

View File

@@ -1,7 +1,7 @@
//ProjectEulerTS/Problems/Problem21.ts //ProjectEulerTS/Problems/Problem21.ts
//Matthew Ellison //Matthew Ellison
// Created: 04-08-21 // Created: 04-08-21
//Modified: 04-08-21 //Modified: 07-14-21
//Evaluate the sum of all the amicable numbers under 10000 //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 //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 { getSum } from "../../../Typescript/typescriptClasses/ArrayAlgorithms";
import { Unsolved } from "../Unsolved"; import { getDivisors } from "../../../Typescript/typescriptClasses/NumberAlgorithms";
import { Problem } from "./Problem"; import { Problem } from "./Problem";
@@ -62,6 +62,7 @@ export class Problem21 extends Problem{
//Start the timer //Start the timer
this.timer.start(); this.timer.start();
//Generate the divisors of all numbers < 10000, get their sum, and add it to the list //Generate the divisors of all numbers < 10000, get their sum, and add it to the list
for(let cnt = 1;cnt < Problem21.LIMIT;++cnt){ for(let cnt = 1;cnt < Problem21.LIMIT;++cnt){
let divisors: number[] = getDivisors(cnt); //Get all the divisors of a number 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 //Sort the array for neatness
this.amicable.sort((n1, n2) => n1 - n2); this.amicable.sort((n1, n2) => n1 - n2);
//Stop the timer //Stop the timer
this.timer.stop(); this.timer.stop();
@@ -107,9 +109,7 @@ export class Problem21 extends Problem{
//Gets //Gets
//Returns the result of solving the problem //Returns the result of solving the problem
public getResult(): string{ public getResult(): string{
if(!this.solved){ this.solvedCheck("result");
throw new Unsolved();
}
let result: string = `All amicable numbers less than ${Problem21.LIMIT} are\n`; let result: string = `All amicable numbers less than ${Problem21.LIMIT} are\n`;
for(let cnt = 0;cnt < this.amicable.length;++cnt){ for(let cnt = 0;cnt < this.amicable.length;++cnt){
result += `${this.amicable[cnt]}\n`; result += `${this.amicable[cnt]}\n`;
@@ -120,22 +120,17 @@ export class Problem21 extends Problem{
} }
//Returns a vector with all of the amicable numbers calculated //Returns a vector with all of the amicable numbers calculated
public getAmicable(): number[]{ public getAmicable(): number[]{
//If the problem hasn't been solved throw an exception this.solvedCheck("amicable numbers");
if(!this.solved){
throw new Unsolved();
}
return this.amicable; return this.amicable;
} }
//Returns the sum of all of the amicable numbers //Returns the sum of all of the amicable numbers
public getSum(): number{ public getSum(): number{
//If the problem hasn't been solved throw an exception this.solvedCheck("sum of the amicable numbers");
if(!this.solved){
throw new Unsolved();
}
return getSum(this.amicable); return getSum(this.amicable);
} }
} }
/* Results: /* Results:
The sum of all of these amicable numbers is 31626 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 It took an average of 7.454 milliseconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
//ProjectEulerTS/Problems/Problem22.ts //ProjectEulerTS/Problems/Problem22.ts
//Matthew Ellison //Matthew Ellison
// Created: 04-08-21 // Created: 04-08-21
//Modified: 04-08-21 //Modified: 07-14-21
//What is the total of all the name scores in this file? //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 //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 <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import { getSum } from "../../../Typescript/typescriptClasses/Algorithms";
import { Unsolved } from "../Unsolved"; import { getSum } from "../../../Typescript/typescriptClasses/ArrayAlgorithms";
import { Problem } from "./Problem"; import { Problem } from "./Problem";
@@ -422,6 +422,7 @@ export class Problem22 extends Problem{
//Start the timer //Start the timer
this.timer.start(); this.timer.start();
//Sort all the names //Sort all the names
Problem22.names.sort(); Problem22.names.sort();
//Step through every name adding up the values of the characters //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 //Get the sum of all the numbers
this.sum = getSum(this.prod); this.sum = getSum(this.prod);
//Stop the timer //Stop the timer
this.timer.stop(); this.timer.stop();
@@ -456,9 +458,7 @@ export class Problem22 extends Problem{
//Gets //Gets
//Returns the result of solving the problem //Returns the result of solving the problem
public getResult(): string{ public getResult(): string{
if(!this.solved){ this.solvedCheck("result");
throw new Unsolved();
}
return `The answer to the question is ${this.sum}`; return `The answer to the question is ${this.sum}`;
} }
//Returns the vector of the names being scored //Returns the vector of the names being scored
@@ -467,13 +467,12 @@ export class Problem22 extends Problem{
} }
//Returns the sum of the names scores //Returns the sum of the names scores
public getNameScoreSum(): number{ public getNameScoreSum(): number{
if(!this.solved){ this.solvedCheck("score of the names");
throw new Unsolved();
}
return this.sum; return this.sum;
} }
} }
/* Results: /* Results:
The answer to the question is 871198282 The answer to the question is 871198282
It took an average of 473.266 microseconds to run this problem through 100 iterations It took an average of 473.266 microseconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
//ProjectEulerTS/Problems/Problem23.ts //ProjectEulerTS/Problems/Problem23.ts
//Matthew Ellison //Matthew Ellison
// Created: 04-08-21 // 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 //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 //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 { getSum } from "../../../Typescript/typescriptClasses/ArrayAlgorithms";
import { Unsolved } from "../Unsolved"; import { getDivisors } from "../../../Typescript/typescriptClasses/NumberAlgorithms";
import { Problem } from "./Problem"; import { Problem } from "./Problem";
@@ -122,20 +122,17 @@ export class Problem23 extends Problem{
//Gets //Gets
//Returns the result of solving the problem //Returns the result of solving the problem
public getResult(): string{ public getResult(): string{
if(!this.solved){ this.solvedCheck("result");
throw new Unsolved();
}
return `The answer is ${this.sum}`; return `The answer is ${this.sum}`;
} }
//Returns the sum of the numbers asked for //Returns the sum of the numbers asked for
public getSum(): number{ public getSum(): number{
if(!this.solved){ this.solvedCheck("sum");
throw new Unsolved();
}
return this.sum; return this.sum;
} }
} }
/* Results: /* Results:
The answer is 4179871 The answer is 4179871
It took an average of 7.798 seconds to run this problem through 100 iterations It took an average of 7.798 seconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
//ProjectEulerTS/Problems/Problem24.ts //ProjectEulerTS/Problems/Problem24.ts
//Matthew Ellison //Matthew Ellison
// Created: 04-08-21 // 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? //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 //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 { getPermutations } from "../../../Typescript/typescriptClasses/StringAlgorithms";
import { Unsolved } from "../Unsolved";
import { Problem } from "./Problem"; import { Problem } from "./Problem";
@@ -52,9 +51,11 @@ export class Problem24 extends Problem{
//Start the timer //Start the timer
this.timer.start(); this.timer.start();
//Get all the permutations of the string //Get all the permutations of the string
this.permutations = getPermutations(Problem24.nums); this.permutations = getPermutations(Problem24.nums);
//Stop the timer //Stop the timer
this.timer.stop(); this.timer.stop();
@@ -69,28 +70,22 @@ export class Problem24 extends Problem{
//Gets //Gets
//Returns the result of solving the problem //Returns the result of solving the problem
public getResult(): string{ public getResult(): string{
if(!this.solved){ this.solvedCheck("result");
throw new Unsolved();
}
return `The 1 millionth permutation is ${this.permutations[Problem24.NEEDED_PERM - 1]}`; return `The 1 millionth permutation is ${this.permutations[Problem24.NEEDED_PERM - 1]}`;
} }
//Returns an array with all of the permutations //Returns an array with all of the permutations
public getPermutationsList(): string[]{ public getPermutationsList(): string[]{
if(!this.solved){ this.solvedCheck("permutations");
throw new Unsolved();
}
return this.permutations; return this.permutations;
} }
//Returns the requested permutation //Returns the requested permutation
public getPermutation(){ public getPermutation(){
//If the problem hasn't been solved throw an exception this.solvedCheck("1,000,000th permutation");
if(!this.solved){
throw new Unsolved();
}
return this.permutations[Problem24.NEEDED_PERM - 1]; return this.permutations[Problem24.NEEDED_PERM - 1];
} }
} }
/* Results: /* Results:
The 1 millionth permutation is 2783915460 The 1 millionth permutation is 2783915460
It took an average of 1.946 seconds to run this problem through 100 iterations It took an average of 1.946 seconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
//ProjectEulerTS/Problems/Problem25.ts //ProjectEulerTS/Problems/Problem25.ts
//Matthew Ellison //Matthew Ellison
// Created: 04-08-21 // 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? //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 //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 { getFibBig } from "../../../Typescript/typescriptClasses/NumberAlgorithms";
import { Unsolved } from "../Unsolved";
import { Problem } from "./Problem"; import { Problem } from "./Problem";
@@ -53,12 +52,14 @@ export class Problem25 extends Problem{
//Start the timer //Start the timer
this.timer.start(); this.timer.start();
//Move through all Fibonacci numbers until you reach the one with at least NUM_DIGITS digits //Move through all Fibonacci numbers until you reach the one with at least NUM_DIGITS digits
while(this.number.toString().length < Problem25.NUM_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.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 this.number = getFibBig(this.index); //Calculate the number
} }
//Stop the timer //Stop the timer
this.timer.stop(); this.timer.stop();
@@ -74,48 +75,37 @@ export class Problem25 extends Problem{
//Gets //Gets
//Returns the result of solving the problem //Returns the result of solving the problem
public getResult(): string{ public getResult(): string{
if(!this.solved){ this.solvedCheck("result");
throw new Unsolved();
}
return `The first Fibonacci number with ${Problem25.NUM_DIGITS} digits is ${this.number}\nIts index is ${this.index}`; return `The first Fibonacci number with ${Problem25.NUM_DIGITS} digits is ${this.number}\nIts index is ${this.index}`;
} }
//Returns the Fibonacci number asked for //Returns the Fibonacci number asked for
public getNumber(): bigint{ public getNumber(): bigint{
if(!this.solved){ this.solvedCheck("fibonacci number");
throw new Unsolved();
}
return this.number; return this.number;
} }
//Returns the Fibonacci number asked for as a string //Returns the Fibonacci number asked for as a string
public getNumberString(): string{ public getNumberString(): string{
if(!this.solved){ this.solvedCheck("fibonacci number as a string");
throw new Unsolved();
}
return this.number.toString(); return this.number.toString();
} }
//Returns the index of the requested Fibonacci number //Returns the index of the requested Fibonacci number
public getIndex(): bigint{ public getIndex(): bigint{
if(!this.solved){ this.solvedCheck("index of the fibonacci number");
throw new Unsolved();
}
return this.index; return this.index;
} }
//Returns the index of the requested Fibonacci number as a string //Returns the index of the requested Fibonacci number as a string
public getIndexString(): string{ public getIndexString(): string{
if(!this.solved){ this.solvedCheck("index of the fibonacci number as a string");
throw new Unsolved();
}
return this.index.toString(); return this.index.toString();
} }
//Returns the index of the requested Fibonacci number as a number //Returns the index of the requested Fibonacci number as a number
public getIndexInt(): number{ public getIndexInt(): number{
if(!this.solved){ this.solvedCheck("index of the fibonacci number as an int");
throw new Unsolved();
}
return Number(this.index); return Number(this.index);
} }
} }
/* Results: /* Results:
The first Fibonacci number with 1000 digits is 1070066266382758936764980584457396885083683896632151665013235203375314520604694040621889147582489792657804694888177591957484336466672569959512996030461262748092482186144069433051234774442750273781753087579391666192149259186759553966422837148943113074699503439547001985432609723067290192870526447243726117715821825548491120525013201478612965931381792235559657452039506137551467837543229119602129934048260706175397706847068202895486902666185435124521900369480641357447470911707619766945691070098024393439617474103736912503231365532164773697023167755051595173518460579954919410967778373229665796581646513903488154256310184224190259846088000110186255550245493937113651657039447629584714548523425950428582425306083544435428212611008992863795048006894330309773217834864543113205765659868456288616808718693835297350643986297640660000723562917905207051164077614812491885830945940566688339109350944456576357666151619317753792891661581327159616877487983821820492520348473874384736771934512787029218636250627816 The first Fibonacci number with 1000 digits is 1070066266382758936764980584457396885083683896632151665013235203375314520604694040621889147582489792657804694888177591957484336466672569959512996030461262748092482186144069433051234774442750273781753087579391666192149259186759553966422837148943113074699503439547001985432609723067290192870526447243726117715821825548491120525013201478612965931381792235559657452039506137551467837543229119602129934048260706175397706847068202895486902666185435124521900369480641357447470911707619766945691070098024393439617474103736912503231365532164773697023167755051595173518460579954919410967778373229665796581646513903488154256310184224190259846088000110186255550245493937113651657039447629584714548523425950428582425306083544435428212611008992863795048006894330309773217834864543113205765659868456288616808718693835297350643986297640660000723562917905207051164077614812491885830945940566688339109350944456576357666151619317753792891661581327159616877487983821820492520348473874384736771934512787029218636250627816
Its index is 4782 Its index is 4782

View File

@@ -1,7 +1,7 @@
//ProjectEulerTS/Problems/Problem26.ts //ProjectEulerTS/Problems/Problem26.ts
//Matthew Ellison //Matthew Ellison
// Created: 04-18-21 // 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. //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 //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"; import { Problem } from "./Problem";
@@ -52,6 +51,7 @@ export class Problem26 extends Problem{
//Start the timer //Start the timer
this.timer.start(); this.timer.start();
//Start with 1/2 and find out how long the logest cycle is by checking the reainders //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 //Loop through every number from 2-999 and use it for the denominator
for(let denominator: number = 2;denominator <= Problem26.TOP_NUM;++denominator){ for(let denominator: number = 2;denominator <= Problem26.TOP_NUM;++denominator){
@@ -88,6 +88,7 @@ export class Problem26 extends Problem{
} }
} }
//Stop the timer //Stop the timer
this.timer.stop(); this.timer.stop();
@@ -103,27 +104,22 @@ export class Problem26 extends Problem{
//Gets //Gets
//Returns the result of solving the problem //Returns the result of solving the problem
public getResult(): string{ public getResult(): string{
if(!this.solved){ this.solvedCheck("result");
throw new Unsolved();
}
return `The longest cycle is ${this.longestCycle} digits long\nIt started with the number ${this.longestNumber}`; return `The longest cycle is ${this.longestCycle} digits long\nIt started with the number ${this.longestNumber}`;
} }
//Returns the length of the longest cycle //Returns the length of the longest cycle
public getLongestCycle(): number{ public getLongestCycle(): number{
if(!this.solved){ this.solvedCheck("length of the longest cycle");
throw new Unsolved();
}
return this.longestCycle; return this.longestCycle;
} }
//Returns the denominator that start the longest cycle //Returns the denominator that start the longest cycle
public getLongestNumber(){ public getLongestNumber(){
if(!this.solved){ this.solvedCheck("denominator that starts the longest cycle");
throw new Unsolved();
}
return this.longestNumber; return this.longestNumber;
} }
} }
/* Results: /* Results:
The longest cycle is 982 digits long The longest cycle is 982 digits long
It started with the number 983 It started with the number 983

View File

@@ -1,7 +1,7 @@
//ProjectEulerTS/Problems/Problem26.ts //ProjectEulerTS/Problems/Problem26.ts
//Matthew Ellison //Matthew Ellison
// Created: 05-24-21 // 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. //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 //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 { isPrime } from "../../../Typescript/typescriptClasses/NumberAlgorithms";
import { Unsolved } from "../Unsolved";
import { Problem } from "./Problem"; import { Problem } from "./Problem";
@@ -56,6 +55,7 @@ export class Problem27 extends Problem{
//Start the timer //Start the timer
this.timer.start(); this.timer.start();
//Start with the lowest possible A and check all possibilities after that //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){ 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 //Start witht he lowest possible B and check all possibilities after that
@@ -78,6 +78,7 @@ export class Problem27 extends Problem{
} }
} }
//Stop the timer //Stop the timer
this.timer.stop(); this.timer.stop();
@@ -94,45 +95,32 @@ export class Problem27 extends Problem{
//Gets //Gets
//Returns the result of solving the problem //Returns the result of solving the problem
public getResult(): string{ public getResult(): string{
if(!this.solved){ this.solvedCheck("result");
throw new Unsolved();
}
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()}`; 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 //Returns the top A that was generated
public getTopA(): number{ public getTopA(): number{
//If the problem hasn't been solved throw an exception this.solvedCheck("largest A");
if(!this.solved){
throw new Unsolved();
}
return this.topA; return this.topA;
} }
//Returns the top B that was generated //Returns the top B that was generated
public getTopB(): number{ public getTopB(): number{
//If the problem hasn't been solved throw an exception this.solvedCheck("largest B");
if(!this.solved){
throw new Unsolved();
}
return this.topB; return this.topB;
} }
//Returns the top N that was generated //Returns the top N that was generated
public getTopN(): number{ public getTopN(): number{
//If the problem hasn't been solved throw an exception this.solvedCheck("largest N");
if(!this.solved){
throw new Unsolved();
}
return this.topN; return this.topN;
} }
//Returns the product of A and B for the answer //Returns the product of A and B for the answer
public getProduct(): number{ public getProduct(): number{
//If the problem hasn't been solved throw an exception this.solvedCheck("product of A and B");
if(!this.solved){
throw new Unsolved();
}
return this.topA * this.topB; return this.topA * this.topB;
} }
} }
/* Results: /* Results:
The greatest number of primes found is 70 The greatest number of primes found is 70
It was found with A = -61, B = 971 It was found with A = -61, B = 971

View File

@@ -1,7 +1,7 @@
//ProjectEulerTS/Problems/Problem28.ts //ProjectEulerTS/Problems/Problem28.ts
//Matthew Ellison //Matthew Ellison
// Created: 05-26-21 // 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 //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 //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"; import { Problem } from "./Problem";
@@ -122,11 +121,13 @@ export class Problem28 extends Problem{
//Start the timer //Start the timer
this.timer.start(); this.timer.start();
//Setup the grid //Setup the grid
this.setupGrid(); this.setupGrid();
//FInd the sum of the diagonals in the grid //FInd the sum of the diagonals in the grid
this.findSum(); this.findSum();
//Stop the timer //Stop the timer
this.timer.stop(); this.timer.stop();
@@ -141,29 +142,22 @@ export class Problem28 extends Problem{
//Gets //Gets
//Returns the result of solving the problem //Returns the result of solving the problem
public getResult(): string{ public getResult(): string{
if(!this.solved){ this.solvedCheck("result");
throw new Unsolved();
}
return `The sum of the diagonals in the given grid is ${this.sumOfDiagonals}`; return `The sum of the diagonals in the given grid is ${this.sumOfDiagonals}`;
} }
//Returns the grid //Returns the grid
public getGrid(): number[][]{ public getGrid(): number[][]{
//IF the problem hasn't been solved throw an exception this.solvedCheck("grid");
if(!this.solved){
throw new Unsolved();
}
return Problem28.grid; return Problem28.grid;
} }
//Returns the sum of the diagonals //Returns the sum of the diagonals
public getSum(): number{ public getSum(): number{
//If the problem hasn't been solved throw an exception this.solvedCheck("sum");
if(!this.solved){
throw new Unsolved();
}
return this.sumOfDiagonals; return this.sumOfDiagonals;
} }
} }
/* Results: /* Results:
The sum of the diagonals in the given grid is 669171001 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 It took an average of 16.924 milliseconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
//ProjectEulerTS/Problems/Problem29.ts //ProjectEulerTS/Problems/Problem29.ts
//Matthew Ellison //Matthew Ellison
// Created: 05-28-21 // 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? //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 //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"; import { Problem } from "./Problem";
@@ -53,6 +52,7 @@ export class Problem29 extends Problem{
//Start the timer //Start the timer
this.timer.start(); this.timer.start();
//Start witht he first A and move towards the top //Start witht he first A and move towards the top
for(let currentA = Problem29.BOTTOM_A;currentA <= Problem29.TOP_A;++currentA){ for(let currentA = Problem29.BOTTOM_A;currentA <= Problem29.TOP_A;++currentA){
//Start with the first B and move towards the top //Start with the first B and move towards the top
@@ -66,6 +66,7 @@ export class Problem29 extends Problem{
} }
} }
//Stop the timer //Stop the timer
this.timer.stop(); this.timer.stop();
@@ -80,9 +81,7 @@ export class Problem29 extends Problem{
//Gets //Gets
//Returns the result of solving the problem //Returns the result of solving the problem
public getResult(): string{ public getResult(): string{
if(!this.solved){ this.solvedCheck("result");
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}`; 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 //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 //Returns a list of all the unique values for a^b
public getUnique(): bigint[]{ public getUnique(): bigint[]{
//If the problem hasn't been solved throw an exception this.solvedCheck("unique values for a^b");
if(!this.solved){
throw new Unsolved();
}
return this.unique; 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: /* Results:
The number of unique values generated by a^b for 2 <= a <= 100 and 2 <= b <= 100 is 9183 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 It took an average of 349.571 milliseconds to run this problem through 100 iterations

View File

@@ -1,11 +1,11 @@
//ProjectEulerTS/Problems/Problem3.ts //ProjectEulerTS/Problems/Problem3.ts
//Matthew Ellison //Matthew Ellison
// Created: 10-19-20 // Created: 10-19-20
//Modified: 10-19-20 //Modified: 07-14-21
//The largest prime factor of 600851475143 //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 //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 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 it under the terms of the GNU Lesser General Public License as published by
@@ -23,8 +23,7 @@
import { Problem } from "./Problem"; import { Problem } from "./Problem";
import { getFactors } from "../../../Typescript/typescriptClasses/Algorithms"; import { getFactors } from "../../../Typescript/typescriptClasses/NumberAlgorithms";
import { Unsolved } from "../Unsolved";
export class Problem3 extends Problem{ export class Problem3 extends Problem{
@@ -51,10 +50,12 @@ export class Problem3 extends Problem{
//Start the timer //Start the timer
this.timer.start(); this.timer.start();
//Get all the factors of the number //Get all the factors of the number
this.factors = getFactors(Problem3.GOAL_NUMBER); this.factors = getFactors(Problem3.GOAL_NUMBER);
//The last element should be the largest factor //The last element should be the largest factor
//Stop the timer //Stop the timer
this.timer.stop(); this.timer.stop();
@@ -69,23 +70,17 @@ export class Problem3 extends Problem{
//Gets //Gets
//Returns the result of solving the problem //Returns the result of solving the problem
public getResult(): string{ public getResult(): string{
if(!this.solved){ this.solvedCheck("result");
throw new Unsolved();
}
return `The largest factor of the number ${Problem3.GOAL_NUMBER} is ${this.factors[this.factors.length - 1]}`; 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 //Returns the list of factors of the number
public getFactors(): number[]{ public getFactors(): number[]{
if(!this.solved){ this.solvedCheck("factors");
throw new Unsolved();
}
return this.factors; return this.factors;
} }
//Returns the largest factor of the number //Returns the largest factor of the number
public getLargestFactor(): number{ public getLargestFactor(): number{
if(!this.solved){ this.solvedCheck("largest factor");
throw new Unsolved();
}
return this.factors[this.factors.length]; return this.factors[this.factors.length];
} }
//Returns the number for which we are getting the factor //Returns the number for which we are getting the factor
@@ -94,6 +89,7 @@ export class Problem3 extends Problem{
} }
} }
/* Results: /* Results:
*/ */

View File

@@ -1,7 +1,7 @@
//ProjectEulerTS/Problems/Problem30.ts //ProjectEulerTS/Problems/Problem30.ts
//Matthew Ellison //Matthew Ellison
// Created: 05-28-21 // 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. //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 //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 { getSum } from "../../../Typescript/typescriptClasses/ArrayAlgorithms";
import { Unsolved } from "../Unsolved";
import { Problem } from "./Problem"; import { Problem } from "./Problem";
@@ -66,6 +65,7 @@ export class Problem30 extends Problem{
//Start the timer //Start the timer
this.timer.start(); this.timer.start();
//Start with the lowest number and increment until you reach the largest number //Start with the lowest number and increment until you reach the largest number
for(let currentNum = Problem30.BOTTOM_NUM;currentNum <= Problem30.TOP_NUM;++currentNum){ for(let currentNum = Problem30.BOTTOM_NUM;currentNum <= Problem30.TOP_NUM;++currentNum){
//Get the digits of the number //Get the digits of the number
@@ -85,6 +85,7 @@ export class Problem30 extends Problem{
//Get the sum of the numbers //Get the sum of the numbers
this.sum = getSum(this.sumOfFifthNumbers); this.sum = getSum(this.sumOfFifthNumbers);
//Stop the timer //Stop the timer
this.timer.stop(); this.timer.stop();
@@ -99,9 +100,7 @@ export class Problem30 extends Problem{
//Gets //Gets
//Returns the result of solving the problem //Returns the result of solving the problem
public getResult(): string{ public getResult(): string{
if(!this.solved){ this.solvedCheck("result");
throw new Unsolved();
}
return `This sum of all the numbers that can be written as the sum of the fifth powers of their digits is ${this.sum}`; 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 //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 //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[]{ public getListOfSumOfFifths(): number[]{
//If the problem hasn't been solved throw an exception this.solvedCheck("list of all numbers that are the sum of the 5th power of their digits");
if(!this.solved){
throw new Unsolved();
}
return this.sumOfFifthNumbers; return this.sumOfFifthNumbers;
} }
//This returns the sum of all entries in sumOfFifthNumbers //This returns the sum of all entries in sumOfFifthNumbers
public getSumOfList(): number{ public getSumOfList(): number{
//If the problem hasn't been solved throw an eception this.solvedCheck("sum of all numbers that are the sum of the 5th power of their digits");
if(!this.solved){
throw new Unsolved();
}
return this.sum; return this.sum;
} }
} }
/* Results: /* Results:
This sum of all the numbers that can be written as the sum of the fifth powers of their digits is 443839 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 It took an average of 180.976 milliseconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
//ProjectEulerTS/Problems/Problem31.ts //ProjectEulerTS/Problems/Problem31.ts
//Matthew Ellison //Matthew Ellison
// Created: 05-28-21 // 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? //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 //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"; import { Problem } from "./Problem";
@@ -50,6 +49,7 @@ export class Problem31 extends Problem{
//Start the timer //Start the timer
this.timer.start(); this.timer.start();
//Start with 200p and remove the necessary coins with each loop //Start with 200p and remove the necessary coins with each loop
for(let pound2 = Problem31.DESIRED_VALUE;pound2 >= 0;pound2 -= 200){ for(let pound2 = Problem31.DESIRED_VALUE;pound2 >= 0;pound2 -= 200){
for(let pound1 = pound2;pound1 >= 0;pound1 -= 100){ for(let pound1 = pound2;pound1 >= 0;pound1 -= 100){
@@ -67,6 +67,7 @@ export class Problem31 extends Problem{
} }
} }
//Stop the timer //Stop the timer
this.timer.stop(); this.timer.stop();
@@ -81,21 +82,17 @@ export class Problem31 extends Problem{
//Gets //Gets
//Returns the result of solving the proble //Returns the result of solving the proble
public getResult(): string{ public getResult(): string{
if(!this.solved){ this.solvedCheck("result");
throw new Unsolved();
}
return `There are ${this.permutations} ways to make 2 pounds with the given denominations of coins`; 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 //Returns the number of correct permutations of the coins
public getPermutations(): number{ public getPermutations(): number{
//If the problem hasn't been solved throw an exception this.solvedCheck("number of correct permutations of the coins");
if(!this.solved){
throw new Unsolved();
}
return this.permutations; return this.permutations;
} }
} }
/* Results: /* Results:
There are 73682 ways to make 2 pounds with the given denominations of coins 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 It took an average of 197.293 microseconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
//ProjectEulerTS/Problems/Problem32.ts //ProjectEulerTS/Problems/Problem32.ts
//Matthew Ellison //Matthew Ellison
// Created: 05-28-21 // 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. //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 //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 { findNumOccurrence } from "../../../Typescript/typescriptClasses/StringAlgorithms";
import { Unsolved } from "../Unsolved";
import { Problem } from "./Problem"; import { Problem } from "./Problem";
@@ -85,6 +84,7 @@ export class Problem32 extends Problem{
//Start the timer //Start the timer
this.timer.start(); this.timer.start();
//Create the multiplicand and start working your way up //Create the multiplicand and start working your way up
for(let multiplicand = 1;multiplicand <= Problem32.TOP_MULTIPLICAND;++multiplicand){ for(let multiplicand = 1;multiplicand <= Problem32.TOP_MULTIPLICAND;++multiplicand){
//Run through all possible multipliers //Run through all possible multipliers
@@ -108,6 +108,7 @@ export class Problem32 extends Problem{
this.sumOfPandigitals += prod.getProduct(); this.sumOfPandigitals += prod.getProduct();
} }
//Stop the timer //Stop the timer
this.timer.stop(); this.timer.stop();
@@ -140,21 +141,17 @@ export class Problem32 extends Problem{
} }
//Gets //Gets
public getResult(): string{ public getResult(): string{
if(!this.solved){ this.solvedCheck("result");
throw new Unsolved();
}
return `There are ${this.listOfProducts.length} unique 1-9 pandigitals\nThe sum of the products of the pandigitals is ${this.sumOfPandigitals}` 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 //Returns the sum of pandigitals
public getSumOfPandigitlas(): number{ public getSumOfPandigitlas(): number{
//If the problem hasn't been solved throw an exception this.solvedCheck("sum of the pandigitals");
if(!this.solved){
throw new Unsolved();
}
return this.sumOfPandigitals; return this.sumOfPandigitals;
} }
} }
/* Results: /* Results:
There are 7 unique 1-9 pandigitals There are 7 unique 1-9 pandigitals
The sum of the products of the pandigitals is 45228 The sum of the products of the pandigitals is 45228

View File

@@ -1,7 +1,7 @@
//ProjectEulerTS/Problems/Problem33.ts //ProjectEulerTS/Problems/Problem33.ts
//Matthew Ellison //Matthew Ellison
// Created: 05-28-21 // 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 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 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 { getProd } from "../../../Typescript/typescriptClasses/ArrayAlgorithms";
import { Unsolved } from "../Unsolved"; import { gcd } from "../../../Typescript/typescriptClasses/NumberAlgorithms";
import { Problem } from "./Problem"; import { Problem } from "./Problem";
@@ -63,6 +63,7 @@ export class Problem33 extends Problem{
//Start the timer //Start the timer
this.timer.start(); this.timer.start();
//Search every possible numerator/denominator pair //Search every possible numerator/denominator pair
for(let denominator = Problem33.MIN_DENOMINATOR;denominator <= Problem33.MAX_DENOMINATOR;++denominator){ for(let denominator = Problem33.MIN_DENOMINATOR;denominator <= Problem33.MAX_DENOMINATOR;++denominator){
for(let numerator = Problem33.MIN_NUMERATOR;(numerator < denominator) && (numerator <= Problem33.MAX_NUMERATOR);++numerator){ 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 //Save the denominator
this.prodDenominator = denomProd / curGcd; this.prodDenominator = denomProd / curGcd;
//Stop the timer //Stop the timer
this.timer.stop(); this.timer.stop();
@@ -124,37 +126,27 @@ export class Problem33 extends Problem{
//Gets //Gets
//Returns the reult of solving the problem //Returns the reult of solving the problem
public getResult(): string{ public getResult(): string{
if(!this.solved){ this.solvedCheck("result");
throw new Unsolved();
}
return `The denominator of the product is ${this.prodDenominator}`; return `The denominator of the product is ${this.prodDenominator}`;
} }
//Returns the list of numerators //Returns the list of numerators
public getNumerators(): number[]{ public getNumerators(): number[]{
//If the problem hasn't been solved throw an exception this.solvedCheck("list of numerators");
if(!this.solved){
throw new Unsolved();
}
return this.numerators; return this.numerators;
} }
//Returns the list of denominators //Returns the list of denominators
public getDenominators(): number[]{ public getDenominators(): number[]{
//If the porblem hasn't been solved throw an exception this.solvedCheck("list of denominators");
if(!this.solved){
throw new Unsolved();
}
return this.denominators; return this.denominators;
} }
//Returns the answer to the question //Returns the answer to the question
public getProdDenominator(){ public getProdDenominator(){
//If the problem hasn't been solved throw an exception this.solvedCheck("denominator");
if(!this.solved){
throw new Unsolved();
}
return this.prodDenominator; return this.prodDenominator;
} }
} }
/* Results: /* Results:
The denominator of the product is 100 The denominator of the product is 100
It took an average of 508.730 microseconds to run this problem through 100 iterations It took an average of 508.730 microseconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
//ProjectEulerTS/Problems/Problem34.ts //ProjectEulerTS/Problems/Problem34.ts
//Matthew Ellison //Matthew Ellison
// Created: 06-01-21 // 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 //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 //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 { factorial } from "../../../Typescript/typescriptClasses/NumberAlgorithms";
import { Unsolved } from "../Unsolved";
import { Problem } from "./Problem"; import { Problem } from "./Problem";
@@ -56,6 +55,7 @@ export class Problem34 extends Problem{
//Start the timer //Start the timer
this.timer.start(); this.timer.start();
//Pre-compute the possible factorials form 0! to 9! //Pre-compute the possible factorials form 0! to 9!
for(let cnt = 0;cnt <= 9;++cnt){ for(let cnt = 0;cnt <= 9;++cnt){
this.factorials[cnt] = factorial(cnt); this.factorials[cnt] = factorial(cnt);
@@ -75,6 +75,7 @@ export class Problem34 extends Problem{
} }
} }
//Stop the timer //Stop the timer
this.timer.stop(); this.timer.stop();
@@ -93,30 +94,22 @@ export class Problem34 extends Problem{
//Gets //Gets
//Returns the result of solving the problem //Returns the result of solving the problem
public getResult(): string{ public getResult(): string{
//If the problem hasn't been solved throw an exception this.solvedCheck("result");
if(!this.solved){
throw new Unsolved();
}
return `The sum of all numbers that are the sum of their digit's factorials is ${this.sum}`; 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 //Returns the list of factorials from 0-9
public getFactorials(): number[]{ public getFactorials(): number[]{
//If the problem hasn't been solved throw an exception this.solvedCheck("list of factorials");
if(!this.solved){
throw new Unsolved();
}
return this.factorials; return this.factorials;
} }
//Returns the sum of all numbers equal to the sum of their digit's factorials //Returns the sum of all numbers equal to the sum of their digit's factorials
public getSum(): number{ public getSum(): number{
//If the problem hasn't been solved throw an exception this.solvedCheck("sum");
if(!this.solved){
throw new Unsolved();
}
return this.sum; return this.sum;
} }
} }
/* Results: /* Results:
The sum of all numbers that are the sum of their digit's factorials is 40730 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 It took an average of 87.615 milliseconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
//ProjectEulerTS/Problems/Problem35.ts //ProjectEulerTS/Problems/Problem35.ts
//Matthew Ellison //Matthew Ellison
// Created: 06-06-21 // Created: 06-06-21
//Modified: 06-06-21 //Modified: 07-14-21
//How many circular primes are there below one million? //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 //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 { getPrimes } from "../../../Typescript/typescriptClasses/NumberAlgorithms";
import { Unsolved } from "../Unsolved";
import { Problem } from "./Problem"; import { Problem } from "./Problem";
@@ -62,6 +61,7 @@ export class Problem35 extends Problem{
//Start the timer //Start the timer
this.timer.start(); this.timer.start();
//Get all primes under 1,000,000 //Get all primes under 1,000,000
this.primes = getPrimes(Problem35.MAX_NUM); this.primes = getPrimes(Problem35.MAX_NUM);
//Go through all primes, get all their rotations, and check if htose numbers are also primes //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 //Stop the timer
this.timer.stop(); this.timer.stop();
@@ -99,38 +100,27 @@ export class Problem35 extends Problem{
//Gets //Gets
//Returns a string with the solution to the problem //Returns a string with the solution to the problem
public getResult(): string{ public getResult(): string{
//If the problem hasn't been solved throw an exception this.solvedCheck("result");
if(!this.solved){
throw new Unsolved();
}
return `The number of all circular prime numbers under ${Problem35.MAX_NUM} is ${this.circularPrimes.length}`; return `The number of all circular prime numbers under ${Problem35.MAX_NUM} is ${this.circularPrimes.length}`;
} }
//Returns the array of primes < MAX_NUM //Returns the array of primes < MAX_NUM
public getPrimes(): number[]{ public getPrimes(): number[]{
//If the problem hasn't been solved throw an exception this.solvedCheck("list of primes");
if(!this.solved){
throw new Unsolved();
}
return this.primes; return this.primes;
} }
//Returns the array of circular primes < MAX_NUM //Returns the array of circular primes < MAX_NUM
public getCircularPrimes(): number[]{ public getCircularPrimes(): number[]{
//If the problem hasn't been solved throw an exception this.solvedCheck("list of circular primes");
if(!this.solved){
throw new Unsolved();
}
return this.circularPrimes; return this.circularPrimes;
} }
//Returns the number of circular primes < MAX_NUM //Returns the number of circular primes < MAX_NUM
public getNumCircularPrimes(): number{ public getNumCircularPrimes(): number{
//If the problem hasn't been solved throw an exception this.solvedCheck("number oc circular primes");
if(!this.solved){
throw new Unsolved();
}
return this.circularPrimes.length; return this.circularPrimes.length;
} }
} }
/* Results: /* Results:
The number of all circular prime numbers under 999999 is 55 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 It took an average of 7.204 seconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
//ProjectEulerTS/Problems/Problem36.ts //ProjectEulerTS/Problems/Problem36.ts
//Matthew Ellison //Matthew Ellison
// Created: 06-29-21 // 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. //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 //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 { getSum } from "../../../Typescript/typescriptClasses/ArrayAlgorithms";
import { Unsolved } from "../Unsolved"; import { toBin } from "../../../Typescript/typescriptClasses/NumberAlgorithms";
import { isPalindrome } from "../../../Typescript/typescriptClasses/StringAlgorithms";
import { Problem } from "./Problem"; import { Problem } from "./Problem";
@@ -53,6 +54,7 @@ export class Problem36 extends Problem{
//Start the timer //Start the timer
this.timer.start(); this.timer.start();
//Start with 1, check if it is a palindrome in base 10 and 2, and continue to MAX_NUM //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){ for(let num = 1;num < Problem36.MAX_NUM;++num){
//Check if num is a palindrome //Check if num is a palindrome
@@ -68,6 +70,7 @@ export class Problem36 extends Problem{
//Get the sum of all palindromes in the list //Get the sum of all palindromes in the list
this.sum = getSum(this.palindromes); this.sum = getSum(this.palindromes);
//Stop the timer //Stop the timer
this.timer.stop(); this.timer.stop();
@@ -83,30 +86,22 @@ export class Problem36 extends Problem{
//Gets //Gets
//Returns a string with the solution to the problem //Returns a string with the solution to the problem
public getResult(): string{ public getResult(): string{
//If the problem hasn't been solved throw an exception this.solvedCheck("result");
if(!this.solved){
throw new Unsolved();
}
return `The sum of all base 10 and base 2 palindromic numbers < ${Problem36.MAX_NUM} is ${this.sum}`; 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 //Return the array of palindromes < MAX_NUM
public getPalindromes(){ public getPalindromes(){
//If the porblem hasn't been solved throw an exception this.solvedCheck("list of palindromes");
if(!this.solved){
throw new Unsolved();
}
return this.palindromes; return this.palindromes;
} }
//Returns the sum of all elements in the array of palindromes //Returns the sum of all elements in the array of palindromes
public getSumOfPalindromes(){ public getSumOfPalindromes(){
//If the problem hasn't been solved throw an exception this.solvedCheck("sum of all palindromes");
if(!this.solved){
throw new Unsolved();
}
return this.sum; return this.sum;
} }
} }
/* Results: /* Results:
The sum of all base 10 and base 2 palindromic numbers < 999999 is 872187 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 It took an average of 297.957 milliseconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
//ProjectEulerTS/Problems/Problem37.ts //ProjectEulerTS/Problems/Problem37.ts
//Matthew Ellison //Matthew Ellison
// Created: 07-01-21 // 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). //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 //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 <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
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 { Unsolved } from "../Unsolved";
import { Problem } from "./Problem"; import { Problem } from "./Problem";
export class Problem37 extends Problem{ export class Problem37 extends Problem{
//Variables //Variables
//Static variables //Static variables
@@ -51,6 +54,7 @@ export class Problem37 extends Problem{
//Start the timer //Start the timer
this.timer.start(); this.timer.start();
//Create the sieve and get the first prime number //Create the sieve and get the first prime number
let sieve = sieveOfEratosthenes(); let sieve = sieveOfEratosthenes();
let tempPrime = sieve.next().value; let tempPrime = sieve.next().value;
@@ -126,6 +130,7 @@ export class Problem37 extends Problem{
//Get the sum of all elements in the truncPrimes list //Get the sum of all elements in the truncPrimes list
this.sum = getSum(this.truncPrimes); this.sum = getSum(this.truncPrimes);
//Stop the timer //Stop the timer
this.timer.stop(); this.timer.stop();
@@ -141,28 +146,22 @@ export class Problem37 extends Problem{
//Gets //Gets
//Returns a string with the solution to the problem //Returns a string with the solution to the problem
public getResult(): string{ public getResult(): string{
//If the problem hasn't been solve dthrow an exception this.solvedCheck("result");
if(!this.solved){
throw new Unsolved();
}
return `The sum of all left and right truncatable primes is ${this.sum}`; return `The sum of all left and right truncatable primes is ${this.sum}`;
} }
//Returns the list of primes that can be truncated //Returns the list of primes that can be truncated
public getTruncatablePrimes(): number[]{ public getTruncatablePrimes(): number[]{
if(!this.solved){ this.solvedCheck("list of truncatable primes");
throw new Unsolved();
}
return this.truncPrimes; return this.truncPrimes;
} }
//Returns the sum of all elements in truncPrimes //Returns the sum of all elements in truncPrimes
public getSumOfTruncatablePrimes(): number{ public getSumOfTruncatablePrimes(): number{
if(!this.solved){ this.solvedCheck("sum of truncatable primes");
throw new Unsolved();
}
return this.sum; return this.sum;
} }
} }
/* Results: /* Results:
The sum of all left and right truncatable primes is 748317 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 It took an average of 104.007 milliseconds to run this problem through 100 iterations

View File

@@ -1,11 +1,11 @@
//ProjectEulerTS/Problems/Problem4.ts //ProjectEulerTS/Problems/Problem4.ts
//Matthew Ellison //Matthew Ellison
// Created: 10-24-20 // 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 //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 //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 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 it under the terms of the GNU Lesser General Public License as published by
@@ -23,7 +23,6 @@
import { Problem } from "./Problem"; import { Problem } from "./Problem";
import { Unsolved } from "../Unsolved";
export class Problem4 extends Problem{ export class Problem4 extends Problem{
@@ -49,6 +48,7 @@ export class Problem4 extends Problem{
//Start the timer //Start the timer
this.timer.start(); this.timer.start();
//Start at the first 3-digit number and check every one up to the last 3-digit number //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){ 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) //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 //Sort the palindromes so that the last one is the largest
this.palindromes = this.palindromes.sort((n1, n2) => n1 - n2); this.palindromes = this.palindromes.sort((n1, n2) => n1 - n2);
//Stop the timer //Stop the timer
this.timer.stop(); this.timer.stop();
@@ -86,27 +87,22 @@ export class Problem4 extends Problem{
//Gets //Gets
//Returns the result of solving the problem //Returns the result of solving the problem
public getResult(): string{ public getResult(): string{
if(!this.solved){ this.solvedCheck("result");
throw new Unsolved();
}
return `The largest palindrome is ${this.palindromes[this.palindromes.length - 1]}`; return `The largest palindrome is ${this.palindromes[this.palindromes.length - 1]}`;
} }
//Returns the list of all palindromes //Returns the list of all palindromes
public getPalindromes(): number[]{ public getPalindromes(): number[]{
if(!this.solved){ this.solvedCheck("palindromes");
throw new Unsolved();
}
return this.palindromes; return this.palindromes;
} }
//Returns the largest palindrome //Returns the largest palindrome
public getLargestPalindrome(): number{ public getLargestPalindrome(): number{
if(!this.solved){ this.solvedCheck("largest palindrome");
throw new Unsolved();
}
return this.palindromes[this.palindromes.length - 1]; return this.palindromes[this.palindromes.length - 1];
} }
} }
/* Results: /* Results:
The largest palindrome is 906609 The largest palindrome is 906609
It took an average of 121.130 milliseconds to run this problem through 100 iterations It took an average of 121.130 milliseconds to run this problem through 100 iterations

View File

@@ -1,11 +1,11 @@
//ProjectEulerTS/Problems/Problem5.ts //ProjectEulerTS/Problems/Problem5.ts
//Matthew Ellison //Matthew Ellison
// Created: 10-26-20 // 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? //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 //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 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 it under the terms of the GNU Lesser General Public License as published by
@@ -23,7 +23,6 @@
import { Problem } from "./Problem"; import { Problem } from "./Problem";
import { Unsolved } from "../Unsolved";
export class Problem5 extends Problem{ export class Problem5 extends Problem{
@@ -48,6 +47,7 @@ export class Problem5 extends Problem{
//Start the timer //Start the timer
this.timer.start(); 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 //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 numFound: boolean = false; //A flag for finding the divisible number
let currentNum: number = 20; //The number that it is currently checking against let currentNum: number = 20; //The number that it is currently checking against
@@ -67,9 +67,10 @@ export class Problem5 extends Problem{
currentNum += 2; currentNum += 2;
} }
} }
//Save the current number as the smallest
this.smallestNum = currentNum this.smallestNum = currentNum
//Stop the timer //Stop the timer
this.timer.stop(); this.timer.stop();
@@ -84,20 +85,17 @@ export class Problem5 extends Problem{
//Gets //Gets
//Returns the result of solving the problem //Returns the result of solving the problem
public getResult(): string{ public getResult(): string{
if(!this.solved){ this.solvedCheck("result");
throw new Unsolved();
}
return `The smallest positive number evenly divisible by all number 1-20 is ${this.smallestNum}`; return `The smallest positive number evenly divisible by all number 1-20 is ${this.smallestNum}`;
} }
//Returns the requested number //Returns the requested number
public getNumber(): number{ public getNumber(): number{
if(!this.solved){ this.solvedCheck("number");
throw new Unsolved();
}
return this.smallestNum; return this.smallestNum;
} }
} }
/* Results: /* Results:
The smallest positive number evenly divisible by all number 1-20 is 232792560 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 It took an average of 892.378 milliseconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
//ProjectEulerTS/Problems/Problem6.ts //ProjectEulerTS/Problems/Problem6.ts
//Matthew Ellison //Matthew Ellison
// Created: 03-10-21 // 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. //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 //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 { Problem } from "./Problem";
import { Unsolved } from "../Unsolved";
export class Problem6 extends Problem{ export class Problem6 extends Problem{
@@ -53,6 +52,7 @@ export class Problem6 extends Problem{
//Start the timer //Start the timer
this.timer.start(); this.timer.start();
//Run through all numbers and add them to the appropriate sums //Run through all numbers and add them to the appropriate sums
for(let currentNum: number = Problem6.START_NUM;currentNum <= Problem6.END_NUM;++currentNum){ 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 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 //Square the sum of all the numbers
this.squareOfSum *= this.squareOfSum; this.squareOfSum *= this.squareOfSum;
//Stop the timer //Stop the timer
this.timer.stop(); this.timer.stop();
@@ -76,34 +77,27 @@ export class Problem6 extends Problem{
//Gets //Gets
//Returns the result of solving the problem //Returns the result of solving the problem
public getResult(): string{ public getResult(): string{
if(!this.solved){ this.solvedCheck("result");
throw new Unsolved();
}
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()}`; 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 //Returns the sum of all the squares
public getSumOfSquares(): number{ public getSumOfSquares(): number{
if(!this.solved){ this.solvedCheck("sum of the squares");
throw new Unsolved();
}
return this.sumOfSquares; return this.sumOfSquares;
} }
//Returns the square of all the sums //Returns the square of all the sums
public getSquareOfSum(){ public getSquareOfSum(){
if(!this.solved){ this.solvedCheck("square of the sums");
throw new Unsolved();
}
return this.squareOfSum; return this.squareOfSum;
} }
//Returns the requested difference //Returns the requested difference
public getDifference(): number{ public getDifference(): number{
if(!this.solved){ this.solvedCheck("difference between the two numbers");
throw new Unsolved();
}
return Math.abs(this.sumOfSquares - this.squareOfSum); return Math.abs(this.sumOfSquares - this.squareOfSum);
} }
} }
/* Results: /* Results:
The difference between the sum of the squares and the square of the sum of all numbers from 1-100 is 25164150 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 It took an average of 344.001 nanoseconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
//ProjectEulerTS/Problems/Problem67.ts //ProjectEulerTS/Problems/Problem67.ts
//Matthew Ellison //Matthew Ellison
// Created: 05-28-21 // Created: 05-28-21
//Modified: 05-28-21 //Modified: 07-14-21
//Find the maximum total from top to bottom //Find the maximum total from top to bottom
/* /*
59 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]] [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: /* Results:
The value of the longest path is 7273 The value of the longest path is 7273
It took an average of 413.002 milliseconds to run this problem through 100 iterations It took an average of 413.002 milliseconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
//ProjectEulerTS/Problems/Problem7.ts //ProjectEulerTS/Problems/Problem7.ts
//Matthew Ellison //Matthew Ellison
// Created: 03-10-21 // Created: 03-10-21
//Modified: 03-10-21 //Modified: 07-14-21
//What is the 10001th prime number? //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 //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 { Problem } from "./Problem";
import { Unsolved } from "../Unsolved"; import { getNumPrimes } from "../../../Typescript/typescriptClasses/NumberAlgorithms";
import { getNumPrimes } from "../../../Typescript/typescriptClasses/Algorithms";
export class Problem7 extends Problem{ export class Problem7 extends Problem{
@@ -50,9 +49,11 @@ export class Problem7 extends Problem{
//Start the timer //Start the timer
this.timer.start(); this.timer.start();
//Setup the variables //Setup the variables
this.primes = getNumPrimes(Problem7.NUMBER_OF_PRIMES); //Holds the prime numbers this.primes = getNumPrimes(Problem7.NUMBER_OF_PRIMES); //Holds the prime numbers
//Stop the timer //Stop the timer
this.timer.stop(); this.timer.stop();
@@ -67,21 +68,17 @@ export class Problem7 extends Problem{
//Gets //Gets
//Returns the result of solving the problem //Returns the result of solving the problem
public getResult(): string{ public getResult(): string{
if(!this.solved){ this.solvedCheck("result");
throw new Unsolved();
}
return `The ${Problem7.NUMBER_OF_PRIMES}th prime number is ${this.getPrime()}`; return `The ${Problem7.NUMBER_OF_PRIMES}th prime number is ${this.getPrime()}`;
} }
//Returns the requested prime number //Returns the requested prime number
public getPrime(): number{ public getPrime(): number{
//If the problem hasn't been solved throw an exception this.solvedCheck("prime");
if(!this.solved){
throw new Unsolved();
}
return this.primes[this.primes.length - 1]; return this.primes[this.primes.length - 1];
} }
} }
/* Results: /* Results:
The 10001th prime number is 104743 The 10001th prime number is 104743
It took an average of 3.534 milliseconds to run this problem through 100 iterations It took an average of 3.534 milliseconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
//ProjectEulerTS/Problems/Problem8.ts //ProjectEulerTS/Problems/Problem8.ts
//Matthew Ellison //Matthew Ellison
// Created: 03-10-21 // 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? //Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
/* /*
73167176531330624919225119674426574742355349194934 73167176531330624919225119674426574742355349194934
@@ -45,7 +45,6 @@
import { Problem } from "./Problem"; import { Problem } from "./Problem";
import { Unsolved } from "../Unsolved";
export class Problem8 extends Problem{ export class Problem8 extends Problem{
@@ -74,6 +73,7 @@ export class Problem8 extends Problem{
//Start the timer //Start the timer
this.timer.start(); this.timer.start();
//Cycle through the string of numbers looking for the maximum product //Cycle through the string of numbers looking for the maximum product
for(let cnt: number = 12;cnt < Problem8.NUMBER.length;++cnt){ 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)); 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 //Stop the timer
this.timer.stop(); this.timer.stop();
@@ -98,29 +99,22 @@ export class Problem8 extends Problem{
//Gets //Gets
//Returns the result of solving the problem //Returns the result of solving the problem
public getResult(): string{ public getResult(): string{
if(!this.solved){ this.solvedCheck("result");
throw new Unsolved();
}
return `The greatest product is ${this.maxProduct}\nThe numbers are ${this.maxNums}`; return `The greatest product is ${this.maxProduct}\nThe numbers are ${this.maxNums}`;
} }
//Returns the string of numbers that produces the largest product //Returns the string of numbers that produces the largest product
public getLargestNums(): string{ public getLargestNums(): string{
//If the problem hasn't been solved throw an exception this.solvedCheck("numbers that make the largest product");
if(!this.solved){
throw new Unsolved();
}
return this.maxNums; return this.maxNums;
} }
//Returns the requested product //Returns the requested product
public getLargestProduct(): number{ public getLargestProduct(): number{
//If the problem hasn't been solved throw an exception this.solvedCheck("product of the numbers");
if(!this.solved){
throw new Unsolved();
}
return this.maxProduct; return this.maxProduct;
} }
} }
/* Results: /* Results:
The greatest product is 23514624000 The greatest product is 23514624000
The numbers are 5576689664895 The numbers are 5576689664895

View File

@@ -1,7 +1,7 @@
//ProjectEulerTS/Problems/Problem9.ts //ProjectEulerTS/Problems/Problem9.ts
//Matthew Ellison //Matthew Ellison
// Created: 03-24-21 // 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. //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 //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 { Unsolved } from "../Unsolved";
import { Problem } from "./Problem";
export class Problem9 extends Problem{ export class Problem9 extends Problem{
@@ -56,6 +56,7 @@ export class Problem9 extends Problem{
//Start the timer //Start the timer
this.timer.start(); this.timer.start();
//Loop through all possible a's //Loop through all possible a's
while((this.a < Problem9.GOAL_SUM) && (!this.found)){ while((this.a < Problem9.GOAL_SUM) && (!this.found)){
this.b = this.a + 1; //b must be larger than a this.b = this.a + 1; //b must be larger than a
@@ -76,6 +77,7 @@ export class Problem9 extends Problem{
} }
} }
//Stop the timer //Stop the timer
this.timer.stop(); this.timer.stop();
@@ -98,46 +100,32 @@ export class Problem9 extends Problem{
//Gets //Gets
//Returns the result of solving the problem //Returns the result of solving the problem
public getResult(): string{ public getResult(): string{
//If the problem hasn't been solved throw an exception this.solvedCheck("result");
if(!this.solved){
throw new Unsolved();
}
return `The Pythagorean triplet is ${this.a} + ${this.b} + ${this.c}\nThe numbers' product is ${this.getProduct()}`; return `The Pythagorean triplet is ${this.a} + ${this.b} + ${this.c}\nThe numbers' product is ${this.getProduct()}`;
} }
//Returns the length of the first side //Returns the length of the first side
public getSideA(): number{ public getSideA(): number{
//If the problem hasn't been solved throw an exception this.solvedCheck("first side");
if(!this.solved){
throw new Unsolved();
}
return this.a; return this.a;
} }
//Returns the length of the second side //Returns the length of the second side
public getSideB(): number{ public getSideB(): number{
//If the problem hasn't been solved throw an exception this.solvedCheck("second side");
if(!this.solved){
throw new Unsolved();
}
return this.b; return this.b;
} }
//Returns the length of the hyp //Returns the length of the hyp
public getSideC(): number{ public getSideC(): number{
//If the problem hasn't been solved throw an exception this.solvedCheck("third side");
if(!this.solved){
throw new Unsolved();
}
return this.c; return this.c;
} }
//Returns the product of the 3 sides //Returns the product of the 3 sides
public getProduct(): number{ public getProduct(): number{
//If the problem hasn't been solved throw an exception this.solvedCheck("product of all three sides");
if(!this.solved){
throw new Unsolved();
}
return this.a * this.b * this.c; return this.a * this.b * this.c;
} }
} }
/* Results: /* Results:
The Pythagorean triplet is 200 + 375 + 425 The Pythagorean triplet is 200 + 375 + 425
The numbers' product is 31875000 The numbers' product is 31875000