diff --git a/ProjectEulerCS/ProblemSelection.cs b/ProjectEulerCS/ProblemSelection.cs index b06700b..17beb91 100644 --- a/ProjectEulerCS/ProblemSelection.cs +++ b/ProjectEulerCS/ProblemSelection.cs @@ -30,7 +30,7 @@ namespace ProjectEulerCS{ public class ProblemSelection{ //Holds the valid problem numbers private static readonly List _PROBLEM_NUMBERS = new List() - {0, 1, 2, 3, 4, 5, 6}; + {0, 1, 2, 3, 4, 5, 6, 7}; public static System.Collections.Generic.List PROBLEM_NUMBERS{ get { return _PROBLEM_NUMBERS; } } @@ -45,6 +45,7 @@ namespace ProjectEulerCS{ case 4: problem = new Problem4(); break; case 5: problem = new Problem5(); break; case 6: problem = new Problem6(); break; + case 7: problem = new Problem7(); break; } return problem; } diff --git a/ProjectEulerCS/Problems/Problem7.cs b/ProjectEulerCS/Problems/Problem7.cs new file mode 100644 index 0000000..fe38101 --- /dev/null +++ b/ProjectEulerCS/Problems/Problem7.cs @@ -0,0 +1,83 @@ +//ProjectEuler/ProjectEulerCS/src/Problems/Problem7.cs +//Matthew Ellison +// Created: 08-23-20 +//Modified: 08-23-20 +//What is the 10001th prime number? +//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.Problems{ + public class Problem7 : Problem{ + //Variables + //Static variables + private const long NUMBER_OF_PRIMES = 10001; //The number of primes we are trying to get + //Instance variables + private List primes; + public long prime{ + get{ + if(!solved){ + throw new Unsolved(); + } + return primes[primes.Count - 1]; + } + } + + //Functions + //Constructor + public Problem7() : base("What is the 10001th prime number?"){ + primes = new List(); + } + //Operatinal 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(); + + //Setup the variables + primes = mee.Algorithms.getNumPrimes(NUMBER_OF_PRIMES); //Holds the prime numbers + + //Stop the timer + _timer.stop(); + + //Throw a flag to show the problem is solved + solved = true; + + //Save the results + _result = "The " + NUMBER_OF_PRIMES + "th prime number is " + primes[primes.Count - 1]; + } + //Reset the problem so it can be run again + public override void reset(){ + base.reset(); + primes.Clear(); + } + } +} + +/* Results: +The 10001th prime number is 104743 +It took an average of 3.559 milliseconds to run this problem through 100 iterations +*/