Changed how results are handled

This commit is contained in:
2020-08-27 13:30:04 -04:00
parent 46b5b39f1c
commit b307880181
21 changed files with 254 additions and 128 deletions

View File

@@ -9,16 +9,8 @@ namespace ProjectEulerCS{
public abstract class Problem{ public abstract class Problem{
//Variables //Variables
//Instance variables //Instance variables
//protected const Stopwatch timer = new Stopwatch(); //To time how long it takes to run the algorithm //The results of the problem
protected string _result = null; //Holds the results of the problem public abstract string Result{get;}
public string Result{
get{
if(!solved){
throw new Unsolved();
}
return _result;
}
}
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; }
@@ -55,7 +47,6 @@ namespace ProjectEulerCS{
public virtual void Reset(){ public virtual void Reset(){
timer.Reset(); timer.Reset();
solved = false; solved = false;
_result = null;
} }
} }
} }

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCS/src/Problems/Problem1.cs //ProjectEuler/ProjectEulerCS/src/Problems/Problem1.cs
//Matthew Ellison //Matthew Ellison
// Created: 08-14-20 // Created: 08-14-20
//Modified: 08-14-20 //Modified: 08-27-20
//What is the sum of all the multiples of 3 or 5 that are less than 1000 //What is the sum of all the multiples of 3 or 5 that are less than 1000
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses
/* /*
@@ -25,6 +25,7 @@
namespace ProjectEulerCS.Problems{ namespace ProjectEulerCS.Problems{
public class Problem1 : Problem{ public class Problem1 : Problem{
//Variables //Variables
//Static variables
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
@@ -37,9 +38,19 @@ namespace ProjectEulerCS.Problems{
} }
} }
//The results of the problem
public override string Result{
get{
if(!solved){
throw new Unsolved();
}
return string.Format("The sum of all numbers < %d is %d", (TOP_NUM + 1), fullSum);
}
}
//Functions //Functions
//Constructor //Constructor
public Problem1() : base("What is the sum of all the multiples of 3 or 5 that are less than 1000"){ public Problem1() : base(string.Format("What is the sum of all the multiples of 3 or 5 that are less than %d", TOP_NUM + 1)){
fullSum = 0; fullSum = 0;
} }
//Operational functions //Operational functions
@@ -68,9 +79,6 @@ namespace ProjectEulerCS.Problems{
//Thow a flag to show the problem is solved //Thow a flag to show the problem is solved
solved = true; solved = true;
//Save the results
_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(){

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCS/src/Problems/Problem10.cs //ProjectEuler/ProjectEulerCS/src/Problems/Problem10.cs
//Matthew Ellison //Matthew Ellison
// Created: 08-23-20 // Created: 08-23-20
//Modified: 08-23-20 //Modified: 08-27-20
//Find the sum of all the primes below two million //Find the sum of all the primes below two million
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses
/* /*
@@ -41,9 +41,19 @@ namespace ProjectEulerCS{
} }
} }
//The results of the problem
public override string Result{
get{
if(!solved){
throw new Unsolved();
}
return string.Format("The sum of all the primes < %d is %d", (GOAL_NUMBER + 1), sum);
}
}
//Functions //Functions
//Constructor //Constructor
public Problem10() : base("Find the sum of all the primes below two million"){ public Problem10() : base(string.Format("Find the sum of all the primes below %d.", GOAL_NUMBER + 1)){
sum = 0; sum = 0;
} }
//Operational functions //Operational functions
@@ -58,16 +68,13 @@ namespace ProjectEulerCS{
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(); 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;
//Save the results
_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(){

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCS/src/Problems/Problem11.cs //ProjectEuler/ProjectEulerCS/src/Problems/Problem11.cs
//Matthew Ellison //Matthew Ellison
// Created: 08-24-20 // Created: 08-24-20
//Modified: 08-24-20 //Modified: 08-27-20
//What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid? //What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid?
/* /*
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
@@ -53,26 +53,26 @@ namespace ProjectEulerCS.Problems{
//Variables //Variables
//Static variables //Static variables
//This is the grid of numbers that we will be working with //This is the grid of numbers that we will be working with
private static readonly int[,] grid = {{8, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12, 50, 77, 91, 8}, private static readonly int[,] grid = {{ 8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8},
{49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 04, 56, 62, 00}, {49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0},
{81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 03, 49, 13, 36, 65}, {81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65},
{52, 70, 95, 23, 04, 60, 11, 42, 69, 24, 68, 56, 01, 32, 56, 71, 37, 02, 36, 91}, {52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91},
{22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80}, {22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80},
{24, 47, 32, 60, 99, 03, 45, 02, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50}, {24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50},
{32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70}, {32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70},
{67, 26, 20, 68, 02, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21}, {67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21},
{24, 55, 58, 05, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72}, {24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72},
{21, 36, 23, 9, 75, 00, 76, 44, 20, 45, 35, 14, 00, 61, 33, 97, 34, 31, 33, 95}, {21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95},
{78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 03, 80, 04, 62, 16, 14, 9, 53, 56, 92}, {78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92},
{16, 39, 05, 42, 96, 35, 31, 47, 55, 58, 88, 24, 00, 17, 54, 24, 36, 29, 85, 57}, {16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57},
{86, 56, 00, 48, 35, 71, 89, 07, 05, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58}, {86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58},
{19, 80, 81, 68, 05, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 04, 89, 55, 40}, {19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40},
{04, 52, 8, 83, 97, 35, 99, 16, 07, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66}, { 4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66},
{88, 36, 68, 87, 57, 62, 20, 72, 03, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69}, {88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69},
{04, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36}, { 4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36},
{20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 04, 36, 16}, {20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16},
{20, 73, 35, 29, 78, 31, 90, 01, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 05, 54}, {20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54},
{01, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 01, 89, 19, 67, 48}}; { 1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 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{
@@ -92,6 +92,16 @@ namespace ProjectEulerCS.Problems{
} }
} }
//The results of the problem
public override string Result{
get{
if(!solved){
throw new Unsolved();
}
return string.Format("The greatestProduct of 4 numbers in a list is %d\nThe numbers are [%s]", mee.Algorithms.GetProd(greatestProduct), string.Join(", ", greatestProduct));
}
}
//Functions //Functions
//Constructor //Constructor
public Problem11() : base("What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid?"){ public Problem11() : base("What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid?"){
@@ -191,10 +201,6 @@ namespace ProjectEulerCS.Problems{
//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
_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 //Reset the problem so it can be run again
public override void Reset(){ public override void Reset(){

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCS/src/Problems/Problem12.cs //ProjectEuler/ProjectEulerCS/src/Problems/Problem12.cs
//Matthew Ellison //Matthew Ellison
// Created: 08-24-20 // Created: 08-24-20
//Modified: 08-24-20 //Modified: 08-27-20
//What is the value of the first triangle number to have over five hundred divisors? //What is the value of the first triangle number to have over five hundred divisors?
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses
/* /*
@@ -68,6 +68,16 @@ namespace ProjectEulerCS.Problems{
} }
} }
//The result of the problem
public override string Result{
get{
if(!solved){
throw new Unsolved();
}
return string.Format("The triangular number %d is the sum of all numbers >= %d and has %d divisors", sum, (counter - 1), divisors.Count);
}
}
//Functions //Functions
//Constructor //Constructor
public Problem12() : base("What is the value of the first triangle number to have over five hundred divisors?"){ public Problem12() : base("What is the value of the first triangle number to have over five hundred divisors?"){
@@ -108,9 +118,6 @@ namespace ProjectEulerCS.Problems{
//Throw a flag to show the problem is sovled //Throw a flag to show the problem is sovled
solved = true; solved = true;
//Save the results
_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(){

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCS/src/Problems/Problem13.cs //ProjectEuler/ProjectEulerCS/src/Problems/Problem13.cs
//Matthew Ellison //Matthew Ellison
// Created: 08-24-20 // Created: 08-24-20
//Modified: 08-24-20 //Modified: 08-27-20
//Work out the first ten digits of the sum of the following one-hundred 50-digit numbers //Work out the first ten digits of the sum of the following one-hundred 50-digit numbers
/* /*
37107287533902102798797998220837590246510135740250 37107287533902102798797998220837590246510135740250
@@ -151,6 +151,16 @@ namespace ProjectEulerCS.Problems{
} }
} }
//The results of the problem
public override string Result{
get{
if(!solved){
throw new Unsolved();
}
return string.Format("The sum of all %d numbers is %d\nThe first 10 digits of the sum of the numbers is %d", nums.Count, sum, sum.ToString().Substring(0, 10));
}
}
//Functions //Functions
//Constructor //Constructor
public Problem13() : base("Work out the first ten digits of the sum of the one-hundred 50-digit numbers"){ public Problem13() : base("Work out the first ten digits of the sum of the one-hundred 50-digit numbers"){
@@ -279,10 +289,6 @@ namespace ProjectEulerCS.Problems{
//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
_result = "The sum of all " + nums.Count + " numbers is " + sum + "\n" +
"The first 10 digits of the sum of the numbers is " + sum.ToString().Substring(0, 10);
} }
//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(){

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCS/src/Problems/Problem14.cs //ProjectEuler/ProjectEulerCS/src/Problems/Problem14.cs
//Matthew Ellison //Matthew Ellison
// Created: 08-24-20 // Created: 08-24-20
//Modified: 08-24-20 //Modified: 08-27-20
/* /*
The following iterative sequence is defined for the set of positive integers: The following iterative sequence is defined for the set of positive integers:
n → n/2 (n is even) n → n/2 (n is even)
@@ -52,9 +52,18 @@ namespace ProjectEulerCS.Problems{
} }
} }
public override string Result{
get{
if(!solved){
throw new Unsolved();
}
return string.Format("The number %d producted a chain of %d steps", maxNum, maxLength);
}
}
//Functions //Functions
//Constructor //Constructor
public Problem14() : base("Which starting number, under one million, produces the longest chain using the itterative sequence?"){ public Problem14() : base(string.Format("Which starting number, under %d, produces the longest chain using the itterative sequence?", MAX_NUM + 1)){
maxLength = 0; maxLength = 0;
maxNum = 0; maxNum = 0;
} }
@@ -84,9 +93,6 @@ namespace ProjectEulerCS.Problems{
//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
_result = "The number " + maxNum + " produced a chain of " + maxLength + " steps";
} }
//This function follows the rules of the sequence and returns its length //This function follows the rules of the sequence and returns its length
private long CheckSeries(long num){ private long CheckSeries(long num){

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCS/src/Problems/Problem15.cs //ProjectEuler/ProjectEulerCS/src/Problems/Problem15.cs
//Matthew Ellison //Matthew Ellison
// Created: 08-25-20 // Created: 08-25-20
//Modified: 08-25-20 //Modified: 08-27-20
//How many routes from the top left corner to the bottom right corner are there through a 20×20 grid if you can only move right and down? //How many routes from the top left corner to the bottom right corner are there through a 20×20 grid if you can only move right and down?
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses
/* /*
@@ -39,6 +39,16 @@ namespace ProjectEulerCS.Problems{
} }
} }
//The results of the problem
public override string Result{
get{
if(!solved){
throw new Unsolved();
}
return string.Format("The number of routes is %d", numOfRoutes);
}
}
//Functions //Functions
//Constructor //Constructor
public Problem15() : base("How many routes from the top left corner to the bottom right corner are there through a 20x20 grid if you can only move right and down?"){ public Problem15() : base("How many routes from the top left corner to the bottom right corner are there through a 20x20 grid if you can only move right and down?"){
@@ -64,9 +74,6 @@ namespace ProjectEulerCS.Problems{
//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
_result = "The number of routes is " + numOfRoutes;
} }
//This function acts as a handler for moving the position on the grid and counting the distance //This function acts as a handler for moving the position on the grid and counting the distance
//It moves right first, then down //It moves right first, then down

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCS/src/Problems/Problem16.cs //ProjectEuler/ProjectEulerCS/src/Problems/Problem16.cs
//Matthew Ellison //Matthew Ellison
// Created: 08-25-20 // Created: 08-25-20
//Modified: 08-25-20 //Modified: 08-27-20
//What is the sum of the digits of the number 2^1000? //What is the sum of the digits of the number 2^1000?
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses
/* /*
@@ -52,9 +52,19 @@ namespace ProjectEulerCS.Problems{
} }
} }
//The results of the problem
public override string Result{
get{
if(!solved){
throw new Unsolved();
}
return string.Format("%d^%d = %d\nThe sum of the elements is %d", NUM_TO_POWER, POWER, num, sumOfElements);
}
}
//Functions //Functions
//Constructor //Constructor
public Problem16() : base("What is the sum of the digits of the number 2^1000?"){ public Problem16() : base(string.Format("What is the sum of the digits of the number %d^%d?", NUM_TO_POWER, POWER)){
num = 0; num = 0;
sumOfElements = 0; sumOfElements = 0;
} }
@@ -85,10 +95,6 @@ namespace ProjectEulerCS.Problems{
//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
_result = NUM_TO_POWER + "^" + POWER + " = " + num + "\n" +
"The sum of the elements is " + sumOfElements;
} }
//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(){

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCS/src/Problems/Problem17.cs //ProjectEuler/ProjectEulerCS/src/Problems/Problem17.cs
//Matthew Ellison //Matthew Ellison
// Created: 08-25-20 // Created: 08-25-20
//Modified: 08-25-20 //Modified: 08-27-20
//If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used? //If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses
/* /*
@@ -42,9 +42,19 @@ namespace ProjectEulerCS.Problems{
} }
} }
//The results of the problem
public override string Result{
get{
if(!solved){
throw new Unsolved();
}
return string.Format("The sum of all the letters in all the numbers %d-%d is %d", START_NUM, STOP_NUM, letterCounter);
}
}
//Functions //Functions
//Constructor //Constructor
public Problem17() : base("If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?"){ public Problem17() : base(string.Format("If all the numbers from %d to %d inclusive were written out in words, how many letters would be used?", START_NUM, STOP_NUM)){
letterCounter = 0; letterCounter = 0;
} }
//Operational functions //Operational functions
@@ -71,9 +81,6 @@ namespace ProjectEulerCS.Problems{
//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
_result = "The sum of all the letters in all the numbers " + START_NUM + "-" + STOP_NUM + " is " + letterCounter;
} }
//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(){

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCS/src/Problems/Problem18.cs //ProjectEuler/ProjectEulerCS/src/Problems/Problem18.cs
//Matthew Ellison //Matthew Ellison
// Created: 08-25-20 // Created: 08-25-20
//Modified: 08-26-20 //Modified: 08-27-20
//Find the maximum total from top to bottom //Find the maximum total from top to bottom
/* /*
75 75
@@ -61,6 +61,16 @@ namespace ProjectEulerCS.Problems{
} }
} }
//The results of the problem
public override string Result{
get{
if(!solved){
throw new Unsolved();
}
return string.Format("The value of the longest path is %d", actualTotal);
}
}
//Variables //Variables
//Static variables //Static variables
//The list to hold the numbers in //The list to hold the numbers in
@@ -244,9 +254,6 @@ namespace ProjectEulerCS.Problems{
} }
} }
//Stop the timer
timer.Stop();
//Invert the list again so it is correct //Invert the list again so it is correct
Invert(list); Invert(list);
@@ -254,10 +261,11 @@ namespace ProjectEulerCS.Problems{
//Get the correct total which will be the inversion of the current one //Get the correct total which will be the inversion of the current one
int actualTotal = ((100 * list.Count) - foundPoints[^1].Total); int actualTotal = ((100 * list.Count) - foundPoints[^1].Total);
//Stop the timer
timer.Stop();
//Throw a flag to show the problem is solved //Throw a flag to show the problem is solved
solved = true; solved = true;
_result = "The value of the longest path is " + actualTotal;
} }
//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(){

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCS/src/Problems/Problem19.cs //ProjectEuler/ProjectEulerCS/src/Problems/Problem19.cs
//Matthew Ellison //Matthew Ellison
// Created: 08-26-20 // Created: 08-26-20
//Modified: 08-26-20 //Modified: 08-27-20
//How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)? //How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
/* /*
You are given the following information, but you may prefer to do some research for yourself. You are given the following information, but you may prefer to do some research for yourself.
@@ -47,6 +47,15 @@ namespace ProjectEulerCS.Problems{
//Instance //Instance
private long totalSundays; //Keep track of the number of sundays private long totalSundays; //Keep track of the number of sundays
//The results of the problem
public override string Result{
get{
if(!solved){
throw new Unsolved();
}
return string.Format("There are %d Sundays that landed ont he first of the months from %d to %d", totalSundays, START_YEAR, END_YEAR);
}
}
//Functions //Functions
//Constructor //Constructor
public Problem19() : base("How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?"){ public Problem19() : base("How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?"){
@@ -82,8 +91,6 @@ namespace ProjectEulerCS.Problems{
//Throw a flag to show the problem is solved //Throw a flag to show the problem is solved
solved = true; solved = true;
_result = "There are " + totalSundays + " Sundays that landed on the first of the months from " + START_YEAR + " to " + END_YEAR;
} }
//Return the day of the week that the date you pass into it is on //Return the day of the week that the date you pass into it is on
private DAYS GetDay(int month, int day, int year){ private DAYS GetDay(int month, int day, int year){

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCS/src/Problems/Problem2.cs //ProjectEuler/ProjectEulerCS/src/Problems/Problem2.cs
//Matthew Ellison //Matthew Ellison
// Created: 08-23-20 // Created: 08-23-20
//Modified: 08-23-20 //Modified: 08-27-20
//The sum of the even Fibonacci numbers less than 4,000,000 //The sum of the even Fibonacci numbers less than 4,000,000
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses
/* /*
@@ -42,9 +42,19 @@ namespace ProjectEulerCS.Problems{
} }
} }
//The results of the problem
public override string Result{
get{
if(!solved){
throw new Unsolved();
}
return string.Format("The sum of all even fibonacci numbers <= %d is %d", TOP_NUM, fullSum);
}
}
//Functions //Functions
//Constructor //Constructor
public Problem2() : base("What is the sum of the even Fibonacci numbers less than 4,000,000?"){ public Problem2() : base(string.Format("What is the sum of the even Fibonacci numbers less than %d?", TOP_NUM + 1)){
fullSum = 0; fullSum = 0;
} }
//Operational functions //Operational functions
@@ -73,9 +83,6 @@ namespace ProjectEulerCS.Problems{
//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
_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(){

View File

@@ -48,7 +48,7 @@ namespace ProjectEulerCS.Problems{
return num.ToString(); return num.ToString();
} }
} }
private long sum; //THe sum of the digits of num private long sum; //The sum of the digits of num
public long Sum{ public long Sum{
get{ get{
if(!solved){ if(!solved){
@@ -58,9 +58,19 @@ namespace ProjectEulerCS.Problems{
} }
} }
//The results of the problem
public override string Result{
get{
if(!solved){
throw new Unsolved();
}
return string.Format("%d! = %d\nThe sum of the digits is: %d", TOP_NUM, num, sum);
}
}
//Functions //Functions
//Constructor //Constructor
public Problem20() : base("What is the sum of the digits of 100!?"){ public Problem20() : base(string.Format("What is the sum of the digits of %d!?", TOP_NUM)){
num = 1; num = 1;
sum = 0; sum = 0;
} }
@@ -93,9 +103,6 @@ namespace ProjectEulerCS.Problems{
//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
_result = TOP_NUM + "! = " + num + "\nThe sum of the digits 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(){

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCS/src/Problems/Problem3.cs //ProjectEuler/ProjectEulerCS/src/Problems/Problem3.cs
//Matthew Ellison //Matthew Ellison
// Created: 08-23-20 // Created: 08-23-20
//Modified: 08-23-20 //Modified: 08-27-20
//The largest prime factor of 600851475143 //The largest prime factor of 600851475143
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses
/* /*
@@ -30,7 +30,7 @@ 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 static long GoalNumber{
get{ return GOAL_NUMBER; } get{ return GOAL_NUMBER; }
} }
//Instance variables //Instance variables
@@ -52,9 +52,18 @@ namespace ProjectEulerCS.Problems{
} }
} }
//The results of the problem
public override string Result{
get{
if(!solved){
throw new Unsolved();
}
return string.Format("The largest factor of the number %d is %d", GOAL_NUMBER, factors[^1]);
}
}
//Functions //Functions
//Constructor //Constructor
public Problem3() : base("What is the largest prime factor of 600851475143?"){ public Problem3() : base(string.Format("What is the largest prime factor of %d?", GOAL_NUMBER)){
factors = new List<long>(); factors = new List<long>();
} }
//Operational functions //Operational functions
@@ -77,9 +86,6 @@ namespace ProjectEulerCS.Problems{
//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
_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(){

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCS/src/Problems/Problem4.cs //ProjectEuler/ProjectEulerCS/src/Problems/Problem4.cs
//Matthew Ellison //Matthew Ellison
// Created: 08-23-20 // Created: 08-23-20
//Modified: 08-23-20 //Modified: 08-27-20
//Find the largest palindrome made from the product of two 3-digit numbers //Find the largest palindrome made from the product of two 3-digit numbers
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses
/* /*
@@ -51,6 +51,16 @@ namespace ProjectEulerCS.Problems{
} }
} }
//The results of the problem
public override string Result{
get{
if(!solved){
throw new Unsolved();
}
return string.Format("The largest palindrome is %d", 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>();
@@ -93,9 +103,6 @@ namespace ProjectEulerCS.Problems{
//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
_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(){

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCS/src/Problems/Problem5.cs //ProjectEuler/ProjectEulerCS/src/Problems/Problem5.cs
//Matthew Ellison //Matthew Ellison
// Created: 08-23-20 // Created: 08-23-20
//Modified: 08-23-20 //Modified: 08-27-20
//What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? //What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses
/* /*
@@ -36,6 +36,16 @@ namespace ProjectEulerCS.Problems{
} }
} }
//The results of the problem
public override string Result{
get{
if(!solved){
throw new Unsolved();
}
return string.Format("The smallest positive number evenly divisible by all numbers 1-20 is %d", smallestNum);
}
}
//Functions //Functions
//Constructor //Constructor
public Problem5() : base("What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?"){ public Problem5() : base("What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?"){
@@ -79,9 +89,6 @@ namespace ProjectEulerCS.Problems{
//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
_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(){

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCS/src/Problems/Problem6.cs //ProjectEuler/ProjectEulerCS/src/Problems/Problem6.cs
//Matthew Ellison //Matthew Ellison
// Created: 08-23-20 // Created: 08-23-20
//Modified: 08-23-20 //Modified: 08-27-20
//Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum. //Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses
/* /*
@@ -59,9 +59,18 @@ namespace ProjectEulerCS.Problems{
} }
} }
//The results of the problem
public override string Result{
get{
if(!solved){
throw new Unsolved();
}
return string.Format("The difference between the sum of the squares and the square of the sum of all numbers from 1-100 is %d", 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(string.Format("Find the difference between the sum of the squares and the square of the sum of the numbers %d-%d.", START_NUM, END_NUM)){
sumOfSquares = 0; sumOfSquares = 0;
squareOfSum = 0; squareOfSum = 0;
} }
@@ -89,9 +98,6 @@ namespace ProjectEulerCS.Problems{
//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
_result = "The difference between the sum of the squares and the square of the sum of all numbers from 1-100 is " + 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(){

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCS/src/Problems/Problem7.cs //ProjectEuler/ProjectEulerCS/src/Problems/Problem7.cs
//Matthew Ellison //Matthew Ellison
// Created: 08-23-20 // Created: 08-23-20
//Modified: 08-23-20 //Modified: 08-27-20
//What is the 10001th prime number? //What is the 10001th prime number?
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses
/* /*
@@ -41,9 +41,19 @@ namespace ProjectEulerCS.Problems{
} }
} }
//The results of the problem
public override string Result{
get{
if(!solved){
throw new Unsolved();
}
return string.Format("The %dth prime number is %d", NUMBER_OF_PRIMES, primes[^1]);
}
}
//Functions //Functions
//Constructor //Constructor
public Problem7() : base("What is the 10001th prime number?"){ public Problem7() : base(string.Format("What is the %dth prime number?", NUMBER_OF_PRIMES)){
primes = new List<long>(); primes = new List<long>();
} }
//Operatinal functions //Operatinal functions
@@ -65,9 +75,6 @@ namespace ProjectEulerCS.Problems{
//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
_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(){

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCS/src/Problems/Problem8.cs //ProjectEuler/ProjectEulerCS/src/Problems/Problem8.cs
//Matthew Ellison //Matthew Ellison
// Created: 08-23-20 // Created: 08-23-20
//Modified: 08-23-20 //Modified: 08-27-20
//Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product? //Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
/* /*
73167176531330624919225119674426574742355349194934 73167176531330624919225119674426574742355349194934
@@ -73,6 +73,16 @@ namespace ProjectEulerCS.Problems{
} }
} }
//The results of the problem
public override string Result{
get{
if(!solved){
throw new Unsolved();
}
return string.Format("The greatest product is %d\nThe numbers are %d", maxProduct, maxNums);
}
}
//Functions //Functions
//Constructor //Constructor
public Problem8() : base("Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?"){ public Problem8() : base("Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?"){
@@ -106,9 +116,6 @@ namespace ProjectEulerCS.Problems{
//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
_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(){

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCS/src/Problems/Problem9.cs //ProjectEuler/ProjectEulerCS/src/Problems/Problem9.cs
//Matthew Ellison //Matthew Ellison
// Created: 08-23-20 // Created: 08-23-20
//Modified: 08-23-20 //Modified: 08-27-20
//There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc. //There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses
/* /*
@@ -68,9 +68,18 @@ namespace ProjectEulerCS{
} }
} }
//The results of the problem
public override string Result{
get{
if(!solved){
throw new Unsolved();
}
return string.Format("The Pythagorean triplet is %d + %d + %d\nThe numbers' product is %d", a, b, Math.Round(c), a * b * Math.Round(c));
}
}
//Functions //Functions
//Constrcutor //Constrcutor
public Problem9() : base("There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc."){ public Problem9() : base(string.Format("There exists exactly one Pythagorean triplet for which a + b + c = %d. Find the product abc.", GOAL_SUM)){
a = 1; a = 1;
b = 0; b = 0;
c = 0; c = 0;
@@ -111,14 +120,11 @@ namespace ProjectEulerCS{
timer.Stop(); timer.Stop();
//Throw a flag to show the problem is solved //Throw a flag to show the problem is solved
solved = true;
if(found){ if(found){
_result = "The Pythagorean triplet is " + a + " + " + b + " + " + Math.Round(c) + solved = true;
"\nThe numbers' product is " + Product;
} }
else{ else{
_result = "The number was not found!"; throw new Unsolved("THe problem was not solved!");
} }
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again