Added solution for problem 7

This commit is contained in:
2020-08-23 15:41:07 -04:00
parent b69f87cb35
commit c4d8624017
2 changed files with 85 additions and 1 deletions

View File

@@ -30,7 +30,7 @@ namespace ProjectEulerCS{
public class ProblemSelection{ public class ProblemSelection{
//Holds the valid problem numbers //Holds the valid problem numbers
private static readonly List<int> _PROBLEM_NUMBERS = new List<int>() private static readonly List<int> _PROBLEM_NUMBERS = new List<int>()
{0, 1, 2, 3, 4, 5, 6}; {0, 1, 2, 3, 4, 5, 6, 7};
public static System.Collections.Generic.List<int> PROBLEM_NUMBERS{ public static System.Collections.Generic.List<int> PROBLEM_NUMBERS{
get { return _PROBLEM_NUMBERS; } get { return _PROBLEM_NUMBERS; }
} }
@@ -45,6 +45,7 @@ namespace ProjectEulerCS{
case 4: problem = new Problem4(); break; case 4: problem = new Problem4(); break;
case 5: problem = new Problem5(); break; case 5: problem = new Problem5(); break;
case 6: problem = new Problem6(); break; case 6: problem = new Problem6(); break;
case 7: problem = new Problem7(); break;
} }
return problem; return problem;
} }

View File

@@ -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 <https://www.gnu.org/licenses/>.
*/
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<long> 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<long>();
}
//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
*/