From b3078801810859f71a68d5b900724f7f04844001 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Thu, 27 Aug 2020 13:30:04 -0400 Subject: [PATCH] Changed how results are handled --- ProjectEulerCS/Problem.cs | 13 ++----- ProjectEulerCS/Problems/Problem1.cs | 18 +++++++--- ProjectEulerCS/Problems/Problem10.cs | 19 ++++++---- ProjectEulerCS/Problems/Problem11.cs | 52 ++++++++++++++++------------ ProjectEulerCS/Problems/Problem12.cs | 15 +++++--- ProjectEulerCS/Problems/Problem13.cs | 16 ++++++--- ProjectEulerCS/Problems/Problem14.cs | 16 ++++++--- ProjectEulerCS/Problems/Problem15.cs | 15 +++++--- ProjectEulerCS/Problems/Problem16.cs | 18 ++++++---- ProjectEulerCS/Problems/Problem17.cs | 17 ++++++--- ProjectEulerCS/Problems/Problem18.cs | 20 +++++++---- ProjectEulerCS/Problems/Problem19.cs | 13 +++++-- ProjectEulerCS/Problems/Problem2.cs | 17 ++++++--- ProjectEulerCS/Problems/Problem20.cs | 17 ++++++--- ProjectEulerCS/Problems/Problem3.cs | 18 ++++++---- ProjectEulerCS/Problems/Problem4.cs | 15 +++++--- ProjectEulerCS/Problems/Problem5.cs | 15 +++++--- ProjectEulerCS/Problems/Problem6.cs | 16 ++++++--- ProjectEulerCS/Problems/Problem7.cs | 17 ++++++--- ProjectEulerCS/Problems/Problem8.cs | 15 +++++--- ProjectEulerCS/Problems/Problem9.cs | 20 +++++++---- 21 files changed, 254 insertions(+), 128 deletions(-) diff --git a/ProjectEulerCS/Problem.cs b/ProjectEulerCS/Problem.cs index 4f47a03..1d72bcf 100644 --- a/ProjectEulerCS/Problem.cs +++ b/ProjectEulerCS/Problem.cs @@ -9,16 +9,8 @@ namespace ProjectEulerCS{ public abstract class Problem{ //Variables //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{ - get{ - if(!solved){ - throw new Unsolved(); - } - return _result; - } - } + //The results of the problem + public abstract string Result{get;} protected readonly string _description; //Holds the description of the problem public string Description{ get{ return _description; } @@ -55,7 +47,6 @@ namespace ProjectEulerCS{ public virtual void Reset(){ timer.Reset(); solved = false; - _result = null; } } } diff --git a/ProjectEulerCS/Problems/Problem1.cs b/ProjectEulerCS/Problems/Problem1.cs index 981491c..35b9e41 100644 --- a/ProjectEulerCS/Problems/Problem1.cs +++ b/ProjectEulerCS/Problems/Problem1.cs @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem1.cs //Matthew Ellison // 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 //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{ public class Problem1 : Problem{ //Variables + //Static variables private const int TOP_NUM = 999; //The largest number to tbe checked //Instance variables 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 //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; } //Operational functions @@ -68,9 +79,6 @@ namespace ProjectEulerCS.Problems{ //Thow a flag to show the problem is solved 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 public override void Reset(){ diff --git a/ProjectEulerCS/Problems/Problem10.cs b/ProjectEulerCS/Problems/Problem10.cs index c1ca319..9cd0da4 100644 --- a/ProjectEulerCS/Problems/Problem10.cs +++ b/ProjectEulerCS/Problems/Problem10.cs @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem10.cs //Matthew Ellison // Created: 08-23-20 -//Modified: 08-23-20 +//Modified: 08-27-20 //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 /* @@ -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 //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; } //Operational functions @@ -58,16 +68,13 @@ namespace ProjectEulerCS{ timer.Start(); //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 timer.Stop(); //Throw a flag to show the problem is solved 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 public override void Reset(){ diff --git a/ProjectEulerCS/Problems/Problem11.cs b/ProjectEulerCS/Problems/Problem11.cs index 23dd3bc..7848350 100644 --- a/ProjectEulerCS/Problems/Problem11.cs +++ b/ProjectEulerCS/Problems/Problem11.cs @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem11.cs //Matthew Ellison // 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? /* 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 //Static variables //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}, - {49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 04, 56, 62, 00}, - {81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 03, 49, 13, 36, 65}, - {52, 70, 95, 23, 04, 60, 11, 42, 69, 24, 68, 56, 01, 32, 56, 71, 37, 02, 36, 91}, + 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, 4, 56, 62, 0}, + {81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65}, + {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}, - {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}, - {67, 26, 20, 68, 02, 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}, - {21, 36, 23, 9, 75, 00, 76, 44, 20, 45, 35, 14, 00, 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}, - {16, 39, 05, 42, 96, 35, 31, 47, 55, 58, 88, 24, 00, 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}, - {19, 80, 81, 68, 05, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 04, 89, 55, 40}, - {04, 52, 8, 83, 97, 35, 99, 16, 07, 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}, - {04, 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, 73, 35, 29, 78, 31, 90, 01, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 05, 54}, - {01, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 01, 89, 19, 67, 48}}; + {67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21}, + {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, 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, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92}, + {16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57}, + {86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58}, + {19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40}, + { 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, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69}, + { 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, 4, 36, 16}, + {20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54}, + { 1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48}}; //Instance variables private List greatestProduct; //Holds the largest product we have found so far public List 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 //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?"){ @@ -191,10 +201,6 @@ namespace ProjectEulerCS.Problems{ //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" + - "The numbers are [" + string.Join(", ", greatestProduct) + "]"; } //Reset the problem so it can be run again public override void Reset(){ diff --git a/ProjectEulerCS/Problems/Problem12.cs b/ProjectEulerCS/Problems/Problem12.cs index bd83027..59bc6ab 100644 --- a/ProjectEulerCS/Problems/Problem12.cs +++ b/ProjectEulerCS/Problems/Problem12.cs @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem12.cs //Matthew Ellison // 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? //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 //Constructor 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 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 public override void Reset(){ diff --git a/ProjectEulerCS/Problems/Problem13.cs b/ProjectEulerCS/Problems/Problem13.cs index 8141dd2..b6fce2f 100644 --- a/ProjectEulerCS/Problems/Problem13.cs +++ b/ProjectEulerCS/Problems/Problem13.cs @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem13.cs //Matthew Ellison // 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 /* 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 //Constructor 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 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 public override void Reset(){ diff --git a/ProjectEulerCS/Problems/Problem14.cs b/ProjectEulerCS/Problems/Problem14.cs index d63d8d4..134b8af 100644 --- a/ProjectEulerCS/Problems/Problem14.cs +++ b/ProjectEulerCS/Problems/Problem14.cs @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem14.cs //Matthew Ellison // Created: 08-24-20 -//Modified: 08-24-20 +//Modified: 08-27-20 /* The following iterative sequence is defined for the set of positive integers: 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 //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; maxNum = 0; } @@ -84,9 +93,6 @@ namespace ProjectEulerCS.Problems{ //Throw a flag to show the problem is solved 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 private long CheckSeries(long num){ diff --git a/ProjectEulerCS/Problems/Problem15.cs b/ProjectEulerCS/Problems/Problem15.cs index cfc850d..13b20ae 100644 --- a/ProjectEulerCS/Problems/Problem15.cs +++ b/ProjectEulerCS/Problems/Problem15.cs @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem15.cs //Matthew Ellison // 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? //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 //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?"){ @@ -64,9 +74,6 @@ namespace ProjectEulerCS.Problems{ //Throw a flag to show the problem is solved 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 //It moves right first, then down diff --git a/ProjectEulerCS/Problems/Problem16.cs b/ProjectEulerCS/Problems/Problem16.cs index a077c09..4cd1fe4 100644 --- a/ProjectEulerCS/Problems/Problem16.cs +++ b/ProjectEulerCS/Problems/Problem16.cs @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem16.cs //Matthew Ellison // Created: 08-25-20 -//Modified: 08-25-20 +//Modified: 08-27-20 //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 /* @@ -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 //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; sumOfElements = 0; } @@ -85,10 +95,6 @@ namespace ProjectEulerCS.Problems{ //Throw a flag to show the problem is solved 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 public override void Reset(){ diff --git a/ProjectEulerCS/Problems/Problem17.cs b/ProjectEulerCS/Problems/Problem17.cs index d100146..c67c0f3 100644 --- a/ProjectEulerCS/Problems/Problem17.cs +++ b/ProjectEulerCS/Problems/Problem17.cs @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem17.cs //Matthew Ellison // 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? //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 //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; } //Operational functions @@ -71,9 +81,6 @@ namespace ProjectEulerCS.Problems{ //Throw a flag to show the problem is solved 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 public override void Reset(){ diff --git a/ProjectEulerCS/Problems/Problem18.cs b/ProjectEulerCS/Problems/Problem18.cs index a0d7d7f..7535931 100644 --- a/ProjectEulerCS/Problems/Problem18.cs +++ b/ProjectEulerCS/Problems/Problem18.cs @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem18.cs //Matthew Ellison // Created: 08-25-20 -//Modified: 08-26-20 +//Modified: 08-27-20 //Find the maximum total from top to bottom /* 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 //Static variables //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(list); @@ -254,10 +261,11 @@ namespace ProjectEulerCS.Problems{ //Get the correct total which will be the inversion of the current one int actualTotal = ((100 * list.Count) - foundPoints[^1].Total); + //Stop the timer + timer.Stop(); + //Throw a flag to show the problem is solved solved = true; - - _result = "The value of the longest path is " + actualTotal; } //Reset the problem so it can be run again public override void Reset(){ diff --git a/ProjectEulerCS/Problems/Problem19.cs b/ProjectEulerCS/Problems/Problem19.cs index 17acafb..d87eb42 100644 --- a/ProjectEulerCS/Problems/Problem19.cs +++ b/ProjectEulerCS/Problems/Problem19.cs @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem19.cs //Matthew Ellison // 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)? /* You are given the following information, but you may prefer to do some research for yourself. @@ -47,6 +47,15 @@ namespace ProjectEulerCS.Problems{ //Instance 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 //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)?"){ @@ -82,8 +91,6 @@ namespace ProjectEulerCS.Problems{ //Throw a flag to show the problem is solved 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 private DAYS GetDay(int month, int day, int year){ diff --git a/ProjectEulerCS/Problems/Problem2.cs b/ProjectEulerCS/Problems/Problem2.cs index f7882cd..e83e592 100644 --- a/ProjectEulerCS/Problems/Problem2.cs +++ b/ProjectEulerCS/Problems/Problem2.cs @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem2.cs //Matthew Ellison // Created: 08-23-20 -//Modified: 08-23-20 +//Modified: 08-27-20 //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 /* @@ -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 //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; } //Operational functions @@ -73,9 +83,6 @@ namespace ProjectEulerCS.Problems{ //Throw a flag to show the problem is solved 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 public override void Reset(){ diff --git a/ProjectEulerCS/Problems/Problem20.cs b/ProjectEulerCS/Problems/Problem20.cs index a0802b4..af29e8a 100644 --- a/ProjectEulerCS/Problems/Problem20.cs +++ b/ProjectEulerCS/Problems/Problem20.cs @@ -48,7 +48,7 @@ namespace ProjectEulerCS.Problems{ 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{ get{ 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 //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; sum = 0; } @@ -93,9 +103,6 @@ namespace ProjectEulerCS.Problems{ //Throw a flag to show the problem is solved 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 public override void Reset(){ diff --git a/ProjectEulerCS/Problems/Problem3.cs b/ProjectEulerCS/Problems/Problem3.cs index 5b57151..61a708a 100644 --- a/ProjectEulerCS/Problems/Problem3.cs +++ b/ProjectEulerCS/Problems/Problem3.cs @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem3.cs //Matthew Ellison // Created: 08-23-20 -//Modified: 08-23-20 +//Modified: 08-27-20 //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 /* @@ -30,7 +30,7 @@ namespace ProjectEulerCS.Problems{ //Variables //Static variables private const long GOAL_NUMBER = 600851475143L; //The number that needs factored - public long GoalNumber{ + public static long GoalNumber{ get{ return GOAL_NUMBER; } } //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 //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(); } //Operational functions @@ -77,9 +86,6 @@ namespace ProjectEulerCS.Problems{ //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[^1]; } //Reset the porblem so it can be run again public override void Reset(){ diff --git a/ProjectEulerCS/Problems/Problem4.cs b/ProjectEulerCS/Problems/Problem4.cs index efe2c20..aa52c6b 100644 --- a/ProjectEulerCS/Problems/Problem4.cs +++ b/ProjectEulerCS/Problems/Problem4.cs @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem4.cs //Matthew Ellison // 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 //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 public Problem4() : base("Find the largest palindrome made from the product of two 3-digit numbers"){ palindromes = new List(); @@ -93,9 +103,6 @@ namespace ProjectEulerCS.Problems{ //Throw a flag to show the problem is solved solved = true; - - //Save the results - _result = "The largest palindrome is " + palindromes[^1]; } //Reset the problem so it can be run again public override void Reset(){ diff --git a/ProjectEulerCS/Problems/Problem5.cs b/ProjectEulerCS/Problems/Problem5.cs index 43fae23..882b7ea 100644 --- a/ProjectEulerCS/Problems/Problem5.cs +++ b/ProjectEulerCS/Problems/Problem5.cs @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem5.cs //Matthew Ellison // 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? //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 //Constructor 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 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 public override void Reset(){ diff --git a/ProjectEulerCS/Problems/Problem6.cs b/ProjectEulerCS/Problems/Problem6.cs index e2e3b81..7615055 100644 --- a/ProjectEulerCS/Problems/Problem6.cs +++ b/ProjectEulerCS/Problems/Problem6.cs @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem6.cs //Matthew Ellison // 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. //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 //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; squareOfSum = 0; } @@ -89,9 +98,6 @@ namespace ProjectEulerCS.Problems{ //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 " + Math.Abs(sumOfSquares - squareOfSum); } //Reset the problem so it can be run again public override void Reset(){ diff --git a/ProjectEulerCS/Problems/Problem7.cs b/ProjectEulerCS/Problems/Problem7.cs index 576e462..bc8ebb2 100644 --- a/ProjectEulerCS/Problems/Problem7.cs +++ b/ProjectEulerCS/Problems/Problem7.cs @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem7.cs //Matthew Ellison // Created: 08-23-20 -//Modified: 08-23-20 +//Modified: 08-27-20 //What is the 10001th prime number? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses /* @@ -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 //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(); } //Operatinal functions @@ -65,9 +75,6 @@ namespace ProjectEulerCS.Problems{ //Throw a flag to show the problem is solved 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 public override void Reset(){ diff --git a/ProjectEulerCS/Problems/Problem8.cs b/ProjectEulerCS/Problems/Problem8.cs index 42079fa..dd9f934 100644 --- a/ProjectEulerCS/Problems/Problem8.cs +++ b/ProjectEulerCS/Problems/Problem8.cs @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem8.cs //Matthew Ellison // 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? /* 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 //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?"){ @@ -106,9 +116,6 @@ namespace ProjectEulerCS.Problems{ //Throw a flag to show the problem is solved solved = true; - - //Save the results - _result = "The greatest product is " + maxProduct + "\nThe numbers are " + maxNums; } //Reset the problem so it can be run again public override void Reset(){ diff --git a/ProjectEulerCS/Problems/Problem9.cs b/ProjectEulerCS/Problems/Problem9.cs index 8c61c66..365eef9 100644 --- a/ProjectEulerCS/Problems/Problem9.cs +++ b/ProjectEulerCS/Problems/Problem9.cs @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem9.cs //Matthew Ellison // 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. //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 //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; b = 0; c = 0; @@ -111,14 +120,11 @@ namespace ProjectEulerCS{ timer.Stop(); //Throw a flag to show the problem is solved - solved = true; - if(found){ - _result = "The Pythagorean triplet is " + a + " + " + b + " + " + Math.Round(c) + - "\nThe numbers' product is " + Product; + solved = true; } else{ - _result = "The number was not found!"; + throw new Unsolved("THe problem was not solved!"); } } //Reset the problem so it can be run again