mirror of
https://bitbucket.org/Mattrixwv/projecteulercs.git
synced 2025-12-06 17:23:57 -05:00
Added solution to problem 5
This commit is contained in:
@@ -27,7 +27,8 @@ using System.Collections.Generic;
|
||||
namespace ProjectEulerCS{
|
||||
public class Benchmark{
|
||||
private enum BenchmarkOptions { runSpecific = 1, runAllShort, runAll, exit, size };
|
||||
private static readonly List<int> tooLong = new List<int>();
|
||||
private static readonly List<int> tooLong = new List<int>()
|
||||
{5};
|
||||
|
||||
//The driver function for the benchmark selection
|
||||
public static void benchmarkMenu(){
|
||||
|
||||
@@ -28,7 +28,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};
|
||||
{0, 1, 2, 3, 4, 5};
|
||||
public static System.Collections.Generic.List<int> PROBLEM_NUMBERS{
|
||||
get { return _PROBLEM_NUMBERS; }
|
||||
}
|
||||
@@ -41,6 +41,7 @@ namespace ProjectEulerCS{
|
||||
case 2: problem = new Problem2(); break;
|
||||
case 3: problem = new Problem3(); break;
|
||||
case 4: problem = new Problem4(); break;
|
||||
case 5: problem = new Problem5(); break;
|
||||
}
|
||||
return problem;
|
||||
}
|
||||
|
||||
97
ProjectEulerCS/Problems/Problem5.cs
Normal file
97
ProjectEulerCS/Problems/Problem5.cs
Normal file
@@ -0,0 +1,97 @@
|
||||
//ProjectEuler/ProjectEulerCS/src/Problems/Problem5.cs
|
||||
//Matthew Ellison
|
||||
// Created: 08-23-20
|
||||
//Modified: 08-23-20
|
||||
//What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
|
||||
//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{
|
||||
public class Problem5 : Problem{
|
||||
//Variables
|
||||
//Instance variables
|
||||
private int smallestNum; //The smallest number that is found
|
||||
public int number{
|
||||
get{
|
||||
if(!solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
return smallestNum;
|
||||
}
|
||||
}
|
||||
|
||||
//Functions
|
||||
//Constructor
|
||||
public Problem5() : base("What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?"){
|
||||
smallestNum = 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();
|
||||
|
||||
//Start at 20 because it must at least be divisible by 20. Increment by 2 because it must be an even number to be divisible by 2
|
||||
bool numFound = false; //A flag for finding the divisible number
|
||||
int currentNum = 20; //The number that it are currently checking against
|
||||
while((currentNum > 0) && (!numFound)){
|
||||
//Start by assuming you found the number (because we throw a flag if we didn't find it)
|
||||
numFound = true;
|
||||
//Step through every number from 1-20 seeing if the current number is divisible by it
|
||||
for(int divisor = 1;divisor <= 20;++divisor){
|
||||
//If it is not divisible then throw a flag and start looking at the next number
|
||||
if((currentNum % divisor) != 0){
|
||||
numFound = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
//If you didn't find the correct number then increment by 2
|
||||
if(!numFound){
|
||||
currentNum += 2;
|
||||
}
|
||||
}
|
||||
|
||||
smallestNum = currentNum;
|
||||
|
||||
//Stop the timer
|
||||
_timer.stop();
|
||||
|
||||
//Throw a flag to show the problem is solved
|
||||
solved = true;
|
||||
|
||||
//Save the results
|
||||
_result = "The smallest positive number evenly divisible by all numbers 1-20 is " + currentNum;
|
||||
}
|
||||
//Reset the problem so it can be run again
|
||||
public override void reset(){
|
||||
base.reset();
|
||||
smallestNum = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Results:
|
||||
The smallest positive number evenly divisible by all numbers 1-20 is 232792560
|
||||
It took an average of 2.549 seconds to run this problem through 100 iterations
|
||||
*/
|
||||
Reference in New Issue
Block a user