Added solution to problem 6

This commit is contained in:
2020-08-23 14:39:45 -04:00
parent 38701d36d2
commit b69f87cb35
2 changed files with 107 additions and 1 deletions

View File

@@ -30,7 +30,7 @@ namespace ProjectEulerCS{
public class ProblemSelection{
//Holds the valid problem numbers
private static readonly List<int> _PROBLEM_NUMBERS = new List<int>()
{0, 1, 2, 3, 4, 5};
{0, 1, 2, 3, 4, 5, 6};
public static System.Collections.Generic.List<int> PROBLEM_NUMBERS{
get { return _PROBLEM_NUMBERS; }
}
@@ -44,6 +44,7 @@ namespace ProjectEulerCS{
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;
}

View File

@@ -0,0 +1,105 @@
//ProjectEuler/ProjectEulerCS/src/Problems/Problem6.cs
//Matthew Ellison
// Created: 08-23-20
//Modified: 08-23-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/CSClasses
/*
Copyright (C) 2020 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/>.
*/
namespace ProjectEulerCS.Problems{
public class Problem6 : Problem{
//Variables
//Static variables
private const int START_NUM = 1; //The first number that needs to be counted
private const int END_NUM = 100; //The last number that needs to be counted
//Instance variables
private long _sumOfSquares; //Holds the sum of the squares of all the numbers
public long sumOfSquares{
get{
if(!solved){
throw new Unsolved();
}
return _sumOfSquares;
}
}
private long _squareOfSum; //Holds the square of the sum of all the numbers
public long squareOfSum{
get{
if(!solved){
throw new Unsolved();
}
return _squareOfSum;
}
}
public long difference{
get{
if(!solved){
throw new Unsolved();
}
return System.Math.Abs(_sumOfSquares - _squareOfSum);
}
}
//Functions
//Constructor
public Problem6() : base("Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum."){
_sumOfSquares = 0;
_squareOfSum = 0;
}
//Operational functions
//Solve the problem
public override void solve(){
//If the problem has already been solved do nothing and end the function
if(solved){
return;
}
//Start the timer
_timer.start();
//Run through all numbers and add them to the appropriate sums
for(int currentNum = START_NUM;currentNum <= END_NUM;++currentNum){
_sumOfSquares += (currentNum * currentNum); //Add the square to the correct variable
_squareOfSum += currentNum; //Add the number to the correct variable to squaring later
}
//Squaring the sum that needs it
_squareOfSum *= _squareOfSum;
//Stop the timer
_timer.stop();
//Throw a flag to show the problem is solved
solved = true;
//Save the results
_result = "The difference between the sum of the squares and the square of the sum of all numbers from 1-100 is " + System.Math.Abs(_sumOfSquares - _squareOfSum);
}
//Reset the problem so it can be run again
public override void reset(){
base.reset();
_squareOfSum = 0;
_sumOfSquares = 0;
}
}
}
/* 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 234.000 nanoseconds to run this problem through 100 iterations
*/