Files
ProjectEulerCS/ProjectEulerCS/Problems/Problem1.cs

86 lines
2.4 KiB
C#

//ProjectEuler/ProjectEulerCS/src/Problems/Problem1.cs
//Matthew Ellison
// Created: 08-14-20
//Modified: 08-14-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/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 Problem1 : Problem{
//Variables
private const int TOP_NUM = 999; //The largest number to tbe checked
//Instance variables
private int fullSum; //The sum of all the numbers
public int sum{
get{
if(!solved){
throw new Unsolved();
}
return fullSum;
}
}
//Functions
//Constructor
public Problem1() : base("What is the sum of all the multiples of 3 or 5 that are less than 1000"){
fullSum = 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();
//Check every number < 1000 to see if it is a multiple of 3 or 5. If it is add it to the running sum
for (int cnt = 1; cnt <= TOP_NUM; ++cnt){
if ((cnt % 3) == 0){
fullSum += cnt;
}
else if ((cnt % 5) == 0){
fullSum += cnt;
}
}
//Stop the timer
_timer.stop();
//Thow a flag to show the problem is solved
solved = true;
//Save the results
_result = "The sum of all numbers < " + (TOP_NUM + 1) + " is " + fullSum;
}
//Reset the problem so it can be run again
public override void reset(){
base.reset();
fullSum = 0;
}
}
}
/* Results:
The sum of all numbers < 1000 is 233168
It took an average of 1.430 microseconds to run this problem through 100 iterations
*/