Updated problem 1 algorithm

This commit is contained in:
2020-10-26 14:44:41 -04:00
parent a19249c00b
commit 45114949c3

View File

@@ -1,7 +1,7 @@
//ProjectEulerTS/Problems/Problem1.ts
//Matthew Ellison
// Created: 10-18-20
//Modified: 10-18-20
//Modified: 10-26-20
//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
/*
@@ -48,14 +48,8 @@ export class Problem1 extends Problem{
//Start the timer
this.timer.start();
for(var num: number = 1;num < 1000;++num){
if((num % 3) == 0){
this.fullSum += num;
}
else if((num % 5) == 0){
this.fullSum += num;
}
}
//Get the sum of the progressions of 3 and 5 and remove the sum of progressions of the overlap
this.fullSum = this.sumOfProgression(3) + this.sumOfProgression(5) - this.sumOfProgression(3 * 5);
//Stop the timer
this.timer.stop();
@@ -68,6 +62,12 @@ export class Problem1 extends Problem{
super.reset();
this.fullSum = 0;
}
//Gets the sum of the progression of the multiple
private sumOfProgression(multiple: number): number{
let numTerms = Math.floor(Problem1.TOP_NUM / multiple); //Get the sum of the progressions of 3 and 5 and remove the sum of progressions of the overlap
//The sum of progression formula is (n / 2)(a + l). n = number of terms, a = multiple, l = last term
return ((numTerms / 2) * (multiple + (numTerms * multiple)))
}
//Gets
//Returns the result of solving the problem
public getResult(): string{
@@ -86,5 +86,6 @@ export class Problem1 extends Problem{
}
/* Results:
The sum of all numbers < 1000 is 233168
It took an average of 1.921 microseconds to run this problem through 100 iterations
*/