From 63591f9f914c81a23d4f182e9125a08667bb4c9d Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Sun, 23 Aug 2020 13:33:05 -0400 Subject: [PATCH] Added solution to problem 5 --- ProjectEulerCS/Benchmark.cs | 3 +- ProjectEulerCS/ProblemSelection.cs | 3 +- ProjectEulerCS/Problems/Problem5.cs | 97 +++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 ProjectEulerCS/Problems/Problem5.cs diff --git a/ProjectEulerCS/Benchmark.cs b/ProjectEulerCS/Benchmark.cs index d67d0e8..701da8d 100644 --- a/ProjectEulerCS/Benchmark.cs +++ b/ProjectEulerCS/Benchmark.cs @@ -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 tooLong = new List(); + private static readonly List tooLong = new List() + {5}; //The driver function for the benchmark selection public static void benchmarkMenu(){ diff --git a/ProjectEulerCS/ProblemSelection.cs b/ProjectEulerCS/ProblemSelection.cs index 7e072a3..d2dbf1b 100644 --- a/ProjectEulerCS/ProblemSelection.cs +++ b/ProjectEulerCS/ProblemSelection.cs @@ -28,7 +28,7 @@ namespace ProjectEulerCS{ public class ProblemSelection{ //Holds the valid problem numbers private static readonly List _PROBLEM_NUMBERS = new List() - {0, 1, 2, 3, 4}; + {0, 1, 2, 3, 4, 5}; public static System.Collections.Generic.List 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; } diff --git a/ProjectEulerCS/Problems/Problem5.cs b/ProjectEulerCS/Problems/Problem5.cs new file mode 100644 index 0000000..b19b915 --- /dev/null +++ b/ProjectEulerCS/Problems/Problem5.cs @@ -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 . +*/ + + +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 +*/