Updated code to match C# conventions

This commit is contained in:
2020-08-24 14:15:34 -04:00
parent 7f748ef54f
commit c240878d96
16 changed files with 233 additions and 237 deletions

View File

@@ -26,27 +26,27 @@ using System.Collections.Generic;
namespace ProjectEulerCS{ namespace ProjectEulerCS{
public class Benchmark{ public class Benchmark{
private enum BenchmarkOptions { runSpecific = 1, runAllShort, runAll, exit, size }; 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}; {5};
//The driver function for the benchmark selection //The driver function for the benchmark selection
public static void benchmarkMenu(){ public static void BenchmarkMenu(){
BenchmarkOptions selection = BenchmarkOptions.size; BenchmarkOptions selection;
printMenu(); PrintMenu();
selection = getMenuSelection(); selection = GetMenuSelection();
switch(selection){ switch(selection){
case BenchmarkOptions.runSpecific: runSpecific(); break; case BenchmarkOptions.RunSpecific: RunSpecific(); break;
case BenchmarkOptions.runAllShort: runAllShort(); break; case BenchmarkOptions.RunAllShort: RunAllShort(); break;
case BenchmarkOptions.runAll: runAll(); break; case BenchmarkOptions.RunAll: RunAll(); break;
case BenchmarkOptions.exit: break; case BenchmarkOptions.Exit: break;
case BenchmarkOptions.size: break; case BenchmarkOptions.Size: break;
} }
} }
//Print the benchmark menu //Print the benchmark menu
private static void printMenu(){ private static void PrintMenu(){
System.Console.WriteLine("1. Run a specific problem"); System.Console.WriteLine("1. Run a specific problem");
System.Console.WriteLine("2. Run all problems that have a reasonably short run time"); System.Console.WriteLine("2. Run all problems that have a reasonably short run time");
System.Console.WriteLine("3. Run all problems"); System.Console.WriteLine("3. Run all problems");
@@ -54,20 +54,20 @@ namespace ProjectEulerCS{
System.Console.WriteLine(); System.Console.WriteLine();
} }
//Returns a valid menu option //Returns a valid menu option
private static BenchmarkOptions getMenuSelection(){ private static BenchmarkOptions GetMenuSelection(){
string selectionString = System.Console.ReadLine(); string selectionString = System.Console.ReadLine();
int selection = System.Convert.ToInt32(selectionString); int selection = System.Convert.ToInt32(selectionString);
while(!isValidMenu(selection)){ while(!IsValidMenu(selection)){
System.Console.WriteLine("That is an invalid option!\nPress Enter to continue"); System.Console.WriteLine("That is an invalid option!\nPress Enter to continue");
printMenu(); PrintMenu();
selectionString = System.Console.ReadLine(); selectionString = System.Console.ReadLine();
selection = System.Convert.ToInt32(selectionString); selection = System.Convert.ToInt32(selectionString);
} }
return getSelection(selection); return GetSelection(selection);
} }
//Determines if a value is a valid menu option. Helper for getBenchmarMenuSeleciton //Determines if a value is a valid menu option. Helper for getBenchmarMenuSeleciton
private static bool isValidMenu(int selection){ private static bool IsValidMenu(int selection){
if((selection >= (int)BenchmarkOptions.runSpecific) && (selection < (int)BenchmarkOptions.size)){ if((selection >= (int)BenchmarkOptions.RunSpecific) && (selection < (int)BenchmarkOptions.Size)){
return true; return true;
} }
else{ else{
@@ -75,39 +75,37 @@ namespace ProjectEulerCS{
} }
} }
//A helper function for getMenuSelection that turns an integer to a BenchmarkOptions //A helper function for getMenuSelection that turns an integer to a BenchmarkOptions
private static BenchmarkOptions getSelection(int selection){ private static BenchmarkOptions GetSelection(int selection){
BenchmarkOptions sel = BenchmarkOptions.size; BenchmarkOptions sel = selection switch{
1 => BenchmarkOptions.RunSpecific,
switch(selection){ 2 => BenchmarkOptions.RunAllShort,
case 1: sel = BenchmarkOptions.runSpecific; break; 3 => BenchmarkOptions.RunAll,
case 2: sel = BenchmarkOptions.runAllShort; break; 4 => BenchmarkOptions.Exit,
case 3: sel = BenchmarkOptions.runAll; break; _ => BenchmarkOptions.Size,
case 4: sel = BenchmarkOptions.exit; break; };
default: sel = BenchmarkOptions.size; break;
}
return sel; return sel;
} }
//Determines which problem user wants to run and runs it //Determines which problem user wants to run and runs it
private static void runSpecific(){ private static void RunSpecific(){
//Ask which problem the user wants to run //Ask which problem the user wants to run
int problemNumber = ProblemSelection.getProblemNumber(); int problemNumber = ProblemSelection.GetProblemNumber();
//Ask how many times to run the problem //Ask how many times to run the problem
int timesToRun = getNumberOfTimesToRun(); int timesToRun = GetNumberOfTimesToRun();
//Get the problem and print its description //Get the problem and print its description
Problem problem = ProblemSelection.getProblem(problemNumber); Problem problem = ProblemSelection.GetProblem(problemNumber);
System.Console.WriteLine(problemNumber + ". " + problem.description); System.Console.WriteLine(problemNumber + ". " + problem.Description);
//Run the problem the specific number of times //Run the problem the specific number of times
decimal totalTime = runProblem(problem, timesToRun); decimal totalTime = RunProblem(problem, timesToRun);
//Print the results //Print the results
System.Console.WriteLine(getBenchmarkResults(problem, totalTime, timesToRun)); System.Console.WriteLine(GetBenchmarkResults(problem, totalTime, timesToRun));
} }
//Runs all problems except a few that are specified because of run length //Runs all problems except a few that are specified because of run length
private static void runAllShort(){ private static void RunAllShort(){
//Ask how many times to run the problems //Ask how many times to run the problems
int timesToRun = getNumberOfTimesToRun(); int timesToRun = GetNumberOfTimesToRun();
//Run through all valid problem numbers, skipping a few that are in the tooLong list //Run through all valid problem numbers, skipping a few that are in the tooLong list
for(int cnt = 1; cnt < ProblemSelection.PROBLEM_NUMBERS.Count; ++cnt){ for(int cnt = 1; cnt < ProblemSelection.PROBLEM_NUMBERS.Count; ++cnt){
@@ -119,39 +117,39 @@ namespace ProjectEulerCS{
} }
//Get the problem and print its description //Get the problem and print its description
Problem problem = ProblemSelection.getProblem(problemNumber); Problem problem = ProblemSelection.GetProblem(problemNumber);
System.Console.WriteLine(problemNumber + ". " + problem.description); System.Console.WriteLine(problemNumber + ". " + problem.Description);
//Run each problem the specified number of times //Run each problem the specified number of times
decimal totalTime = runProblem(problem, timesToRun); decimal totalTime = RunProblem(problem, timesToRun);
//Print the results //Print the results
System.Console.WriteLine(getBenchmarkResults(problem, totalTime, timesToRun)); System.Console.WriteLine(GetBenchmarkResults(problem, totalTime, timesToRun));
} }
} }
//Runs all problems //Runs all problems
private static void runAll(){ private static void RunAll(){
//Ask how many times to run the problem //Ask how many times to run the problem
int timesToRun = getNumberOfTimesToRun(); int timesToRun = GetNumberOfTimesToRun();
//Run through all valid problem numbers, skipping a few that are in the tooLong list //Run through all valid problem numbers, skipping a few that are in the tooLong list
for(int cnt = 1; cnt < ProblemSelection.PROBLEM_NUMBERS.Count; ++cnt){ for(int cnt = 1; cnt < ProblemSelection.PROBLEM_NUMBERS.Count; ++cnt){
int problemNumber = ProblemSelection.PROBLEM_NUMBERS[cnt]; int problemNumber = ProblemSelection.PROBLEM_NUMBERS[cnt];
//Get the problem //Get the problem
Problem problem = ProblemSelection.getProblem(problemNumber); Problem problem = ProblemSelection.GetProblem(problemNumber);
//Run each problem the specified number of times //Run each problem the specified number of times
System.Console.WriteLine(problemNumber + ". " + problem.description); System.Console.WriteLine(problemNumber + ". " + problem.Description);
decimal totalTime = runProblem(problem, timesToRun); decimal totalTime = RunProblem(problem, timesToRun);
//Print the results //Print the results
System.Console.WriteLine(getBenchmarkResults(problem, totalTime, timesToRun)); System.Console.WriteLine(GetBenchmarkResults(problem, totalTime, timesToRun));
} }
} }
//Asks how many times a problem is supposed to run and returns the value //Asks how many times a problem is supposed to run and returns the value
private static int getNumberOfTimesToRun(){ private static int GetNumberOfTimesToRun(){
int numOfTimesToRun = 1; int numOfTimesToRun;
System.Console.Write("How many times do you want to run this problem? "); System.Console.Write("How many times do you want to run this problem? ");
string numOfTimesToRunString = System.Console.ReadLine(); string numOfTimesToRunString = System.Console.ReadLine();
numOfTimesToRun = System.Convert.ToInt32(numOfTimesToRunString); numOfTimesToRun = System.Convert.ToInt32(numOfTimesToRunString);
@@ -164,28 +162,28 @@ namespace ProjectEulerCS{
return numOfTimesToRun; return numOfTimesToRun;
} }
//Runs the problem the given number of times //Runs the problem the given number of times
private static decimal runProblem(Problem problem, int timesToRun){ private static decimal RunProblem(Problem problem, int timesToRun){
decimal totalTime = 0; decimal totalTime = 0;
System.Console.Write("Solving"); System.Console.Write("Solving");
for(int cnt = 0; cnt < timesToRun; ++cnt){ for(int cnt = 0; cnt < timesToRun; ++cnt){
System.Console.Write('.'); System.Console.Write('.');
//Reset the data so you care actually counting the run time an additional time //Reset the data so you care actually counting the run time an additional time
problem.reset(); problem.Reset();
//Solve the problem //Solve the problem
problem.solve(); problem.Solve();
//Get the time data //Get the time data
totalTime += problem.timer.getNano(); totalTime += problem.Timer.GetNano();
} }
return totalTime; return totalTime;
} }
//Prints the benchmark results of a problem //Prints the benchmark results of a problem
private static string getBenchmarkResults(Problem problem, decimal totalTime, int timesRun){ private static string GetBenchmarkResults(Problem problem, decimal totalTime, int timesRun){
//Calculate the average run time of the problem //Calculate the average run time of the problem
totalTime /= timesRun; totalTime /= timesRun;
string timeResults = mee.Stopwatch.getStr(totalTime); string timeResults = mee.Stopwatch.GetStr(totalTime);
//Tally the results //Tally the results
string results = "\n\n" + problem.result + "\nIt took an average of " + timeResults + " to run this problem through " + timesRun + " iterations\n\n"; string results = "\n\n" + problem.Result + "\nIt took an average of " + timeResults + " to run this problem through " + timesRun + " iterations\n\n";
return results; return results;
} }
} }

View File

@@ -11,7 +11,7 @@ namespace ProjectEulerCS{
//Instance variables //Instance variables
//protected const Stopwatch timer = new Stopwatch(); //To time how long it takes to run the algorithm //protected const Stopwatch timer = new Stopwatch(); //To time how long it takes to run the algorithm
protected string _result = null; //Holds the results of the problem protected string _result = null; //Holds the results of the problem
public string result{ public string Result{
get{ get{
if(!solved){ if(!solved){
throw new Unsolved(); throw new Unsolved();
@@ -20,12 +20,12 @@ namespace ProjectEulerCS{
} }
} }
protected readonly string _description; //Holds the description of the problem protected readonly string _description; //Holds the description of the problem
public string description{ public string Description{
get{ return _description; } get{ return _description; }
} }
protected bool solved; //Shows whether the problem has already been solved protected bool solved; //Shows whether the problem has already been solved
protected mee.Stopwatch _timer = new mee.Stopwatch(); protected mee.Stopwatch _timer = new mee.Stopwatch();
public mee.Stopwatch timer{ public mee.Stopwatch Timer{
get{ get{
if(!solved){ if(!solved){
throw new Unsolved(); throw new Unsolved();
@@ -33,12 +33,12 @@ namespace ProjectEulerCS{
return _timer; return _timer;
} }
} }
public string time{ public string Time{
get{ get{
if(!solved){ if(!solved){
throw new Unsolved(); throw new Unsolved();
} }
return _timer.getStr(); return _timer.GetStr();
} }
} }
@@ -50,10 +50,10 @@ namespace ProjectEulerCS{
//Operations functions //Operations functions
//Solve the problem //Solve the problem
public abstract void solve(); public abstract void Solve();
//Reset the problem so it can be run again //Reset the problem so it can be run again
public virtual void reset(){ public virtual void Reset(){
_timer.reset(); _timer.Reset();
solved = false; solved = false;
_result = null; _result = null;
} }

View File

@@ -37,7 +37,7 @@ namespace ProjectEulerCS{
} }
//Returns the problem corresponding to the given problem number //Returns the problem corresponding to the given problem number
public static Problem getProblem(int problemNumber){ public static Problem GetProblem(int problemNumber){
Problem problem = null; Problem problem = null;
switch(problemNumber){ switch(problemNumber){
case 1: problem = new Problem1(); break; case 1: problem = new Problem1(); break;
@@ -56,26 +56,26 @@ namespace ProjectEulerCS{
return problem; return problem;
} }
//Print the description of a problem //Print the description of a problem
public static void printDescription(int problemNumber){ public static void PrintDescription(int problemNumber){
//Get the problem //Get the problem
Problem problem = getProblem(problemNumber); Problem problem = GetProblem(problemNumber);
//Print the problem's description //Print the problem's description
System.Console.WriteLine(problem.description); System.Console.WriteLine(problem.Description);
} }
//Solve a problem //Solve a problem
public static void solveProblem(int problemNumber){ public static void SolveProblem(int problemNumber){
//Get the problem //Get the problem
Problem problem = getProblem(problemNumber); Problem problem = GetProblem(problemNumber);
//Print the problem description //Print the problem description
System.Console.WriteLine(problem.description); System.Console.WriteLine(problem.Description);
//Solve the problem //Solve the problem
problem.solve(); problem.Solve();
//Print the results //Print the results
System.Console.WriteLine(problem.result + "\nIt took " + problem.time + " to solve this problem.\n\n"); System.Console.WriteLine(problem.Result + "\nIt took " + problem.Time + " to solve this problem.\n\n");
} }
//Get a valid problem number from a user //Get a valid problem number from a user
public static int getProblemNumber(){ public static int GetProblemNumber(){
int problemNumber = 0; int problemNumber;
System.Console.Write("Enter a problem number: "); System.Console.Write("Enter a problem number: ");
string problemNumberString = System.Console.ReadLine(); string problemNumberString = System.Console.ReadLine();
problemNumber = System.Convert.ToInt32(problemNumberString); problemNumber = System.Convert.ToInt32(problemNumberString);
@@ -87,7 +87,7 @@ namespace ProjectEulerCS{
return problemNumber; return problemNumber;
} }
//List all valid problem numbers //List all valid problem numbers
public static void listProblems(){ public static void ListProblems(){
System.Console.Write(_PROBLEM_NUMBERS[1]); System.Console.Write(_PROBLEM_NUMBERS[1]);
for(int problemNumber = 2; problemNumber < _PROBLEM_NUMBERS.Count; ++problemNumber){ for(int problemNumber = 2; problemNumber < _PROBLEM_NUMBERS.Count; ++problemNumber){
System.Console.Write(", " + _PROBLEM_NUMBERS[problemNumber]); System.Console.Write(", " + _PROBLEM_NUMBERS[problemNumber]);

View File

@@ -28,7 +28,7 @@ namespace ProjectEulerCS.Problems{
private const int TOP_NUM = 999; //The largest number to tbe checked private const int TOP_NUM = 999; //The largest number to tbe checked
//Instance variables //Instance variables
private int fullSum; //The sum of all the numbers private int fullSum; //The sum of all the numbers
public int sum{ public int Sum{
get{ get{
if(!solved){ if(!solved){
throw new Unsolved(); throw new Unsolved();
@@ -44,14 +44,14 @@ namespace ProjectEulerCS.Problems{
} }
//Operational functions //Operational functions
//Solve the problem //Solve the problem
public override void solve(){ public override void Solve(){
//If the problem has already been solved do nothing and end the function //If the problem has already been solved do nothing and end the function
if (solved){ if (solved){
return; return;
} }
//Start the timer //Start the timer
_timer.start(); _timer.Start();
//Check every number < 1000 to see if it is a multiple of 3 or 5. If it is add it to the running sum //Check every number < 1000 to see if it is a multiple of 3 or 5. If it is add it to the running sum
for (int cnt = 1; cnt <= TOP_NUM; ++cnt){ for (int cnt = 1; cnt <= TOP_NUM; ++cnt){
@@ -64,7 +64,7 @@ namespace ProjectEulerCS.Problems{
} }
//Stop the timer //Stop the timer
_timer.stop(); _timer.Stop();
//Thow a flag to show the problem is solved //Thow a flag to show the problem is solved
solved = true; solved = true;
@@ -73,8 +73,8 @@ namespace ProjectEulerCS.Problems{
_result = "The sum of all numbers < " + (TOP_NUM + 1) + " is " + fullSum; _result = "The sum of all numbers < " + (TOP_NUM + 1) + " is " + fullSum;
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
public override void reset(){ public override void Reset(){
base.reset(); base.Reset();
fullSum = 0; fullSum = 0;
} }
} }

View File

@@ -31,37 +31,37 @@ namespace ProjectEulerCS{
//Static variables //Static variables
private const long GOAL_NUMBER = 2000000 - 1; private const long GOAL_NUMBER = 2000000 - 1;
//Instance variables //Instance variables
private long _sum; //The sum of all the prime numbers private long sum; //The sum of all the prime numbers
public long sum{ public long Sum{
get{ get{
if(!solved){ if(!solved){
throw new Unsolved(); throw new Unsolved();
} }
return _sum; return sum;
} }
} }
//Functions //Functions
//Constructor //Constructor
public Problem10() : base("Find the sum of all the primes below two million"){ public Problem10() : base("Find the sum of all the primes below two million"){
_sum = 0; sum = 0;
} }
//Operational functions //Operational functions
//Solve the problem //Solve the problem
public override void solve(){ public override void Solve(){
//If the problem has already been solved do nothing and end the function //If the problem has already been solved do nothing and end the function
if(solved){ if(solved){
return; return;
} }
//Start the timer //Start the timer
_timer.start(); _timer.Start();
//Get the sum of all prime numbers < GOAL_NUMBER //Get the sum of all prime numbers < GOAL_NUMBER
long sum = mee.Algorithms.getPrimes(GOAL_NUMBER).Sum(); long sum = mee.Algorithms.GetPrimes(GOAL_NUMBER).Sum();
//Stop the timer //Stop the timer
_timer.stop(); _timer.Stop();
//Throw a flag to show the problem is solved //Throw a flag to show the problem is solved
solved = true; solved = true;
@@ -70,9 +70,9 @@ namespace ProjectEulerCS{
_result = "The sum of all the primes < " + (GOAL_NUMBER + 1) + " is " + sum; _result = "The sum of all the primes < " + (GOAL_NUMBER + 1) + " is " + sum;
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
public override void reset(){ public override void Reset(){
base.reset(); base.Reset();
_sum = 0; sum = 0;
} }
} }
} }

View File

@@ -75,7 +75,7 @@ namespace ProjectEulerCS.Problems{
{01, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 01, 89, 19, 67, 48}}; {01, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 01, 89, 19, 67, 48}};
//Instance variables //Instance variables
private List<int> greatestProduct; //Holds the largest product we have found so far private List<int> greatestProduct; //Holds the largest product we have found so far
public List<int> numbers{ public List<int> Numbers{
get{ get{
if(!solved){ if(!solved){
throw new Unsolved(); throw new Unsolved();
@@ -83,12 +83,12 @@ namespace ProjectEulerCS.Problems{
return greatestProduct; return greatestProduct;
} }
} }
public int product{ public int Product{
get{ get{
if(!solved){ if(!solved){
throw new Unsolved(); throw new Unsolved();
} }
return mee.Algorithms.getProd(greatestProduct); return mee.Algorithms.GetProd(greatestProduct);
} }
} }
@@ -99,7 +99,7 @@ namespace ProjectEulerCS.Problems{
} }
//Operational functions //Operational functions
//Solve the problem //Solve the problem
public override void solve(){ public override void Solve(){
//If the problem has already been solved do nothing and end the function //If the problem has already been solved do nothing and end the function
if(solved){ if(solved){
return; return;
@@ -109,7 +109,7 @@ namespace ProjectEulerCS.Problems{
List<int> currentProduct = new List<int>() {0, 0, 0, 0}; List<int> currentProduct = new List<int>() {0, 0, 0, 0};
//Start the timer //Start the timer
_timer.start(); _timer.Start();
//Loop through every row and column //Loop through every row and column
for(int row = 0;row < grid.GetLength(0);++row){ for(int row = 0;row < grid.GetLength(0);++row){
@@ -140,7 +140,7 @@ namespace ProjectEulerCS.Problems{
currentProduct[3] = grid[row, col + 3]; currentProduct[3] = grid[row, col + 3];
//If the current number's product is greater than the greatest product replace it //If the current number's product is greater than the greatest product replace it
if(mee.Algorithms.getProd(currentProduct) > mee.Algorithms.getProd(greatestProduct)){ if(mee.Algorithms.GetProd(currentProduct) > mee.Algorithms.GetProd(greatestProduct)){
greatestProduct = currentProduct.ToList(); greatestProduct = currentProduct.ToList();
} }
} }
@@ -153,7 +153,7 @@ namespace ProjectEulerCS.Problems{
currentProduct[3] = grid[row + 3, col]; currentProduct[3] = grid[row + 3, col];
//If the current number's product is greater than the greatest product replace it //If the current number's product is greater than the greatest product replace it
if(mee.Algorithms.getProd(currentProduct) > mee.Algorithms.getProd(greatestProduct)){ if(mee.Algorithms.GetProd(currentProduct) > mee.Algorithms.GetProd(greatestProduct)){
greatestProduct = currentProduct.ToList(); greatestProduct = currentProduct.ToList();
} }
} }
@@ -166,7 +166,7 @@ namespace ProjectEulerCS.Problems{
currentProduct[3] = grid[row + 3, col - 3]; currentProduct[3] = grid[row + 3, col - 3];
//If the current number's product is greater than the greatest product replace it //If the current number's product is greater than the greatest product replace it
if(mee.Algorithms.getProd(currentProduct) > mee.Algorithms.getProd(greatestProduct)){ if(mee.Algorithms.GetProd(currentProduct) > mee.Algorithms.GetProd(greatestProduct)){
greatestProduct = currentProduct.ToList(); greatestProduct = currentProduct.ToList();
} }
} }
@@ -179,7 +179,7 @@ namespace ProjectEulerCS.Problems{
currentProduct[3] = grid[row + 3, col + 3]; currentProduct[3] = grid[row + 3, col + 3];
//If the current number's product is greater than the greatest product replace it //If the current number's product is greater than the greatest product replace it
if(mee.Algorithms.getProd(currentProduct) > mee.Algorithms.getProd(greatestProduct)){ if(mee.Algorithms.GetProd(currentProduct) > mee.Algorithms.GetProd(greatestProduct)){
greatestProduct = currentProduct.ToList(); greatestProduct = currentProduct.ToList();
} }
} }
@@ -187,18 +187,18 @@ namespace ProjectEulerCS.Problems{
} }
//Stop the timer //Stop the timer
_timer.stop(); _timer.Stop();
//Throw a flag to show the problem is solved //Throw a flag to show the problem is solved
solved = true; solved = true;
//Save the results //Save the results
_result = "The greatest product of 4 numbers in a line is " + mee.Algorithms.getProd(greatestProduct) + "\n" + _result = "The greatest product of 4 numbers in a line is " + mee.Algorithms.GetProd(greatestProduct) + "\n" +
"The numbers are [" + string.Join(", ", greatestProduct) + "]"; "The numbers are [" + string.Join(", ", greatestProduct) + "]";
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
public override void reset(){ public override void Reset(){
base.reset(); base.Reset();
greatestProduct = new List<int>(){0, 0, 0, 0}; greatestProduct = new List<int>(){0, 0, 0, 0};
} }
} }

View File

@@ -77,7 +77,7 @@ namespace ProjectEulerCS.Problems{
} }
//Operational functions //Operational functions
//Solve the problem //Solve the problem
public override void solve(){ public override void Solve(){
//If the problem has already been solved do nothing and end the function //If the problem has already been solved do nothing and end the function
if(solved){ if(solved){
return; return;
@@ -87,11 +87,11 @@ namespace ProjectEulerCS.Problems{
bool foundNumber = false; //To flag whether the number has been found bool foundNumber = false; //To flag whether the number has been found
//Start the timer //Start the timer
_timer.start(); _timer.Start();
//Loop until you fin the appropriate number //Loop until you fin the appropriate number
while((!foundNumber) && (sum > 0)){ while((!foundNumber) && (sum > 0)){
divisors = mee.Algorithms.getDivisors(sum); divisors = mee.Algorithms.GetDivisors(sum);
//If the number of divisors is correct set the flag //If the number of divisors is correct set the flag
if(divisors.Count > GOAL_DIVISORS){ if(divisors.Count > GOAL_DIVISORS){
foundNumber = true; foundNumber = true;
@@ -104,7 +104,7 @@ namespace ProjectEulerCS.Problems{
} }
//Stop the timer //Stop the timer
_timer.stop(); _timer.Stop();
//Throw a flag to show the problem is sovled //Throw a flag to show the problem is sovled
solved = true; solved = true;
@@ -113,8 +113,8 @@ namespace ProjectEulerCS.Problems{
_result = "The triangular number " + sum + " is the sum of all number >= " + (counter - 1) + " and has " + divisors.Count + " divisors"; _result = "The triangular number " + sum + " is the sum of all number >= " + (counter - 1) + " and has " + divisors.Count + " divisors";
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
public override void reset(){ public override void Reset(){
base.reset(); base.Reset();
sum = 1; sum = 1;
counter = 2; counter = 2;
divisors.Clear(); divisors.Clear();

View File

@@ -33,7 +33,7 @@ namespace ProjectEulerCS.Problems{
private const int TOP_NUM = 4000000 - 1; //The largest number that will be checked as a fibonacci number private const int TOP_NUM = 4000000 - 1; //The largest number that will be checked as a fibonacci number
//Instance variables //Instance variables
private int fullSum; //Holds the sum of all the numbers private int fullSum; //Holds the sum of all the numbers
public int sum{ public int Sum{
get{ get{
if(!solved){ if(!solved){
throw new Unsolved(); throw new Unsolved();
@@ -49,17 +49,17 @@ namespace ProjectEulerCS.Problems{
} }
//Operational functions //Operational functions
//Solve the problem //Solve the problem
public override void solve(){ public override void Solve(){
//If the problem has already been solved do nothing and end the function //If the problem has already been solved do nothing and end the function
if(solved){ if(solved){
return; return;
} }
//Start the timer //Start the timer
_timer.start(); _timer.Start();
//Get a list of all fibonacci numbers < 4,000,000 //Get a list of all fibonacci numbers < 4,000,000
List<int> fibNums = mee.Algorithms.getAllFib(TOP_NUM); List<int> fibNums = mee.Algorithms.GetAllFib(TOP_NUM);
//Step through every element in the list checkint if it is even //Step through every element in the list checkint if it is even
foreach(int num in fibNums){ foreach(int num in fibNums){
//If the number is even add it to the running tally //If the number is even add it to the running tally
@@ -69,7 +69,7 @@ namespace ProjectEulerCS.Problems{
} }
//Stop the timer //Stop the timer
_timer.stop(); _timer.Stop();
//Throw a flag to show the problem is solved //Throw a flag to show the problem is solved
solved = true; solved = true;
@@ -78,8 +78,8 @@ namespace ProjectEulerCS.Problems{
_result = "The sum of all even fibonacci numbers <= " + TOP_NUM + " is " + fullSum; _result = "The sum of all even fibonacci numbers <= " + TOP_NUM + " is " + fullSum;
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
public override void reset(){ public override void Reset(){
base.reset(); base.Reset();
fullSum = 0; fullSum = 0;
} }
} }

View File

@@ -30,61 +30,61 @@ namespace ProjectEulerCS.Problems{
//Variables //Variables
//Static variables //Static variables
private const long GOAL_NUMBER = 600851475143L; //The number that needs factored private const long GOAL_NUMBER = 600851475143L; //The number that needs factored
public long goalNumber{ public long GoalNumber{
get{ return GOAL_NUMBER; } get{ return GOAL_NUMBER; }
} }
//Instance variables //Instance variables
private List<long> _factors; //Holds the factors of goalNumber private List<long> factors; //Holds the factors of goalNumber
public List<long> factors{ public List<long> Factors{
get{ get{
if(!solved){ if(!solved){
throw new Unsolved(); throw new Unsolved();
} }
return _factors; return factors;
} }
} }
public long largestFactor{ public long LargestFactor{
get{ get{
if(!solved){ if(!solved){
throw new Unsolved(); throw new Unsolved();
} }
return _factors[_factors.Count - 1]; return factors[^1];
} }
} }
//Functions //Functions
//Constructor //Constructor
public Problem3() : base("What is the largest prime factor of 600851475143?"){ public Problem3() : base("What is the largest prime factor of 600851475143?"){
_factors = new List<long>(); factors = new List<long>();
} }
//Operational functions //Operational functions
//Solve the problem //Solve the problem
public override void solve(){ public override void Solve(){
//If the problem has already been solved do nothing and end the function //If the problem has already been solved do nothing and end the function
if(solved){ if(solved){
return; return;
} }
//Star the timer //Star the timer
_timer.start(); _timer.Start();
//Get all the factors of the number //Get all the factors of the number
_factors = mee.Algorithms.getFactors(GOAL_NUMBER); factors = mee.Algorithms.GetFactors(GOAL_NUMBER);
//THe last element should be the largest factor //THe last element should be the largest factor
//Stop the timer //Stop the timer
_timer.stop(); _timer.Stop();
//Throw a flag to show the problem is solved //Throw a flag to show the problem is solved
solved = true; solved = true;
//Save the results //Save the results
_result = "The largest factor of the number " + GOAL_NUMBER + " is " + factors[factors.Count - 1]; _result = "The largest factor of the number " + GOAL_NUMBER + " is " + factors[^1];
} }
//Reset the porblem so it can be run again //Reset the porblem so it can be run again
public override void reset(){ public override void Reset(){
base.reset(); base.Reset();
_factors.Clear(); factors.Clear();
} }
} }
} }

View File

@@ -33,38 +33,38 @@ namespace ProjectEulerCS.Problems{
private const int START_NUM = 100; //The first number to be multiplied private const int START_NUM = 100; //The first number to be multiplied
private const int END_NUM = 999; //The last number to be multiplied private const int END_NUM = 999; //The last number to be multiplied
//Instace variable //Instace variable
private List<int> _palindromes; //Holds all numbers that turn out to be palindromes private readonly List<int> palindromes; //Holds all numbers that turn out to be palindromes
public List<int> palindromes{ public List<int> Palindromes{
get{ get{
if(!solved){ if(!solved){
throw new Unsolved(); throw new Unsolved();
} }
return _palindromes; return palindromes;
} }
} }
public int largestPalindrom{ public int LargestPalindrom{
get{ get{
if(!solved){ if(!solved){
throw new Unsolved(); throw new Unsolved();
} }
return _palindromes[_palindromes.Count - 1]; return palindromes[^1];
} }
} }
//Constructor //Constructor
public Problem4() : base("Find the largest palindrome made from the product of two 3-digit numbers"){ public Problem4() : base("Find the largest palindrome made from the product of two 3-digit numbers"){
_palindromes = new List<int>(); palindromes = new List<int>();
} }
//Operational funcitons //Operational funcitons
//SOlve the problem //SOlve the problem
public override void solve(){ public override void Solve(){
//If the problem has already been solved do nothing and end the function //If the problem has already been solved do nothing and end the function
if(solved){ if(solved){
return; return;
} }
//Start the timer //Start the timer
_timer.start(); _timer.Start();
//Start at the first 3-digit number and check every one up to the last 3-digit number //Start at the first 3-digit number and check every one up to the last 3-digit number
for(int firstNum = START_NUM;firstNum <= END_NUM;++firstNum){ for(int firstNum = START_NUM;firstNum <= END_NUM;++firstNum){
@@ -79,28 +79,28 @@ namespace ProjectEulerCS.Problems{
//If the number and it's reverse are the same it is a palindrom so add it to the list //If the number and it's reverse are the same it is a palindrom so add it to the list
if(productString == reverseString){ if(productString == reverseString){
_palindromes.Add(product); palindromes.Add(product);
} }
//If it's not a palindrome ignore it and move to the next number //If it's not a palindrome ignore it and move to the next number
} }
} }
//Sort the palindromes so that the last one is the largest //Sort the palindromes so that the last one is the largest
_palindromes.Sort(); palindromes.Sort();
//Stop the timer //Stop the timer
_timer.stop(); _timer.Stop();
//Throw a flag to show the problem is solved //Throw a flag to show the problem is solved
solved = true; solved = true;
//Save the results //Save the results
_result = "The largest palindrome is " + _palindromes[_palindromes.Count - 1]; _result = "The largest palindrome is " + palindromes[^1];
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
public override void reset(){ public override void Reset(){
base.reset(); base.Reset();
_palindromes.Clear(); palindromes.Clear();
} }
} }
} }

View File

@@ -27,7 +27,7 @@ namespace ProjectEulerCS.Problems{
//Variables //Variables
//Instance variables //Instance variables
private int smallestNum; //The smallest number that is found private int smallestNum; //The smallest number that is found
public int number{ public int Number{
get{ get{
if(!solved){ if(!solved){
throw new Unsolved(); throw new Unsolved();
@@ -43,14 +43,14 @@ namespace ProjectEulerCS.Problems{
} }
//Operational functions //Operational functions
//Solve the problem //Solve the problem
public override void solve(){ public override void Solve(){
//If the problem has already been solved do nothing and end the function //If the problem has already been solved do nothing and end the function
if(solved){ if(solved){
return; return;
} }
//Start the timer //Start the timer
_timer.start(); _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 //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 bool numFound = false; //A flag for finding the divisible number
@@ -75,7 +75,7 @@ namespace ProjectEulerCS.Problems{
smallestNum = currentNum; smallestNum = currentNum;
//Stop the timer //Stop the timer
_timer.stop(); _timer.Stop();
//Throw a flag to show the problem is solved //Throw a flag to show the problem is solved
solved = true; solved = true;
@@ -84,8 +84,8 @@ namespace ProjectEulerCS.Problems{
_result = "The smallest positive number evenly divisible by all numbers 1-20 is " + currentNum; _result = "The smallest positive number evenly divisible by all numbers 1-20 is " + currentNum;
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
public override void reset(){ public override void Reset(){
base.reset(); base.Reset();
smallestNum = 0; smallestNum = 0;
} }
} }

View File

@@ -29,72 +29,72 @@ namespace ProjectEulerCS.Problems{
private const int START_NUM = 1; //The first number that needs to be counted private const int START_NUM = 1; //The first number that needs to be counted
private const int END_NUM = 100; //The last number that needs to be counted private const int END_NUM = 100; //The last number that needs to be counted
//Instance variables //Instance variables
private long _sumOfSquares; //Holds the sum of the squares of all the numbers private long sumOfSquares; //Holds the sum of the squares of all the numbers
public long sumOfSquares{ public long SumOfSquares{
get{ get{
if(!solved){ if(!solved){
throw new Unsolved(); throw new Unsolved();
} }
return _sumOfSquares; return sumOfSquares;
} }
} }
private long _squareOfSum; //Holds the square of the sum of all the numbers private long squareOfSum; //Holds the square of the sum of all the numbers
public long squareOfSum{ public long SquareOfSum{
get{ get{
if(!solved){ if(!solved){
throw new Unsolved(); throw new Unsolved();
} }
return _squareOfSum; return squareOfSum;
} }
} }
public long difference{ public long Difference{
get{ get{
if(!solved){ if(!solved){
throw new Unsolved(); throw new Unsolved();
} }
return System.Math.Abs(_sumOfSquares - _squareOfSum); return System.Math.Abs(sumOfSquares - squareOfSum);
} }
} }
//Functions //Functions
//Constructor //Constructor
public Problem6() : base("Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum."){ public Problem6() : base("Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum."){
_sumOfSquares = 0; sumOfSquares = 0;
_squareOfSum = 0; squareOfSum = 0;
} }
//Operational functions //Operational functions
//Solve the problem //Solve the problem
public override void solve(){ public override void Solve(){
//If the problem has already been solved do nothing and end the function //If the problem has already been solved do nothing and end the function
if(solved){ if(solved){
return; return;
} }
//Start the timer //Start the timer
_timer.start(); _timer.Start();
//Run through all numbers and add them to the appropriate sums //Run through all numbers and add them to the appropriate sums
for(int currentNum = START_NUM;currentNum <= END_NUM;++currentNum){ for(int currentNum = START_NUM;currentNum <= END_NUM;++currentNum){
_sumOfSquares += (currentNum * currentNum); //Add the square to the correct variable sumOfSquares += (currentNum * currentNum); //Add the square to the correct variable
_squareOfSum += currentNum; //Add the number to the correct variable to squaring later squareOfSum += currentNum; //Add the number to the correct variable to squaring later
} }
//Squaring the sum that needs it //Squaring the sum that needs it
_squareOfSum *= _squareOfSum; squareOfSum *= squareOfSum;
//Stop the timer //Stop the timer
_timer.stop(); _timer.Stop();
//Throw a flag to show the problem is solved //Throw a flag to show the problem is solved
solved = true; solved = true;
//Save the results //Save the results
_result = "The difference between the sum of the squares and the square of the sum of all numbers from 1-100 is " + System.Math.Abs(_sumOfSquares - _squareOfSum); _result = "The difference between the sum of the squares and the square of the sum of all numbers from 1-100 is " + System.Math.Abs(sumOfSquares - squareOfSum);
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
public override void reset(){ public override void Reset(){
base.reset(); base.Reset();
_squareOfSum = 0; squareOfSum = 0;
_sumOfSquares = 0; sumOfSquares = 0;
} }
} }
} }

View File

@@ -32,12 +32,12 @@ namespace ProjectEulerCS.Problems{
private const long NUMBER_OF_PRIMES = 10001; //The number of primes we are trying to get private const long NUMBER_OF_PRIMES = 10001; //The number of primes we are trying to get
//Instance variables //Instance variables
private List<long> primes; private List<long> primes;
public long prime{ public long Prime{
get{ get{
if(!solved){ if(!solved){
throw new Unsolved(); throw new Unsolved();
} }
return primes[primes.Count - 1]; return primes[^1];
} }
} }
@@ -48,30 +48,30 @@ namespace ProjectEulerCS.Problems{
} }
//Operatinal functions //Operatinal functions
//Solve the problem //Solve the problem
public override void solve(){ public override void Solve(){
//If the problem has already been solved do nothing and end the function //If the problem has already been solved do nothing and end the function
if(solved){ if(solved){
return; return;
} }
//Start the timer //Start the timer
_timer.start(); _timer.Start();
//Setup the variables //Setup the variables
primes = mee.Algorithms.getNumPrimes(NUMBER_OF_PRIMES); //Holds the prime numbers primes = mee.Algorithms.GetNumPrimes(NUMBER_OF_PRIMES); //Holds the prime numbers
//Stop the timer //Stop the timer
_timer.stop(); _timer.Stop();
//Throw a flag to show the problem is solved //Throw a flag to show the problem is solved
solved = true; solved = true;
//Save the results //Save the results
_result = "The " + NUMBER_OF_PRIMES + "th prime number is " + primes[primes.Count - 1]; _result = "The " + NUMBER_OF_PRIMES + "th prime number is " + primes[^1];
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
public override void reset(){ public override void Reset(){
base.reset(); base.Reset();
primes.Clear(); primes.Clear();
} }
} }

View File

@@ -52,7 +52,7 @@ namespace ProjectEulerCS.Problems{
private const string NUMBER = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"; private const string NUMBER = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
//Instance variables //Instance variables
private string maxNums; //Holds the string of the largest product private string maxNums; //Holds the string of the largest product
public string largestNums{ public string LargestNums{
get{ get{
if(!solved){ if(!solved){
throw new Unsolved(); throw new Unsolved();
@@ -61,7 +61,7 @@ namespace ProjectEulerCS.Problems{
} }
} }
private long maxProduct; //Holds the largest product of 13 numbers private long maxProduct; //Holds the largest product of 13 numbers
public long largestProduct{ public long LargestProduct{
get{ get{
if(!solved){ if(!solved){
throw new Unsolved(); throw new Unsolved();
@@ -78,14 +78,14 @@ namespace ProjectEulerCS.Problems{
} }
//Operational functions //Operational functions
//Solve the problem //Solve the problem
public override void solve(){ public override void Solve(){
//If the problem has already been solved do nothing and end the function //If the problem has already been solved do nothing and end the function
if(solved){ if(solved){
return; return;
} }
//Start the timer //Start the timer
_timer.start(); _timer.Start();
//Cycle through the string of numbers looking for the maximum product //Cycle through the string of numbers looking for the maximum product
for(int cnt = 12;cnt < NUMBER.Length;++cnt){ for(int cnt = 12;cnt < NUMBER.Length;++cnt){
@@ -99,7 +99,7 @@ namespace ProjectEulerCS.Problems{
} }
//Stop the timer //Stop the timer
_timer.stop(); _timer.Stop();
//Throw a flag to show the problem is solved //Throw a flag to show the problem is solved
solved = true; solved = true;
@@ -108,8 +108,8 @@ namespace ProjectEulerCS.Problems{
_result = "The greatest product is " + maxProduct + "\nThe numbers are " + maxNums; _result = "The greatest product is " + maxProduct + "\nThe numbers are " + maxNums;
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
public override void reset(){ public override void Reset(){
base.reset(); base.Reset();
maxNums = ""; maxNums = "";
maxProduct = 0; maxProduct = 0;
} }

View File

@@ -27,7 +27,7 @@ namespace ProjectEulerCS{
//Variables //Variables
//Instance variables //Instance variables
private int a; //Holds the size of the first side private int a; //Holds the size of the first side
public int sideA{ public int SideA{
get{ get{
if(!solved){ if(!solved){
throw new Unsolved(); throw new Unsolved();
@@ -36,7 +36,7 @@ namespace ProjectEulerCS{
} }
} }
private int b; //Holds the size of the second side private int b; //Holds the size of the second side
public int sideB{ public int SideB{
get{ get{
if(!solved){ if(!solved){
throw new Unsolved(); throw new Unsolved();
@@ -45,7 +45,7 @@ namespace ProjectEulerCS{
} }
} }
private double c; //Holds the size of the hyp private double c; //Holds the size of the hyp
public int sideC{ public int SideC{
get{ get{
if(!solved){ if(!solved){
throw new Unsolved(); throw new Unsolved();
@@ -54,7 +54,7 @@ namespace ProjectEulerCS{
} }
} }
private bool found; //A flag to determine if we have found the solution yet private bool found; //A flag to determine if we have found the solution yet
public int product{ public int Product{
get{ get{
if(!solved){ if(!solved){
throw new Unsolved(); throw new Unsolved();
@@ -73,14 +73,14 @@ namespace ProjectEulerCS{
} }
//Operational functions //Operational functions
//Solve the problem //Solve the problem
public override void solve(){ public override void Solve(){
//If the problem has already been solved do nothing and end the function //If the problem has already been solved do nothing and end the function
if(solved){ if(solved){
return; return;
} }
//Start the timer //Start the timer
_timer.start(); _timer.Start();
//Loop through all possible a's //Loop through all possible a's
while((a < 1000) && !found){ while((a < 1000) && !found){
@@ -103,22 +103,22 @@ namespace ProjectEulerCS{
} }
//Stop the timer //Stop the timer
_timer.stop(); _timer.Stop();
//Throw a flag to show the problem is solved //Throw a flag to show the problem is solved
solved = true; solved = true;
if(found){ if(found){
_result = "The Pythagorean triplet is " + a + " + " + b + " + " + System.Math.Round(c) + _result = "The Pythagorean triplet is " + a + " + " + b + " + " + System.Math.Round(c) +
"\nThe numbers' product is " + product; "\nThe numbers' product is " + Product;
} }
else{ else{
_result = "The number was not found!"; _result = "The number was not found!";
} }
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
public override void reset(){ public override void Reset(){
base.reset(); base.Reset();
a = 1; a = 1;
b = 0; b = 0;
c = 0; c = 0;

View File

@@ -27,26 +27,26 @@ namespace ProjectEulerCS{
private enum SELECTIONS { SOLVE = 1, DESCRIPTION, LIST, BENCHMARK, EXIT, SIZE }; private enum SELECTIONS { SOLVE = 1, DESCRIPTION, LIST, BENCHMARK, EXIT, SIZE };
//Drives the program //Drives the program
public static void Main(string[] args){ public static void Main(){
SELECTIONS selection = SELECTIONS.SIZE; SELECTIONS selection;
do{ do{
//Print the menu and prompt the user to select an action //Print the menu and prompt the user to select an action
printMenu(); PrintMenu();
selection = getMenuSelection(); selection = GetMenuSelection();
switch(selection){ switch(selection){
case SELECTIONS.SOLVE: solveMenu(); break; case SELECTIONS.SOLVE: SolveMenu(); break;
case SELECTIONS.DESCRIPTION: descriptionMenu(); break; case SELECTIONS.DESCRIPTION: DescriptionMenu(); break;
case SELECTIONS.LIST: ProblemSelection.listProblems(); break; case SELECTIONS.LIST: ProblemSelection.ListProblems(); break;
case SELECTIONS.BENCHMARK: Benchmark.benchmarkMenu(); break; case SELECTIONS.BENCHMARK: Benchmark.BenchmarkMenu(); break;
case SELECTIONS.EXIT: break; case SELECTIONS.EXIT: break;
case SELECTIONS.SIZE: case SELECTIONS.SIZE:
default: printErrorMessage(); break; default: PrintErrorMessage(); break;
} }
}while(!selection.Equals(SELECTIONS.EXIT)); }while(!selection.Equals(SELECTIONS.EXIT));
} }
//Print the menu //Print the menu
private static void printMenu(){ private static void PrintMenu(){
System.Console.WriteLine("1. Solve a problem"); System.Console.WriteLine("1. Solve a problem");
System.Console.WriteLine("2. Print a problem description"); System.Console.WriteLine("2. Print a problem description");
System.Console.WriteLine("3. List valid problem numbers"); System.Console.WriteLine("3. List valid problem numbers");
@@ -55,19 +55,19 @@ namespace ProjectEulerCS{
System.Console.WriteLine(); System.Console.WriteLine();
} }
//Get a menu selection from the user //Get a menu selection from the user
private static SELECTIONS getMenuSelection(){ private static SELECTIONS GetMenuSelection(){
string selectionString = System.Console.ReadLine(); string selectionString = System.Console.ReadLine();
int selection = System.Convert.ToInt32(selectionString); int selection = System.Convert.ToInt32(selectionString);
while(!isValidMenu(selection)){ while(!IsValidMenu(selection)){
System.Console.WriteLine("that is an invalid option!\nPress Enter to continue"); System.Console.WriteLine("that is an invalid option!\nPress Enter to continue");
printMenu(); PrintMenu();
selectionString = System.Console.ReadLine(); selectionString = System.Console.ReadLine();
selection = System.Convert.ToInt32(selectionString); selection = System.Convert.ToInt32(selectionString);
} }
return getSelection(selection); return GetSelection(selection);
} }
//Make sure the value passed in is a valid menu option //Make sure the value passed in is a valid menu option
private static bool isValidMenu(int selection){ private static bool IsValidMenu(int selection){
if((selection >= (int)SELECTIONS.SOLVE) && (selection < (int)SELECTIONS.SIZE)){ if((selection >= (int)SELECTIONS.SOLVE) && (selection < (int)SELECTIONS.SIZE)){
return true; return true;
} }
@@ -76,48 +76,46 @@ namespace ProjectEulerCS{
} }
} }
//Turns an integer passed to it into a SELECTION enum //Turns an integer passed to it into a SELECTION enum
private static SELECTIONS getSelection(int selection){ private static SELECTIONS GetSelection(int selection){
SELECTIONS sel = SELECTIONS.SIZE; SELECTIONS sel = selection switch{
1 => SELECTIONS.SOLVE,
switch(selection){ 2 => SELECTIONS.DESCRIPTION,
case 1: sel = SELECTIONS.SOLVE; break; 3 => SELECTIONS.LIST,
case 2: sel = SELECTIONS.DESCRIPTION; break; 4 => SELECTIONS.BENCHMARK,
case 3: sel = SELECTIONS.LIST; break; 5 => SELECTIONS.EXIT,
case 4: sel = SELECTIONS.BENCHMARK; break; _ => SELECTIONS.SIZE,
case 5: sel = SELECTIONS.EXIT; break; };
default: sel = SELECTIONS.SIZE; break;
}
return sel; return sel;
} }
//Print an error message //Print an error message
private static void printErrorMessage(){ private static void PrintErrorMessage(){
System.Console.WriteLine("That is an invalid selection!"); System.Console.WriteLine("That is an invalid selection!");
} }
//Handle what happens when a user wants to solve a problem //Handle what happens when a user wants to solve a problem
private static void solveMenu(){ private static void SolveMenu(){
int problemNumber = ProblemSelection.getProblemNumber(); int problemNumber = ProblemSelection.GetProblemNumber();
//This selection solves all problems in order //This selection solves all problems in order
if(problemNumber == 0){ if(problemNumber == 0){
//Solve to every valid problem number, skipping over 0 //Solve to every valid problem number, skipping over 0
for(int problemLocation = 1; problemLocation < ProblemSelection.PROBLEM_NUMBERS.Count; ++problemLocation){ for(int problemLocation = 1; problemLocation < ProblemSelection.PROBLEM_NUMBERS.Count; ++problemLocation){
//Solve the problems //Solve the problems
System.Console.Write(ProblemSelection.PROBLEM_NUMBERS[problemLocation] + ". "); System.Console.Write(ProblemSelection.PROBLEM_NUMBERS[problemLocation] + ". ");
ProblemSelection.solveProblem(ProblemSelection.PROBLEM_NUMBERS[problemLocation]); ProblemSelection.SolveProblem(ProblemSelection.PROBLEM_NUMBERS[problemLocation]);
} }
} }
//This is if a single problem number was chosen //This is if a single problem number was chosen
else{ else{
//Solve the problem //Solve the problem
ProblemSelection.solveProblem(problemNumber); ProblemSelection.SolveProblem(problemNumber);
} }
} }
//Handle what happens when a user wants to see the description of a problem //Handle what happens when a user wants to see the description of a problem
private static void descriptionMenu(){ private static void DescriptionMenu(){
//Give some extra space to print the description //Give some extra space to print the description
System.Console.WriteLine("\n"); System.Console.WriteLine("\n");
//Get the problem number //Get the problem number
int problemNumber = ProblemSelection.getProblemNumber(); int problemNumber = ProblemSelection.GetProblemNumber();
//If the problem number is 0 print out all the descriptions //If the problem number is 0 print out all the descriptions
if(problemNumber == 0){ if(problemNumber == 0){
@@ -125,13 +123,13 @@ namespace ProjectEulerCS{
for(int problemLocation = 1; problemLocation < ProblemSelection.PROBLEM_NUMBERS.Count; ++problemLocation){ for(int problemLocation = 1; problemLocation < ProblemSelection.PROBLEM_NUMBERS.Count; ++problemLocation){
//Print the problem's descrfiption //Print the problem's descrfiption
System.Console.Write(ProblemSelection.PROBLEM_NUMBERS[problemLocation] + ". "); System.Console.Write(ProblemSelection.PROBLEM_NUMBERS[problemLocation] + ". ");
ProblemSelection.printDescription(ProblemSelection.PROBLEM_NUMBERS[problemLocation]); ProblemSelection.PrintDescription(ProblemSelection.PROBLEM_NUMBERS[problemLocation]);
System.Console.WriteLine(); System.Console.WriteLine();
} }
} }
//Otherwise print out a single problem's description //Otherwise print out a single problem's description
else{ else{
ProblemSelection.printDescription(problemNumber); ProblemSelection.PrintDescription(problemNumber);
} }
} }
} }