Added solution to problem 23

This commit is contained in:
2021-04-08 12:37:21 -04:00
parent 019990e3f2
commit 3704bb64c3
3 changed files with 146 additions and 2 deletions

View File

@@ -32,7 +32,7 @@ enum BenchmarkOptions{runSpecific = 1, runAllShort, runAll, exit, size};
export class Benchmark{
private static tooLong: number[] = [14, 15];
private static tooLong: number[] = [14, 15, 23];
//The driver function for the benchmark selection
public static benchmarkMenu(): void{
let selection: BenchmarkOptions = BenchmarkOptions.size;

View File

@@ -46,12 +46,13 @@ import { Problem19 } from "./Problems/Problem19";
import { Problem20 } from "./Problems/Problem20";
import { Problem21 } from "./Problems/Problem21";
import { Problem22 } from "./Problems/Problem22";
import { Problem23 } from "./Problems/Problem23";
export class ProblemSelection{
//Holds the valid problem numbers
public static PROBLEM_NUMBERS: number[] = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22];
21, 22, 23];
//Returns the problem corresponding to the given problem number
public static getProblem(problemNumber: number): Problem{
@@ -79,6 +80,7 @@ export class ProblemSelection{
case 20: problem = new Problem20(); break;
case 21: problem = new Problem21(); break;
case 22: problem = new Problem22(); break;
case 23: problem = new Problem23(); break;
}
return problem;
}

142
Problems/Problem23.ts Normal file
View File

@@ -0,0 +1,142 @@
//ProjectEulerTS/Problems/Problem23.ts
//Matthew Ellison
// Created: 04-08-21
//Modified: 04-08-21
//Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/typescriptClasses
/*
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { getDivisors, getSum } from "../../../Typescript/typescriptClasses/Algorithms";
import { Unsolved } from "../Unsolved";
import { Problem } from "./Problem";
export class Problem23 extends Problem{
//Variables
//Static variables
private static MAX_NUM: number = 28123; //The largest number to be checked
//Instance variables
private divisorSums: number[]; //This gives the sum of the divisors at subscripts
private sum: number; //The sum of all the numbers we are looking for
//Functions
//Constructor
public constructor(){
super(`Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers`);
this.divisorSums = [];
this.reserveArray();
this.sum = 0;
}
//Operational functions
//Reserve the size of the array to speed up insertion
private reserveArray(): void{
//It is faster to reserve the appropriate amount of ram now
//Make sure every element has a 0 in it's location
while(this.divisorSums.length <= Problem23.MAX_NUM){
this.divisorSums.push(0);
}
}
//Solve the problem
public solve(): void{
//If the problem has already been solved do nothing and end the function
if(this.solved){
return;
}
//Start the timer
this.timer.start();
//Get the sum of the divisors of all numbers < MAX_NUM
for(let cnt = 1;cnt < Problem23.MAX_NUM;++cnt){
let div: number[] = getDivisors(cnt);
//Remove the last element, which is the number itself. This gives us the propper divisors
if(div.length > 1){
div.pop();
}
this.divisorSums[cnt] = getSum(div);
}
//Get the abundant numbers
let abund: number[] = [];
for(let cnt = 0;cnt < this.divisorSums.length;++cnt){
if(this.divisorSums[cnt] > cnt){
abund.push(cnt);
}
}
//Check if each number can be the sum of 2 abundant numbers and add to the sum if no
for(let cnt = 1;cnt < Problem23.MAX_NUM;++cnt){
if(!this.isSum(abund, cnt)){
this.sum += cnt;
}
}
//Stop the timer
this.timer.stop();
//Throw a flag to show the problem is solved
this.solved = true;
}
//A function that returns true if num can be created by adding two elements from abund and false if it cannot
private isSum(abund: number[], num: number): boolean{
let sum: number = 0;
//Pick a number for the first part of the sum
for(let firstNum = 0;firstNum < abund.length;++firstNum){
//Pick a number for the second part of the sum
for(let secondNum = firstNum;secondNum < abund.length;++secondNum){
sum = abund[firstNum] + abund[secondNum];
if(sum == num){
return true;
}
else if(sum > num){
break;
}
}
}
//If you have run through the entire list and did not find a sum then it is false
return false;
}
//Reset the problem so it can be run again
public reset(): void{
super.reset();
this.divisorSums = [];
this.reserveArray();
this.sum = 0;
}
//Gets
//Returns the result of solving the problem
public getResult(): string{
if(!this.solved){
throw new Unsolved();
}
return `The answer is ${this.sum}`;
}
//Returns the sum of the numbers asked for
public getSum(): number{
if(!this.solved){
throw new Unsolved();
}
return this.sum;
}
}
/* Results:
The answer is 4179871
It took an average of 7.798 seconds to run this problem through 100 iterations
*/