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/Problem6.ts
|
||||
//Matthew Ellison
|
||||
// Created: 03-10-21
|
||||
//Modified: 03-10-21
|
||||
//Modified: 07-14-21
|
||||
//Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/typescriptClasses
|
||||
/*
|
||||
@@ -23,7 +23,6 @@
|
||||
|
||||
|
||||
import { Problem } from "./Problem";
|
||||
import { Unsolved } from "../Unsolved";
|
||||
|
||||
|
||||
export class Problem6 extends Problem{
|
||||
@@ -53,6 +52,7 @@ export class Problem6 extends Problem{
|
||||
//Start the timer
|
||||
this.timer.start();
|
||||
|
||||
|
||||
//Run through all numbers and add them to the appropriate sums
|
||||
for(let currentNum: number = Problem6.START_NUM;currentNum <= Problem6.END_NUM;++currentNum){
|
||||
this.sumOfSquares += (currentNum * currentNum); //Square the number and add it to the variable
|
||||
@@ -61,6 +61,7 @@ export class Problem6 extends Problem{
|
||||
//Square the sum of all the numbers
|
||||
this.squareOfSum *= this.squareOfSum;
|
||||
|
||||
|
||||
//Stop the timer
|
||||
this.timer.stop();
|
||||
|
||||
@@ -76,34 +77,27 @@ export class Problem6 extends Problem{
|
||||
//Gets
|
||||
//Returns the result of solving the problem
|
||||
public getResult(): string{
|
||||
if(!this.solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
this.solvedCheck("result");
|
||||
return `The difference between the sum of the squares and the square of the sum of all numbers from ${Problem6.START_NUM}-${Problem6.END_NUM} is ${this.getDifference()}`;
|
||||
}
|
||||
//Returns the sum of all the squares
|
||||
public getSumOfSquares(): number{
|
||||
if(!this.solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
this.solvedCheck("sum of the squares");
|
||||
return this.sumOfSquares;
|
||||
}
|
||||
//Returns the square of all the sums
|
||||
public getSquareOfSum(){
|
||||
if(!this.solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
this.solvedCheck("square of the sums");
|
||||
return this.squareOfSum;
|
||||
}
|
||||
//Returns the requested difference
|
||||
public getDifference(): number{
|
||||
if(!this.solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
this.solvedCheck("difference between the two numbers");
|
||||
return Math.abs(this.sumOfSquares - this.squareOfSum);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Results:
|
||||
The difference between the sum of the squares and the square of the sum of all numbers from 1-100 is 25164150
|
||||
It took an average of 344.001 nanoseconds to run this problem through 100 iterations
|
||||
|
||||
Reference in New Issue
Block a user