From 45ff536134850dca220f6b9da6f7089d24348361 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Wed, 10 Mar 2021 10:51:52 -0500 Subject: [PATCH] Added solution to problem 6 --- ProblemSelection.ts | 8 ++-- Problems/Problem6.ts | 110 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 Problems/Problem6.ts diff --git a/ProblemSelection.ts b/ProblemSelection.ts index 04fff8c..7ef1cdc 100644 --- a/ProblemSelection.ts +++ b/ProblemSelection.ts @@ -1,11 +1,11 @@ //ProjectEulerTS/ProblemSelection.ts //Matthew Ellison // Created: 10-18-20 -//Modified: 10-18-20 +//Modified: 03-10-21 //This class holds all of the functions needed to handle a problem //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 it under the terms of the GNU Lesser General Public License as published by @@ -29,11 +29,12 @@ import { Problem2 } from "./Problems/Problem2"; import { Problem3 } from "./Problems/Problem3"; import { Problem4 } from "./Problems/Problem4"; import { Problem5 } from "./Problems/Problem5"; +import { Problem6 } from "./Problems/Problem6"; export class ProblemSelection{ //Holds the valid problem numbers - public static PROBLEM_NUMBERS: number[] = [1, 2, 3, 4, 5]; + public static PROBLEM_NUMBERS: number[] = [1, 2, 3, 4, 5, 6]; //Returns the problem corresponding to the given problem number public static getProblem(problemNumber: number): Problem{ @@ -44,6 +45,7 @@ export class ProblemSelection{ case 3 : problem = new Problem3(); break; case 4 : problem = new Problem4(); break; case 5 : problem = new Problem5(); break; + case 6 : problem = new Problem6(); break; } return problem; } diff --git a/Problems/Problem6.ts b/Problems/Problem6.ts new file mode 100644 index 0000000..afd9d71 --- /dev/null +++ b/Problems/Problem6.ts @@ -0,0 +1,110 @@ +//ProjectEulerTS/Problems/Problem5.ts +//Matthew Ellison +// Created: 10-26-20 +//Modified: 10-26-20 +//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 +/* + 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 . +*/ + + +import { Problem } from "./Problem"; +import { Unsolved } from "../Unsolved"; + + +export class Problem6 extends Problem{ + //Variables + //Static variables + private static START_NUM: number = 1; //The first number that needs to be counted + private static END_NUM: number = 100; //The last number that needs to be counted + //Instance vairables + private sumOfSquares: number; //Holds the sum of the squares of all the numbers + private squareOfSum: number; //Holds the square of the sum of all the numbers + + //Functions + //Constructor + public constructor(){ + super(`Find the difference between the sum of the squares and the square of the sum of the numbers ${Problem6.START_NUM}-${Problem6.END_NUM}`); + this.sumOfSquares = 0; + this.squareOfSum = 0; + } + //Operational functions + //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(); + + //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 + this.squareOfSum += currentNum; //Add the number to the sum to be squared later + } + //Square the sum of all the numbers + this.squareOfSum *= this.squareOfSum; + + //Stop the timer + this.timer.stop(); + + //Throw a flag to show the problem is solved + this.solved = true; + } + //Reset the problem so it can be run again + public reset(): void{ + super.reset(); + this.squareOfSum = 0; + this.sumOfSquares = 0; + } + //Gets + //Returns the result of solving the problem + public getResult(): string{ + if(!this.solved){ + 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()}`; + } + //Returns the sum of all the squares + public getSumOfSquares(): number{ + if(!this.solved){ + throw new Unsolved(); + } + return this.sumOfSquares; + } + //Returns the square of all the sums + public getSquareOfSum(){ + if(!this.solved){ + throw new Unsolved(); + } + return this.squareOfSum; + } + //Returns the requested difference + public getDifference(): number{ + if(!this.solved){ + throw new Unsolved(); + } + 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 +*/