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

View File

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

View File

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

View File

@@ -26,9 +26,9 @@ namespace ProjectEulerCS.Problems{
public class Problem1 : Problem{
//Variables
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
public int sum{
public int Sum{
get{
if(!solved){
throw new Unsolved();
@@ -44,14 +44,14 @@ namespace ProjectEulerCS.Problems{
}
//Operational functions
//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 (solved){
return;
}
//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
for (int cnt = 1; cnt <= TOP_NUM; ++cnt){
@@ -64,7 +64,7 @@ namespace ProjectEulerCS.Problems{
}
//Stop the timer
_timer.stop();
_timer.Stop();
//Thow a flag to show the problem is solved
solved = true;
@@ -73,8 +73,8 @@ namespace ProjectEulerCS.Problems{
_result = "The sum of all numbers < " + (TOP_NUM + 1) + " is " + fullSum;
}
//Reset the problem so it can be run again
public override void reset(){
base.reset();
public override void Reset(){
base.Reset();
fullSum = 0;
}
}

View File

@@ -31,37 +31,37 @@ namespace ProjectEulerCS{
//Static variables
private const long GOAL_NUMBER = 2000000 - 1;
//Instance variables
private long _sum; //The sum of all the prime numbers
public long sum{
private long sum; //The sum of all the prime numbers
public long Sum{
get{
if(!solved){
throw new Unsolved();
}
return _sum;
return sum;
}
}
//Functions
//Constructor
public Problem10() : base("Find the sum of all the primes below two million"){
_sum = 0;
sum = 0;
}
//Operational functions
//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(solved){
return;
}
//Start the timer
_timer.start();
_timer.Start();
//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
_timer.stop();
_timer.Stop();
//Throw a flag to show the problem is solved
solved = true;
@@ -70,9 +70,9 @@ namespace ProjectEulerCS{
_result = "The sum of all the primes < " + (GOAL_NUMBER + 1) + " is " + sum;
}
//Reset the problem so it can be run again
public override void reset(){
base.reset();
_sum = 0;
public override void Reset(){
base.Reset();
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}};
//Instance variables
private List<int> greatestProduct; //Holds the largest product we have found so far
public List<int> numbers{
public List<int> Numbers{
get{
if(!solved){
throw new Unsolved();
@@ -83,12 +83,12 @@ namespace ProjectEulerCS.Problems{
return greatestProduct;
}
}
public int product{
public int Product{
get{
if(!solved){
throw new Unsolved();
}
return mee.Algorithms.getProd(greatestProduct);
return mee.Algorithms.GetProd(greatestProduct);
}
}
@@ -99,7 +99,7 @@ namespace ProjectEulerCS.Problems{
}
//Operational functions
//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(solved){
return;
@@ -109,7 +109,7 @@ namespace ProjectEulerCS.Problems{
List<int> currentProduct = new List<int>() {0, 0, 0, 0};
//Start the timer
_timer.start();
_timer.Start();
//Loop through every row and column
for(int row = 0;row < grid.GetLength(0);++row){
@@ -140,7 +140,7 @@ namespace ProjectEulerCS.Problems{
currentProduct[3] = grid[row, col + 3];
//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();
}
}
@@ -153,7 +153,7 @@ namespace ProjectEulerCS.Problems{
currentProduct[3] = grid[row + 3, col];
//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();
}
}
@@ -166,7 +166,7 @@ namespace ProjectEulerCS.Problems{
currentProduct[3] = grid[row + 3, col - 3];
//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();
}
}
@@ -179,7 +179,7 @@ namespace ProjectEulerCS.Problems{
currentProduct[3] = grid[row + 3, col + 3];
//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();
}
}
@@ -187,18 +187,18 @@ namespace ProjectEulerCS.Problems{
}
//Stop the timer
_timer.stop();
_timer.Stop();
//Throw a flag to show the problem is solved
solved = true;
//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) + "]";
}
//Reset the problem so it can be run again
public override void reset(){
base.reset();
public override void Reset(){
base.Reset();
greatestProduct = new List<int>(){0, 0, 0, 0};
}
}

View File

@@ -77,7 +77,7 @@ namespace ProjectEulerCS.Problems{
}
//Operational functions
//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(solved){
return;
@@ -87,11 +87,11 @@ namespace ProjectEulerCS.Problems{
bool foundNumber = false; //To flag whether the number has been found
//Start the timer
_timer.start();
_timer.Start();
//Loop until you fin the appropriate number
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(divisors.Count > GOAL_DIVISORS){
foundNumber = true;
@@ -104,7 +104,7 @@ namespace ProjectEulerCS.Problems{
}
//Stop the timer
_timer.stop();
_timer.Stop();
//Throw a flag to show the problem is sovled
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";
}
//Reset the problem so it can be run again
public override void reset(){
base.reset();
public override void Reset(){
base.Reset();
sum = 1;
counter = 2;
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
//Instance variables
private int fullSum; //Holds the sum of all the numbers
public int sum{
public int Sum{
get{
if(!solved){
throw new Unsolved();
@@ -49,17 +49,17 @@ namespace ProjectEulerCS.Problems{
}
//Operational functions
//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(solved){
return;
}
//Start the timer
_timer.start();
_timer.Start();
//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
foreach(int num in fibNums){
//If the number is even add it to the running tally
@@ -69,7 +69,7 @@ namespace ProjectEulerCS.Problems{
}
//Stop the timer
_timer.stop();
_timer.Stop();
//Throw a flag to show the problem is solved
solved = true;
@@ -78,8 +78,8 @@ namespace ProjectEulerCS.Problems{
_result = "The sum of all even fibonacci numbers <= " + TOP_NUM + " is " + fullSum;
}
//Reset the problem so it can be run again
public override void reset(){
base.reset();
public override void Reset(){
base.Reset();
fullSum = 0;
}
}

View File

@@ -30,61 +30,61 @@ namespace ProjectEulerCS.Problems{
//Variables
//Static variables
private const long GOAL_NUMBER = 600851475143L; //The number that needs factored
public long goalNumber{
public long GoalNumber{
get{ return GOAL_NUMBER; }
}
//Instance variables
private List<long> _factors; //Holds the factors of goalNumber
public List<long> factors{
private List<long> factors; //Holds the factors of goalNumber
public List<long> Factors{
get{
if(!solved){
throw new Unsolved();
}
return _factors;
return factors;
}
}
public long largestFactor{
public long LargestFactor{
get{
if(!solved){
throw new Unsolved();
}
return _factors[_factors.Count - 1];
return factors[^1];
}
}
//Functions
//Constructor
public Problem3() : base("What is the largest prime factor of 600851475143?"){
_factors = new List<long>();
factors = new List<long>();
}
//Operational functions
//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(solved){
return;
}
//Star the timer
_timer.start();
_timer.Start();
//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
//Stop the timer
_timer.stop();
_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];
_result = "The largest factor of the number " + GOAL_NUMBER + " is " + factors[^1];
}
//Reset the porblem so it can be run again
public override void reset(){
base.reset();
_factors.Clear();
public override void Reset(){
base.Reset();
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 END_NUM = 999; //The last number to be multiplied
//Instace variable
private List<int> _palindromes; //Holds all numbers that turn out to be palindromes
public List<int> palindromes{
private readonly List<int> palindromes; //Holds all numbers that turn out to be palindromes
public List<int> Palindromes{
get{
if(!solved){
throw new Unsolved();
}
return _palindromes;
return palindromes;
}
}
public int largestPalindrom{
public int LargestPalindrom{
get{
if(!solved){
throw new Unsolved();
}
return _palindromes[_palindromes.Count - 1];
return palindromes[^1];
}
}
//Constructor
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
//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(solved){
return;
}
//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
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(productString == reverseString){
_palindromes.Add(product);
palindromes.Add(product);
}
//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
_palindromes.Sort();
palindromes.Sort();
//Stop the timer
_timer.stop();
_timer.Stop();
//Throw a flag to show the problem is solved
solved = true;
//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
public override void reset(){
base.reset();
_palindromes.Clear();
public override void Reset(){
base.Reset();
palindromes.Clear();
}
}
}

View File

@@ -27,7 +27,7 @@ namespace ProjectEulerCS.Problems{
//Variables
//Instance variables
private int smallestNum; //The smallest number that is found
public int number{
public int Number{
get{
if(!solved){
throw new Unsolved();
@@ -43,14 +43,14 @@ namespace ProjectEulerCS.Problems{
}
//Operational functions
//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(solved){
return;
}
//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
bool numFound = false; //A flag for finding the divisible number
@@ -75,7 +75,7 @@ namespace ProjectEulerCS.Problems{
smallestNum = currentNum;
//Stop the timer
_timer.stop();
_timer.Stop();
//Throw a flag to show the problem is solved
solved = true;
@@ -84,8 +84,8 @@ namespace ProjectEulerCS.Problems{
_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();
public override void Reset(){
base.Reset();
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 END_NUM = 100; //The last number that needs to be counted
//Instance variables
private long _sumOfSquares; //Holds the sum of the squares of all the numbers
public long sumOfSquares{
private long sumOfSquares; //Holds the sum of the squares of all the numbers
public long SumOfSquares{
get{
if(!solved){
throw new Unsolved();
}
return _sumOfSquares;
return sumOfSquares;
}
}
private long _squareOfSum; //Holds the square of the sum of all the numbers
public long squareOfSum{
private long squareOfSum; //Holds the square of the sum of all the numbers
public long SquareOfSum{
get{
if(!solved){
throw new Unsolved();
}
return _squareOfSum;
return squareOfSum;
}
}
public long difference{
public long Difference{
get{
if(!solved){
throw new Unsolved();
}
return System.Math.Abs(_sumOfSquares - _squareOfSum);
return System.Math.Abs(sumOfSquares - squareOfSum);
}
}
//Functions
//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."){
_sumOfSquares = 0;
_squareOfSum = 0;
sumOfSquares = 0;
squareOfSum = 0;
}
//Operational functions
//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(solved){
return;
}
//Start the timer
_timer.start();
_timer.Start();
//Run through all numbers and add them to the appropriate sums
for(int currentNum = START_NUM;currentNum <= END_NUM;++currentNum){
_sumOfSquares += (currentNum * currentNum); //Add the square to the correct variable
_squareOfSum += currentNum; //Add the number to the correct variable to squaring later
sumOfSquares += (currentNum * currentNum); //Add the square to the correct variable
squareOfSum += currentNum; //Add the number to the correct variable to squaring later
}
//Squaring the sum that needs it
_squareOfSum *= _squareOfSum;
squareOfSum *= squareOfSum;
//Stop the timer
_timer.stop();
_timer.Stop();
//Throw a flag to show the problem is solved
solved = true;
//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
public override void reset(){
base.reset();
_squareOfSum = 0;
_sumOfSquares = 0;
public override void Reset(){
base.Reset();
squareOfSum = 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
//Instance variables
private List<long> primes;
public long prime{
public long Prime{
get{
if(!solved){
throw new Unsolved();
}
return primes[primes.Count - 1];
return primes[^1];
}
}
@@ -48,30 +48,30 @@ namespace ProjectEulerCS.Problems{
}
//Operatinal functions
//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(solved){
return;
}
//Start the timer
_timer.start();
_timer.Start();
//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
_timer.stop();
_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];
_result = "The " + NUMBER_OF_PRIMES + "th prime number is " + primes[^1];
}
//Reset the problem so it can be run again
public override void reset(){
base.reset();
public override void Reset(){
base.Reset();
primes.Clear();
}
}

View File

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

View File

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

View File

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