mirror of
https://bitbucket.org/Mattrixwv/projecteulerts.git
synced 2025-12-06 17:43:59 -05:00
Updated to use new library layout
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
//ProjectEulerTS/Problems/Problem21.ts
|
||||
//Matthew Ellison
|
||||
// Created: 04-08-21
|
||||
//Modified: 04-08-21
|
||||
//Modified: 07-14-21
|
||||
//Evaluate the sum of all the amicable numbers under 10000
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/typescriptClasses
|
||||
/*
|
||||
@@ -22,8 +22,8 @@
|
||||
*/
|
||||
|
||||
|
||||
import { getDivisors, getSum } from "../../../Typescript/typescriptClasses/Algorithms";
|
||||
import { Unsolved } from "../Unsolved";
|
||||
import { getSum } from "../../../Typescript/typescriptClasses/ArrayAlgorithms";
|
||||
import { getDivisors } from "../../../Typescript/typescriptClasses/NumberAlgorithms";
|
||||
import { Problem } from "./Problem";
|
||||
|
||||
|
||||
@@ -62,6 +62,7 @@ export class Problem21 extends Problem{
|
||||
//Start the timer
|
||||
this.timer.start();
|
||||
|
||||
|
||||
//Generate the divisors of all numbers < 10000, get their sum, and add it to the list
|
||||
for(let cnt = 1;cnt < Problem21.LIMIT;++cnt){
|
||||
let divisors: number[] = getDivisors(cnt); //Get all the divisors of a number
|
||||
@@ -91,6 +92,7 @@ export class Problem21 extends Problem{
|
||||
//Sort the array for neatness
|
||||
this.amicable.sort((n1, n2) => n1 - n2);
|
||||
|
||||
|
||||
//Stop the timer
|
||||
this.timer.stop();
|
||||
|
||||
@@ -107,9 +109,7 @@ export class Problem21 extends Problem{
|
||||
//Gets
|
||||
//Returns the result of solving the problem
|
||||
public getResult(): string{
|
||||
if(!this.solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
this.solvedCheck("result");
|
||||
let result: string = `All amicable numbers less than ${Problem21.LIMIT} are\n`;
|
||||
for(let cnt = 0;cnt < this.amicable.length;++cnt){
|
||||
result += `${this.amicable[cnt]}\n`;
|
||||
@@ -120,22 +120,17 @@ export class Problem21 extends Problem{
|
||||
}
|
||||
//Returns a vector with all of the amicable numbers calculated
|
||||
public getAmicable(): number[]{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!this.solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
this.solvedCheck("amicable numbers");
|
||||
return this.amicable;
|
||||
}
|
||||
//Returns the sum of all of the amicable numbers
|
||||
public getSum(): number{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!this.solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
this.solvedCheck("sum of the amicable numbers");
|
||||
return getSum(this.amicable);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Results:
|
||||
The sum of all of these amicable numbers is 31626
|
||||
It took an average of 7.454 milliseconds to run this problem through 100 iterations
|
||||
|
||||
Reference in New Issue
Block a user