diff --git a/ProjectEulerCS/ProblemSelection.cs b/ProjectEulerCS/ProblemSelection.cs index b485ad1..1f70c22 100644 --- a/ProjectEulerCS/ProblemSelection.cs +++ b/ProjectEulerCS/ProblemSelection.cs @@ -27,7 +27,7 @@ using System.Collections.Generic; namespace ProjectEulerCS{ public class ProblemSelection{ //Holds the valid problem numbers - private static readonly List _PROBLEM_NUMBERS = new List() { 0, 1, 2 }; + private static readonly List _PROBLEM_NUMBERS = new List() { 0, 1, 2, 3 }; public static System.Collections.Generic.List PROBLEM_NUMBERS{ get { return _PROBLEM_NUMBERS; } } @@ -38,6 +38,7 @@ namespace ProjectEulerCS{ switch(problemNumber){ case 1: problem = new Problem1(); break; case 2: problem = new Problem2(); break; + case 3: problem = new Problem3(); break; } return problem; } diff --git a/ProjectEulerCS/Problems/Problem3.cs b/ProjectEulerCS/Problems/Problem3.cs new file mode 100644 index 0000000..4207f04 --- /dev/null +++ b/ProjectEulerCS/Problems/Problem3.cs @@ -0,0 +1,95 @@ +//ProjectEuler/ProjectEulerCS/src/Problems/Problem3.cs +//Matthew Ellison +// Created: 08-23-20 +//Modified: 08-23-20 +//The largest prime factor of 600851475143 +//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 . +*/ + + +using System.Collections.Generic; + + +namespace ProjectEulerCS{ + public class Problem3 : Problem{ + //Variables + //Static variables + private const long GOAL_NUMBER = 600851475143L; //The number that needs factored + public long goalNumber{ + get{ return GOAL_NUMBER; } + } + //Instance variables + private List _factors; //Holds the factors of goalNumber + public List factors{ + get{ + if(!solved){ + throw new Unsolved(); + } + return _factors; + } + } + public long largestFactor{ + get{ + if(!solved){ + throw new Unsolved(); + } + return _factors[_factors.Count - 1]; + } + } + + //Functions + //Constructor + public Problem3() : base("What is the largest prime factor of 600851475143?"){ + _factors = new List(); + } + //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; + } + + //Star the timer + _timer.start(); + + //Get all the factors of the number + _factors = mee.Algorithms.getFactors(GOAL_NUMBER); + //THe last element should be the largest factor + + //Stop the timer + _timer.stop(); + + //Throw a flag to show the problem is solved + solved = true; + + //Save the results + _result = "The largest factor of the number " + GOAL_NUMBER + " is " + factors[factors.Count - 1]; + } + //Reset the porblem so it can be run again + public override void reset(){ + base.reset(); + _factors.Clear(); + } + } +} + +/*Results: +The largest factor of the number 600851475143 is 6857 +It took an average of 47.412 milliseconds to run this problem through 100 iterations +*/