diff --git a/ProjectEulerCS/Problem.cs b/ProjectEulerCS/Problem.cs index 1d72bcf..e1a185c 100644 --- a/ProjectEulerCS/Problem.cs +++ b/ProjectEulerCS/Problem.cs @@ -1,8 +1,24 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem.cs //Matthew Ellison // Created: 08-14-20 -//Modified: 08-14-20 +//Modified: 07-05-21 //This is the base class for the rest of the problems +/* + Copyright (C) 2021 Matthew Ellison + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . +*/ namespace ProjectEulerCS{ @@ -34,10 +50,17 @@ namespace ProjectEulerCS{ } } + //Make sure the problem has been solved and throw an exception if not + protected void SolvedCheck(string str){ + if(!solved){ + throw new Unsolved("You must solve the problem before you can see the " + str); + } + } + //Constructor public Problem(string newDescription){ - this._description = newDescription; + _description = newDescription; } //Operations functions diff --git a/ProjectEulerCS/Problems/Problem1.cs b/ProjectEulerCS/Problems/Problem1.cs index d3eaf84..8e8cfa1 100644 --- a/ProjectEulerCS/Problems/Problem1.cs +++ b/ProjectEulerCS/Problems/Problem1.cs @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem1.cs //Matthew Ellison // Created: 08-14-20 -//Modified: 10-26-20 +//Modified: 07-05-21 //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 /* - Copyright (C) 2020 Matthew Ellison + Copyright (C) 2021 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -34,9 +34,7 @@ namespace ProjectEulerCS.Problems{ private int fullSum; //The sum of all the numbers public int Sum{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("sum"); return fullSum; } } @@ -44,9 +42,7 @@ namespace ProjectEulerCS.Problems{ //The results of the problem public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return $"The sum of all numbers < {TOP_NUM + 1} is {fullSum}"; } } @@ -67,9 +63,11 @@ namespace ProjectEulerCS.Problems{ //Start the timer timer.Start(); + //Get the sum of the progressions of 3 and 5 and remove the sum of progressions of the overlap fullSum = SumOfProgression(3) + SumOfProgression(5) - SumOfProgression(3 * 5); + //Stop the timer timer.Stop(); @@ -89,7 +87,8 @@ namespace ProjectEulerCS.Problems{ } } + /* Results: The sum of all numbers < 1000 is 233168 It took an average of 1.351 microseconds to run this problem through 100 iterations -*/ \ No newline at end of file +*/ diff --git a/ProjectEulerCS/Problems/Problem10.cs b/ProjectEulerCS/Problems/Problem10.cs index d2e8ed5..26bdb6b 100644 --- a/ProjectEulerCS/Problems/Problem10.cs +++ b/ProjectEulerCS/Problems/Problem10.cs @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem10.cs //Matthew Ellison // Created: 08-23-20 -//Modified: 08-27-20 +//Modified: 07-05-21 //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 /* - Copyright (C) 2020 Matthew Ellison + Copyright (C) 2021 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -34,9 +34,7 @@ namespace ProjectEulerCS{ private long sum; //The sum of all the prime numbers public long Sum{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("sum"); return sum; } } @@ -44,9 +42,7 @@ namespace ProjectEulerCS{ //The results of the problem public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return $"The sum of all the primes < {GOAL_NUMBER + 1} is {sum}"; } } @@ -67,8 +63,10 @@ namespace ProjectEulerCS{ //Start the timer timer.Start(); + //Get the sum of all prime numbers < GOAL_NUMBER - sum = mee.Algorithms.GetPrimes(GOAL_NUMBER).Sum(); + sum = mee.NumberAlgorithms.GetPrimes(GOAL_NUMBER).Sum(); + //Stop the timer timer.Stop(); @@ -84,6 +82,7 @@ namespace ProjectEulerCS{ } } + /* Results: The sum of all the primes < 2000000 is 142913828922 It took an average of 165.413 milliseconds to run this problem through 100 iterations diff --git a/ProjectEulerCS/Problems/Problem11.cs b/ProjectEulerCS/Problems/Problem11.cs index f2c1513..efac40f 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-27-20 +//Modified: 07-05-21 //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 @@ -27,7 +27,7 @@ */ //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses /* - Copyright (C) 2020 Matthew Ellison + Copyright (C) 2021 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -77,28 +77,22 @@ namespace ProjectEulerCS.Problems{ private List greatestProduct; //Holds the largest product we have found so far public List Numbers{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("numbers"); return greatestProduct; } } public int Product{ get{ - if(!solved){ - throw new Unsolved(); - } - return mee.Algorithms.GetProd(greatestProduct); + SolvedCheck("product of the numbers"); + return mee.ArrayAlgorithms.GetProd(greatestProduct); } } //The results of the problem public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } - return $"The greatest product of 4 numbers in a line is {mee.Algorithms.GetProd(greatestProduct)}\nThe numbers are [{string.Join(", ", greatestProduct)}]"; + SolvedCheck("result"); + return $"The greatest product of 4 numbers in a line is {mee.ArrayAlgorithms.GetProd(greatestProduct)}\nThe numbers are [{string.Join(", ", greatestProduct)}]"; } } @@ -121,6 +115,7 @@ namespace ProjectEulerCS.Problems{ //Start the timer timer.Start(); + //Loop through every row and column for(int row = 0;row < grid.GetLength(0);++row){ for(int col = 0;col < grid.GetLength(1);++col){ @@ -150,7 +145,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.ArrayAlgorithms.GetProd(currentProduct) > mee.ArrayAlgorithms.GetProd(greatestProduct)){ greatestProduct = currentProduct.ToList(); } } @@ -163,7 +158,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.ArrayAlgorithms.GetProd(currentProduct) > mee.ArrayAlgorithms.GetProd(greatestProduct)){ greatestProduct = currentProduct.ToList(); } } @@ -176,7 +171,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.ArrayAlgorithms.GetProd(currentProduct) > mee.ArrayAlgorithms.GetProd(greatestProduct)){ greatestProduct = currentProduct.ToList(); } } @@ -189,13 +184,14 @@ 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.ArrayAlgorithms.GetProd(currentProduct) > mee.ArrayAlgorithms.GetProd(greatestProduct)){ greatestProduct = currentProduct.ToList(); } } } } + //Stop the timer timer.Stop(); @@ -210,6 +206,7 @@ namespace ProjectEulerCS.Problems{ } } + /* Results: The greatest product of 4 numbers in a line is 70600674 The numbers are [89, 94, 97, 87] diff --git a/ProjectEulerCS/Problems/Problem12.cs b/ProjectEulerCS/Problems/Problem12.cs index a821352..be0a036 100644 --- a/ProjectEulerCS/Problems/Problem12.cs +++ b/ProjectEulerCS/Problems/Problem12.cs @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem12.cs //Matthew Ellison // Created: 08-24-20 -//Modified: 08-27-20 +//Modified: 07-05-21 //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 /* - Copyright (C) 2020 Matthew Ellison + Copyright (C) 2021 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -37,33 +37,25 @@ namespace ProjectEulerCS.Problems{ private List divisors; //Holds the divisors of the triangular number sum public long TriangularNumber{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("triangular number"); return sum; } } public long LastNumberAdded{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("last number added to get the triangular number"); return counter - 1; } } public List DivisorsOfTriangularNumber{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("divisors of the triangular number"); return divisors; } } public int NumberOfDivisors{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("number of divisors of the triangular number"); return divisors.Count; } } @@ -71,9 +63,7 @@ namespace ProjectEulerCS.Problems{ //The result of the problem public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return $"The triangular number {sum} is the sum of all numbers >= {counter - 1} and has {divisors.Count} divisors"; } } @@ -99,9 +89,10 @@ namespace ProjectEulerCS.Problems{ //Start the timer timer.Start(); + //Loop until you fin the appropriate number while((!foundNumber) && (sum > 0)){ - divisors = mee.Algorithms.GetDivisors(sum); + divisors = mee.NumberAlgorithms.GetDivisors(sum); //If the number of divisors is correct set the flag if(divisors.Count > GOAL_DIVISORS){ foundNumber = true; @@ -113,6 +104,7 @@ namespace ProjectEulerCS.Problems{ } } + //Stop the timer timer.Stop(); @@ -129,6 +121,7 @@ namespace ProjectEulerCS.Problems{ } } + /* Results: The triangular number 76576500 is the sum of all numbers >= 12375 and has 576 divisors It took an average of 270.496 milliseconds to run this problem through 100 iterations diff --git a/ProjectEulerCS/Problems/Problem13.cs b/ProjectEulerCS/Problems/Problem13.cs index afa808d..3c6a28f 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-27-20 +//Modified: 07-05-21 //Work out the first ten digits of the sum of the following one-hundred 50-digit numbers /* 37107287533902102798797998220837590246510135740250 @@ -107,7 +107,7 @@ */ //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses /* - Copyright (C) 2020 Matthew Ellison + Copyright (C) 2021 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -135,18 +135,14 @@ namespace ProjectEulerCS.Problems{ private readonly List nums; //The numbers that are being summed public List Numbers{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("numbers"); return nums; } } private BigInteger sum; //The sum of all the numbers public BigInteger Sum{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("sum"); return sum; } } @@ -154,9 +150,7 @@ namespace ProjectEulerCS.Problems{ //The results of the problem public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return $"The sum of all {nums.Count} numbers is {sum}\nThe first 10 digits of the sum of the numbers is {sum.ToString().Substring(0, 10)}"; } } @@ -281,8 +275,10 @@ namespace ProjectEulerCS.Problems{ //Start the timer timer.Start(); + //Get the sum of all the number - sum = mee.Algorithms.GetSum(nums); + sum = mee.ArrayAlgorithms.GetSum(nums); + //Stop the timer timer.Stop(); @@ -300,6 +296,7 @@ namespace ProjectEulerCS.Problems{ } } + /* Results: The sum of all 100 numbers is 5537376230390876637302048746832985971773659831892672 The first 10 digits of the sum of the numbers is 5537376230 diff --git a/ProjectEulerCS/Problems/Problem14.cs b/ProjectEulerCS/Problems/Problem14.cs index 07dfc1d..4675c1d 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-27-20 +//Modified: 07-05-21 /* The following iterative sequence is defined for the set of positive integers: n → n/2 (n is even) @@ -10,7 +10,7 @@ Which starting number, under one million, produces the longest chain? */ //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses /* - Copyright (C) 2020 Matthew Ellison + Copyright (C) 2021 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -36,27 +36,21 @@ namespace ProjectEulerCS.Problems{ private long maxLength; //This is the length of the longest chain public long Length{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("length of the longest chain"); return maxLength; } } private long maxNum; //This is the starting number of the longest chain public long StartingNumber{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("starting number of the longest chain"); return maxNum; } } public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return $"The number {maxNum} produced a chain of {maxLength} steps"; } } @@ -78,6 +72,7 @@ namespace ProjectEulerCS.Problems{ //Start the timer timer.Start(); + //Loop through all numbers <= MAX_NUM and check them against the series for(long currentNum = 1;currentNum <= MAX_NUM;++currentNum){ long currentLength = CheckSeries(currentNum); @@ -88,6 +83,7 @@ namespace ProjectEulerCS.Problems{ } } + //Stop the timer timer.Stop(); @@ -121,6 +117,7 @@ namespace ProjectEulerCS.Problems{ } } + /* Results: The number 837799 produced a chain of 525 steps It took an average of 249.883 milliseconds to run this problem through 100 iterations diff --git a/ProjectEulerCS/Problems/Problem15.cs b/ProjectEulerCS/Problems/Problem15.cs index 910b2ad..336ba3f 100644 --- a/ProjectEulerCS/Problems/Problem15.cs +++ b/ProjectEulerCS/Problems/Problem15.cs @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem15.cs //Matthew Ellison // Created: 08-25-20 -//Modified: 08-27-20 +//Modified: 07-05-21 //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 /* - Copyright (C) 2020 Matthew Ellison + Copyright (C) 2021 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -32,9 +32,7 @@ namespace ProjectEulerCS.Problems{ private long numOfRoutes; //The number of routes from 0, 0 to 20, 20 public long NumberOfRoutes{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("number of routes"); return numOfRoutes; } } @@ -42,9 +40,7 @@ namespace ProjectEulerCS.Problems{ //The results of the problem public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return $"The number of routes is {numOfRoutes}"; } } @@ -65,10 +61,12 @@ namespace ProjectEulerCS.Problems{ //Start the timer timer.Start(); + //We write this as a recursive function //When in a location it always moves right first, then down Move(0, 0); + //Stop the timer timer.Stop(); @@ -102,6 +100,7 @@ namespace ProjectEulerCS.Problems{ } } + /* Results The number of routes is 137846528820 It took 17.328 minutes to solve this problem. diff --git a/ProjectEulerCS/Problems/Problem16.cs b/ProjectEulerCS/Problems/Problem16.cs index 3cfb0be..17cac0b 100644 --- a/ProjectEulerCS/Problems/Problem16.cs +++ b/ProjectEulerCS/Problems/Problem16.cs @@ -36,18 +36,14 @@ namespace ProjectEulerCS.Problems{ private BigInteger num; //The number to be calculated public BigInteger Number{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("number"); return num; } } private int sumOfElements; //The sum of all digits in the number public int Sum{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("sum"); return sumOfElements; } } @@ -55,9 +51,7 @@ namespace ProjectEulerCS.Problems{ //The results of the problem public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return $"{NUM_TO_POWER}^{POWER} = {num}\nThe sum of the elements is {sumOfElements}"; } } @@ -79,6 +73,7 @@ namespace ProjectEulerCS.Problems{ //Start the timer timer.Start(); + //Get the number num = BigInteger.Pow(NUM_TO_POWER, POWER); @@ -90,6 +85,7 @@ namespace ProjectEulerCS.Problems{ sumOfElements += Convert.ToInt32(numString.Substring(cnt, 1)); } + //Stop the timer timer.Stop(); @@ -105,6 +101,7 @@ namespace ProjectEulerCS.Problems{ } } + /* Results: 2^1000 = 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376 The sum of the elements is 1366 diff --git a/ProjectEulerCS/Problems/Problem17.cs b/ProjectEulerCS/Problems/Problem17.cs index e3b55ee..6d0db37 100644 --- a/ProjectEulerCS/Problems/Problem17.cs +++ b/ProjectEulerCS/Problems/Problem17.cs @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem17.cs //Matthew Ellison // Created: 08-25-20 -//Modified: 08-27-20 +//Modified: 07-05-21 //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 /* - Copyright (C) 2020 Matthew Ellison + Copyright (C) 2021 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -35,9 +35,7 @@ namespace ProjectEulerCS.Problems{ private long letterCounter; //This is the cumulative number of letters in the words of the numbers public long LetterCount{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("letter count"); return letterCounter; } } @@ -45,9 +43,7 @@ namespace ProjectEulerCS.Problems{ //The results of the problem public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return $"The sum of all the letters in all the numbers {START_NUM}-{STOP_NUM} is {letterCounter}"; } } @@ -68,6 +64,7 @@ namespace ProjectEulerCS.Problems{ //Start the timer timer.Start(); + //Start with 1 and increment for(int num = START_NUM;num <= STOP_NUM;++num){ //Pass the number to a function that will create a string for the number @@ -76,6 +73,7 @@ namespace ProjectEulerCS.Problems{ letterCounter += GetNumberChars(currentNumString); } + //Stop the timer timer.Stop(); @@ -199,6 +197,7 @@ namespace ProjectEulerCS.Problems{ } } + /* Result: The sum of all the letters in all the numbers 1-1000 is 21124 It took an average of 208.222 microseconds to run this problem through 100 iterations diff --git a/ProjectEulerCS/Problems/Problem18.cs b/ProjectEulerCS/Problems/Problem18.cs index 834306a..6cc4577 100644 --- a/ProjectEulerCS/Problems/Problem18.cs +++ b/ProjectEulerCS/Problems/Problem18.cs @@ -64,9 +64,7 @@ namespace ProjectEulerCS.Problems{ //The results of the problem public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return $"The value of the longest path is {actualTotal}"; } } @@ -94,9 +92,7 @@ namespace ProjectEulerCS.Problems{ } public string Trail{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("trail of the shortest path"); string results = ""; //Save the trail the algorithm took @@ -151,9 +147,7 @@ namespace ProjectEulerCS.Problems{ } public int Total{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("total"); return actualTotal; } } @@ -212,6 +206,7 @@ namespace ProjectEulerCS.Problems{ //Start the timer timer.Start(); + //Invert the list Invert(list); @@ -261,6 +256,7 @@ namespace ProjectEulerCS.Problems{ //Get the correct total which will be the inversion of the current one actualTotal = ((100 * list.Count) - foundPoints[^1].Total); + //Stop the timer timer.Stop(); @@ -277,6 +273,7 @@ namespace ProjectEulerCS.Problems{ } } + /* Results: The value of the longest path is 1074 It took an average of 32.856 microseconds to run this problem through 100 iterations diff --git a/ProjectEulerCS/Problems/Problem19.cs b/ProjectEulerCS/Problems/Problem19.cs index 089b4c8..6f2b534 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-27-20 +//Modified: 07-05-21 //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. @@ -16,7 +16,7 @@ A leap year occurs on any year evenly divisible by 4, but not on a century unles */ //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses /* - Copyright (C) 2020 Matthew Ellison + Copyright (C) 2021 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -50,9 +50,7 @@ namespace ProjectEulerCS.Problems{ //The results of the problem public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return $"There are {totalSundays} Sundays that landed on the first of the months from {START_YEAR} to {END_YEAR}"; } } @@ -72,6 +70,7 @@ namespace ProjectEulerCS.Problems{ //Start the timer timer.Start(); + //Run for all years 1901-2000 for(int year = START_YEAR;year <= END_YEAR;++year){ //Run for all months in the year @@ -86,6 +85,7 @@ namespace ProjectEulerCS.Problems{ } } + //Stop the timer timer.Stop(); @@ -203,6 +203,7 @@ namespace ProjectEulerCS.Problems{ } } + /* Results: There are 171 Sundays that landed on the first of the months from 1901 to 2000 It took an average of 6.001 milliseconds to run this problem through 100 iterations diff --git a/ProjectEulerCS/Problems/Problem2.cs b/ProjectEulerCS/Problems/Problem2.cs index 3ecc0fb..7b611f5 100644 --- a/ProjectEulerCS/Problems/Problem2.cs +++ b/ProjectEulerCS/Problems/Problem2.cs @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem2.cs //Matthew Ellison // Created: 08-23-20 -//Modified: 08-27-20 +//Modified: 07-05-21 //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 /* - Copyright (C) 2020 Matthew Ellison + Copyright (C) 2021 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -35,9 +35,7 @@ namespace ProjectEulerCS.Problems{ private int fullSum; //Holds the sum of all the numbers public int Sum{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("sum"); return fullSum; } } @@ -45,9 +43,7 @@ namespace ProjectEulerCS.Problems{ //The results of the problem public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return $"The sum of all even fibonacci numbers <= {TOP_NUM} is {fullSum}"; } } @@ -68,8 +64,9 @@ namespace ProjectEulerCS.Problems{ //Start the timer timer.Start(); + //Get a list of all fibonacci numbers < 4,000,000 - List fibNums = mee.Algorithms.GetAllFib(TOP_NUM); + List fibNums = mee.NumberAlgorithms.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 @@ -78,6 +75,7 @@ namespace ProjectEulerCS.Problems{ } } + //Stop the timer timer.Stop(); @@ -92,6 +90,7 @@ namespace ProjectEulerCS.Problems{ } } + /* Results: The sum of all even fibonacci numbers <= 3999999 is 4613732 It took an average of 5.810 microseconds to run this problem through 100 iterations diff --git a/ProjectEulerCS/Problems/Problem20.cs b/ProjectEulerCS/Problems/Problem20.cs index 70d310e..a68d924 100644 --- a/ProjectEulerCS/Problems/Problem20.cs +++ b/ProjectEulerCS/Problems/Problem20.cs @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem20.cs //Matthew Ellison // Created: 08-27-20 -//Modified: 08-27-20 +//Modified: 07-05-21 //What is the sum of the digits of 100!? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses /* - Copyright (C) 2020 Matthew Ellison + Copyright (C) 2021 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -34,26 +34,20 @@ namespace ProjectEulerCS.Problems{ private BigInteger num; //Holds the number 100! public BigInteger Number{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("number"); return num; } } public string NumberString{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("number as a string"); return num.ToString(); } } private long sum; //The sum of the digits of num public long Sum{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("sum of the digits"); return sum; } } @@ -61,9 +55,7 @@ namespace ProjectEulerCS.Problems{ //The results of the problem public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return $"{TOP_NUM}! = {num}\nThe sum of the digits is: {sum}"; } } @@ -85,6 +77,7 @@ namespace ProjectEulerCS.Problems{ //Start the timer timer.Start(); + //Run through every number from 1 to 100 and multiply it by the current num to generate 100! for(int cnt = TOP_NUM;cnt > 1;--cnt){ num *= cnt; @@ -98,6 +91,7 @@ namespace ProjectEulerCS.Problems{ sum += (long)char.GetNumericValue(digit); } + //Stop the timer timer.Stop(); @@ -113,6 +107,7 @@ namespace ProjectEulerCS.Problems{ } } + /* Results: 100! = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 The sum of the digits is: 648 diff --git a/ProjectEulerCS/Problems/Problem21.cs b/ProjectEulerCS/Problems/Problem21.cs index 3fd18d2..0d5c19e 100644 --- a/ProjectEulerCS/Problems/Problem21.cs +++ b/ProjectEulerCS/Problems/Problem21.cs @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem21.cs //Matthew Ellison // Created: 09-02-20 -//Modified: 09-02-20 +//Modified: 07-05-21 //Evaluate the sum of all the amicable numbers under 10000 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses /* - Copyright (C) 2020 Matthew Ellison + Copyright (C) 2021 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -35,25 +35,19 @@ namespace ProjectEulerCS.Problems{ private readonly List amicable; //Holds all amicable numbers public List Amicable{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("amicable numbers"); return amicable; } } public int Sum{ get{ - if(!solved){ - throw new Unsolved(); - } - return mee.Algorithms.GetSum(amicable); + SolvedCheck("sum of the amicable numbers"); + return mee.ArrayAlgorithms.GetSum(amicable); } } public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); string result = $"All amicable numebrs less than {LIMIT} are\n"; foreach (int num in amicable){ result += $"{num}\n"; @@ -89,13 +83,14 @@ namespace ProjectEulerCS.Problems{ //Start the timer timer.Start(); + //Generate the divisors of all numbers < 10000, get their sum, and add it to the list for(int cnt = 1;cnt < LIMIT;++cnt){ - List divisors = mee.Algorithms.GetDivisors(cnt); //Get all the divisors of a number + List divisors = mee.NumberAlgorithms.GetDivisors(cnt); //Get all the divisors of a number if(divisors.Count > 1){ divisors.Remove(divisors[^1]); //Remove the last entry because it will be the number itself } - divisorSum[cnt] = mee.Algorithms.GetSum(divisors); //Add the sum of the divisors of the vector + divisorSum[cnt] = mee.ArrayAlgorithms.GetSum(divisors); //Add the sum of the divisors of the vector } //Check every sum of divisors in the list for a matching sum for(int cnt = 1;cnt < divisorSum.Count;++cnt){ @@ -118,6 +113,7 @@ namespace ProjectEulerCS.Problems{ //Sort the list for neatness amicable.Sort(); + //Stop the timer timer.Stop(); @@ -134,6 +130,7 @@ namespace ProjectEulerCS.Problems{ } } + /* Results: All amicable numebrs less than 10000 are 220 diff --git a/ProjectEulerCS/Problems/Problem22.cs b/ProjectEulerCS/Problems/Problem22.cs index 116f3a6..03fec1a 100644 --- a/ProjectEulerCS/Problems/Problem22.cs +++ b/ProjectEulerCS/Problems/Problem22.cs @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem22.cs //Matthew Ellison // Created: 09-02-20 -//Modified: 09-02-20 +//Modified: 07-05-21 //What is the total of all the name scores in this file? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses /* - Copyright (C) 2020 Matthew Ellison + Copyright (C) 2021 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -405,22 +405,19 @@ namespace ProjectEulerCS.Problems{ private long sum; //Holds the sum of the scores public List Names{ get{ + SolvedCheck("names"); return names; } } public long NameScoreSum{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("score of the names"); return sum; } } public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return $"The answer to the question is {sum}"; } } @@ -441,6 +438,7 @@ namespace ProjectEulerCS.Problems{ //Start the timer timer.Start(); + //Sort all the names names.Sort(); //Step through every name adding up the values of the characters @@ -458,7 +456,8 @@ namespace ProjectEulerCS.Problems{ } //Get the sum of all the numbers - sum = mee.Algorithms.GetSum(prod); + sum = mee.ArrayAlgorithms.GetSum(prod); + //Stop the timer timer.Stop(); @@ -476,6 +475,7 @@ namespace ProjectEulerCS.Problems{ } } + /* Results: The answer to the question is 871198282 It took an average of 3.180 milliseconds to run this problem through 100 iterations diff --git a/ProjectEulerCS/Problems/Problem23.cs b/ProjectEulerCS/Problems/Problem23.cs index 5852fce..09433ce 100644 --- a/ProjectEulerCS/Problems/Problem23.cs +++ b/ProjectEulerCS/Problems/Problem23.cs @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem23.cs //Matthew Ellison // Created: 09-03-20 -//Modified: 09-03-20 +//Modified: 07-05-21 //Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses /* - Copyright (C) 2020 Matthew Ellison + Copyright (C) 2021 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -35,17 +35,13 @@ namespace ProjectEulerCS.Problems{ private long sum; //The sum of all the numbers we are looking for public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return $"The answer is {sum}"; } } public long Sum{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("sum"); return sum; } } @@ -76,14 +72,15 @@ namespace ProjectEulerCS.Problems{ //Start the timer timer.Start(); + //Get the sum of the divisors of all numbers < MAX_NUM for(int cnt = 1;cnt < MAX_NUM;++cnt){ - List div = mee.Algorithms.GetDivisors(cnt); + List div = mee.NumberAlgorithms.GetDivisors(cnt); //Remove the last element, which is the number itself. This gives us the propper divisors if(div.Count > 1){ div.Remove(div[^1]); } - divisorSums[cnt] = mee.Algorithms.GetSum(div); + divisorSums[cnt] = mee.ArrayAlgorithms.GetSum(div); } //Get the abundant numbers @@ -101,6 +98,7 @@ namespace ProjectEulerCS.Problems{ } } + //Stop the timer timer.Stop(); @@ -136,7 +134,8 @@ namespace ProjectEulerCS.Problems{ } } + /* Results: The answer is 4179871 It took an average of 11.420 seconds to run this problem through 100 iterations -*/ \ No newline at end of file +*/ diff --git a/ProjectEulerCS/Problems/Problem24.cs b/ProjectEulerCS/Problems/Problem24.cs index 70dfa5b..1ac87ea 100644 --- a/ProjectEulerCS/Problems/Problem24.cs +++ b/ProjectEulerCS/Problems/Problem24.cs @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem24.cs //Matthew Ellison // Created: 09-03-20 -//Modified: 09-04-20 +//Modified: 07-05-21 //What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses /* - Copyright (C) 2020 Matthew Ellison + Copyright (C) 2021 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -35,25 +35,19 @@ namespace ProjectEulerCS.Problems{ private List permutations; //Holds all of the permutations of the string nums public List PermutationsList{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("permutations"); return permutations; } } public string Permutation{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("1,000,000th permutation"); return permutations[NEEDED_PERM - 1]; } } public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return $"The 1 millionth permutation is {Permutation}"; } } @@ -74,8 +68,10 @@ namespace ProjectEulerCS.Problems{ //Start the timer timer.Start(); + //Get all the permutations of the string - permutations = mee.Algorithms.GetPermutations(nums); + permutations = mee.StringAlgorithms.GetPermutations(nums); + //Stop the timer timer.Stop(); @@ -91,6 +87,7 @@ namespace ProjectEulerCS.Problems{ } } + /* Results: The 1 millionth permutation is 2783915460 It took an average of 5.865 seconds to run this problem through 100 iterations diff --git a/ProjectEulerCS/Problems/Problem25.cs b/ProjectEulerCS/Problems/Problem25.cs index 5862506..1a60555 100644 --- a/ProjectEulerCS/Problems/Problem25.cs +++ b/ProjectEulerCS/Problems/Problem25.cs @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem25.cs //Matthew Ellison // Created: 09-11-20 -//Modified: 09-11-20 +//Modified: 07-05-21 //What is the index of the first term in the Fibonacci sequence to contain 1000 digits? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses /* - Copyright (C) 2020 Matthew Ellison + Copyright (C) 2021 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -35,25 +35,19 @@ namespace ProjectEulerCS.Problems{ private BigInteger index; //The index of the current Fibonacci number just calculated public BigInteger Number{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("fibonacci number"); return number; } } public BigInteger Index{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("index of the fibonacci number"); return index; } } public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return $"The first Fibonacci number with {NUM_DIGITS} digits is {number}\nIts index is {index}"; } } @@ -75,12 +69,14 @@ namespace ProjectEulerCS.Problems{ //Star the timer timer.Start(); + //Move through all Fibonacci numbers until you reach the one with at least NUM_DIGITS digits while(number.ToString().Length < NUM_DIGITS){ index += 1; //Increase the index number. Doing this at the beginning keeps the index correct at the end of the loop - number = mee.Algorithms.GetFib(index); //Calculate the number + number = mee.NumberAlgorithms.GetFib(index); //Calculate the number } + //Stop the timer timer.Stop(); @@ -96,6 +92,7 @@ namespace ProjectEulerCS.Problems{ } } + /* Results: The first Fibonacci number with 1000 digits is 1070066266382758936764980584457396885083683896632151665013235203375314520604694040621889147582489792657804694888177591957484336466672569959512996030461262748092482186144069433051234774442750273781753087579391666192149259186759553966422837148943113074699503439547001985432609723067290192870526447243726117715821825548491120525013201478612965931381792235559657452039506137551467837543229119602129934048260706175397706847068202895486902666185435124521900369480641357447470911707619766945691070098024393439617474103736912503231365532164773697023167755051595173518460579954919410967778373229665796581646513903488154256310184224190259846088000110186255550245493937113651657039447629584714548523425950428582425306083544435428212611008992863795048006894330309773217834864543113205765659868456288616808718693835297350643986297640660000723562917905207051164077614812491885830945940566688339109350944456576357666151619317753792891661581327159616877487983821820492520348473874384736771934512787029218636250627816 Its index is 4782 diff --git a/ProjectEulerCS/Problems/Problem26.cs b/ProjectEulerCS/Problems/Problem26.cs index 3fe3036..278e935 100644 --- a/ProjectEulerCS/Problems/Problem26.cs +++ b/ProjectEulerCS/Problems/Problem26.cs @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem26.cs //Matthew Ellison // Created: 09-11-20 -//Modified: 09-11-20 +//Modified: 07-05-21 //Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part. //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses /* - Copyright (C) 2020 Matthew Ellison + Copyright (C) 2021 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -35,25 +35,19 @@ namespace ProjectEulerCS.Problems{ private int longestNumber; //The starting denominator of the longest cycle public int LongestCycle{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("length of the longest cycle"); return longestCycle; } } public int LongestNumber{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("denominator that starts the longest cycle"); return longestNumber; } } public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return $"The longest cycle is {longestCycle} digits long\nIt started with the number {longestNumber}"; } } @@ -75,6 +69,7 @@ namespace ProjectEulerCS.Problems{ //Start the timer timer.Start(); + //Start with 1/2 and find out how long the longest cycle is by checking the remainders //Loop through every number from 2-999 and use it for the denominator for(int denominator = 2;denominator <= TOP_NUM;++denominator){ @@ -111,6 +106,7 @@ namespace ProjectEulerCS.Problems{ } } + //Stop the timer timer.Stop(); @@ -126,6 +122,7 @@ namespace ProjectEulerCS.Problems{ } } + /* Results: The longest cycle is 982 digits long It started with the number 983 diff --git a/ProjectEulerCS/Problems/Problem27.cs b/ProjectEulerCS/Problems/Problem27.cs index 902ffff..2cefc35 100644 --- a/ProjectEulerCS/Problems/Problem27.cs +++ b/ProjectEulerCS/Problems/Problem27.cs @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem27.cs //Matthew Ellison // Created: 09-11-20 -//Modified: 09-11-20 +//Modified: 07-05-21 //Find the product of the coefficients, |a| < 1000 and |b| <= 1000, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n=0. //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses /* - Copyright (C) 2020 Matthew Ellison + Copyright (C) 2021 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -38,33 +38,31 @@ namespace ProjectEulerCS.Problems{ private List primes; //A list of all primes that could possibly be generated with this formula public int TOP_A{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("largest A"); return topA; } } public int TOP_B{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("largest B"); return topB; } } public int TOP_N{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("largest N"); return topN; } } + public int Product{ + get{ + SolvedCheck("product of A and B"); + return topA * topB; + } + } public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return $"The greatest number of primes found is {topN}\nIt was found with A = {topA}, B = {topB}\nThe product of A and B is {topA * topB}"; } } @@ -88,8 +86,9 @@ namespace ProjectEulerCS.Problems{ //Start the timer timer.Start(); + //Get the primes - primes = mee.Algorithms.GetPrimes(12000); + primes = mee.NumberAlgorithms.GetPrimes(12000); //Start with the lowest possible A and check all possibilities after that for(int a = -LARGEST_POSSIBLE_A;a <= LARGEST_POSSIBLE_A;++a){ @@ -113,6 +112,7 @@ namespace ProjectEulerCS.Problems{ } } + //Top the timer timer.Stop(); @@ -130,6 +130,7 @@ namespace ProjectEulerCS.Problems{ } } + /* Result: The greatest number of primes found is 70 It was found with A = -61, B = 971 diff --git a/ProjectEulerCS/Problems/Problem28.cs b/ProjectEulerCS/Problems/Problem28.cs index c427b69..2cbc5e6 100644 --- a/ProjectEulerCS/Problems/Problem28.cs +++ b/ProjectEulerCS/Problems/Problem28.cs @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem28.cs //Matthew Ellison // Created: 09-21-20 -//Modified: 09-21-20 +//Modified: 07-05-21 //What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed by starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses /* - Copyright (C) 2020 Matthew Ellison + Copyright (C) 2021 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -34,25 +34,19 @@ namespace ProjectEulerCS.Problems{ private int sumOfDiagonals; //Holds the sum of the diagonals of the grid public List> Grid{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("grid"); return grid; } } public int Sum{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("sum"); return sumOfDiagonals; } } public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return $"The sum of the diagonals in the given grid is {sumOfDiagonals}"; } } @@ -146,11 +140,13 @@ namespace ProjectEulerCS.Problems{ //Start the timer timer.Start(); + //Setup the grid SetupGrid(); //Find the sum of the diagonals in the grid FindSum(); + //Stop the timer timer.Stop(); @@ -165,6 +161,7 @@ namespace ProjectEulerCS.Problems{ } } + /* Results: The sum of the diagonals in the given grid is 669171001 It took an average of 7.735 milliseconds to run this problem through 100 iterations diff --git a/ProjectEulerCS/Problems/Problem29.cs b/ProjectEulerCS/Problems/Problem29.cs index bc87820..57d1f66 100644 --- a/ProjectEulerCS/Problems/Problem29.cs +++ b/ProjectEulerCS/Problems/Problem29.cs @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem29.cs //Matthew Ellison // Created: 10-03-20 -//Modified: 10-03-20 +//Modified: 07-05-21 //How many distinct terms are in the sequence generated by a^b for 2 <= a <= 100 and 2 <= b <= 100? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses /* - Copyright (C) 2020 Matthew Ellison + Copyright (C) 2021 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -44,7 +44,7 @@ namespace ProjectEulerCS.Problems{ } public static int BottomB{ get{ - return BottomB; + return BOTTOM_B; } } public static int TopA{ @@ -59,18 +59,19 @@ namespace ProjectEulerCS.Problems{ } public List Unique{ get{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("unique values for a^b"); return unique; } } + public int NumUnique{ + get{ + SolvedCheck("number of unique values for a^b"); + return unique.Count; + } + } public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return $"The number of unique values generated by a^b for {BOTTOM_A} <= a <= {TOP_A} and {BOTTOM_B} <= b <= {TOP_B} is {unique.Count}"; } } @@ -91,6 +92,7 @@ namespace ProjectEulerCS.Problems{ //Start the timer timer.Start(); + //Start with the first A and move towards the top for(int currentA = BOTTOM_A;currentA <= TOP_A;++currentA){ //Start with the first B and move towards the top @@ -104,6 +106,7 @@ namespace ProjectEulerCS.Problems{ } } + //Stop the timer timer.Stop(); @@ -117,6 +120,7 @@ namespace ProjectEulerCS.Problems{ } } + /* Results: The number of unique values generated by a^b for 2 <= a <= 100 and 2 <= b <= 100 is 9183 It took an average of 127.770 milliseconds to run this problem through 100 iterations diff --git a/ProjectEulerCS/Problems/Problem3.cs b/ProjectEulerCS/Problems/Problem3.cs index 40c2468..cb84656 100644 --- a/ProjectEulerCS/Problems/Problem3.cs +++ b/ProjectEulerCS/Problems/Problem3.cs @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem3.cs //Matthew Ellison // Created: 08-23-20 -//Modified: 08-27-20 +//Modified: 07-05-21 //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 /* - Copyright (C) 2020 Matthew Ellison + Copyright (C) 2021 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -37,17 +37,13 @@ namespace ProjectEulerCS.Problems{ private List factors; //Holds the factors of goalNumber public List Factors{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("factors"); return factors; } } public long LargestFactor{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("largest factor"); return factors[^1]; } } @@ -55,9 +51,7 @@ namespace ProjectEulerCS.Problems{ //The results of the problem public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return $"The largest factor of the number {GOAL_NUMBER} is {factors[^1]}"; } } @@ -77,10 +71,12 @@ namespace ProjectEulerCS.Problems{ //Star the timer timer.Start(); + //Get all the factors of the number - factors = mee.Algorithms.GetFactors(GOAL_NUMBER); + factors = mee.NumberAlgorithms.GetFactors(GOAL_NUMBER); //THe last element should be the largest factor + //Stop the timer timer.Stop(); @@ -95,6 +91,7 @@ namespace ProjectEulerCS.Problems{ } } + /*Results: The largest factor of the number 600851475143 is 6857 It took an average of 47.412 milliseconds to run this problem through 100 iterations diff --git a/ProjectEulerCS/Problems/Problem30.cs b/ProjectEulerCS/Problems/Problem30.cs index 779fe1c..1008422 100644 --- a/ProjectEulerCS/Problems/Problem30.cs +++ b/ProjectEulerCS/Problems/Problem30.cs @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem30.cs //Matthew Ellison // Created: 10-03-20 -//Modified: 10-03-20 +//Modified: 07-05-21 //Find the sum of all the numbers that can be written as the sum of the fifth powers of their digits. //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses /* - Copyright (C) 2020 Matthew Ellison + Copyright (C) 2021 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -39,33 +39,25 @@ namespace ProjectEulerCS.Problems{ //Gets public long TopNum{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("largest number checked"); return TOP_NUM; } } public List ListOfSumOfFifths{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("list of all numbers that are the sum of the 5th power of their digits"); return SumOfFifthNumbers; } } public long GetSumOfList{ get{ - if(!solved){ - throw new Unsolved(); - } - return mee.Algorithms.GetSum(SumOfFifthNumbers); + SolvedCheck("sum of all numbers that are the sum of the 5th power of their digits"); + return sum; } } public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return $"The sum of all the numbers that can be written as the sum of the fifth powers of their digits is {sum}"; } } @@ -99,6 +91,7 @@ namespace ProjectEulerCS.Problems{ //Start the timer timer.Start(); + //Start with the lowest number and increment until you reach the largest number for(long currentNum = BOTTOM_NUM;currentNum <= TOP_NUM;++currentNum){ //Get the digits of the number @@ -115,7 +108,8 @@ namespace ProjectEulerCS.Problems{ } } - sum = GetSumOfList; + sum = mee.ArrayAlgorithms.GetSum(SumOfFifthNumbers); + //Stop the timer timer.Stop(); @@ -132,6 +126,7 @@ namespace ProjectEulerCS.Problems{ } } + /* Results: The sum of all the numbers that can be written as the sum of the fifth powers of their digits is 443839 It took an average of 217.218 milliseconds to run this problem through 100 iterations diff --git a/ProjectEulerCS/Problems/Problem31.cs b/ProjectEulerCS/Problems/Problem31.cs index ce73c0c..a65cb6c 100644 --- a/ProjectEulerCS/Problems/Problem31.cs +++ b/ProjectEulerCS/Problems/Problem31.cs @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem31.cs //Matthew Ellison // Created: 10-03-20 -//Modified: 10-03-20 +//Modified: 07-05-21 //How many different ways can £2 be made using any number of coins? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses /* - Copyright (C) 2020 Matthew Ellison + Copyright (C) 2021 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -31,17 +31,13 @@ namespace ProjectEulerCS.Problems{ private int permutations; //The number of permutations that are found public int Permutations{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("number of correct permutations of the coins"); return permutations; } } public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return $"There are {permutations} ways to make 2 pounds with the given denominations of coins"; } } @@ -62,6 +58,7 @@ namespace ProjectEulerCS.Problems{ //Start the timer timer.Start(); + //Start with 200p and remove the necessary coins with each loop for(int pound2 = desiredValue; pound2 >= 0;pound2 -= 200){ for(int pound1 = pound2;pound1 >= 0;pound1 -= 100){ @@ -79,6 +76,7 @@ namespace ProjectEulerCS.Problems{ } } + //Stop the timer timer.Stop(); @@ -93,6 +91,7 @@ namespace ProjectEulerCS.Problems{ } } + /* Results: There are 73682 ways to make 2 pounds with the given denominations of coins It took an average of 117.971 microseconds to run this problem through 100 iterations diff --git a/ProjectEulerCS/Problems/Problem32.cs b/ProjectEulerCS/Problems/Problem32.cs index 28653fc..35c0338 100644 --- a/ProjectEulerCS/Problems/Problem32.cs +++ b/ProjectEulerCS/Problems/Problem32.cs @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem32.cs //Matthew Ellison // Created: 10-03-20 -//Modified: 10-03-20 +//Modified: 07-05-21 //Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital. //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses /* - Copyright (C) 2020 Matthew Ellison + Copyright (C) 2021 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -78,17 +78,13 @@ namespace ProjectEulerCS.Problems{ //Gets public long SumOfPandigitals{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("sum of the pandigitals"); return sumOfPandigitals; } } public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return $"There are {listOfProducts.Count} unique 1-9 pandigitals\nThe sum of the products of these pandigitals is {sumOfPandigitals}"; } } @@ -110,6 +106,7 @@ namespace ProjectEulerCS.Problems{ //Start the timer timer.Start(); + //Create the multiplicand and start working your way up for(int multiplicand = 1;multiplicand <= TOP_MULTIPLICAND;++multiplicand){ //Run through all possible multipliers @@ -133,6 +130,7 @@ namespace ProjectEulerCS.Problems{ sumOfPandigitals += prod.Product; } + //Stop the timer timer.Stop(); @@ -150,7 +148,7 @@ namespace ProjectEulerCS.Problems{ //Make sure every number from 1-9 is contained exactly once for(int panNumber = 1;panNumber <= 9;++panNumber){ //Make sure there is exactly one of this number contained in the string - if(mee.Algorithms.FindNumOccurrence(numberString, panNumber.ToString()[0]) != 1){ + if(mee.StringAlgorithms.FindNumOccurrence(numberString, panNumber.ToString()[0]) != 1){ return false; } } @@ -166,6 +164,7 @@ namespace ProjectEulerCS.Problems{ } } + /* Results: There are 7 unique 1-9 pandigitals The sum of the products of these pandigitals is 45228 diff --git a/ProjectEulerCS/Problems/Problem33.cs b/ProjectEulerCS/Problems/Problem33.cs index 61d545e..1b8b643 100644 --- a/ProjectEulerCS/Problems/Problem33.cs +++ b/ProjectEulerCS/Problems/Problem33.cs @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem33.cs //Matthew Ellison // Created: 02-06-21 -//Modified: 02-07-21 +//Modified: 07-05-21 /* The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s We shall consider fractions like, 30/50 = 3/5, to be trivial examples @@ -46,28 +46,28 @@ namespace ProjectEulerCS.Problems{ //The results of the problem public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return $"The denominator of the product is {prodDenominator}"; } } public List Numerators{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("list of numerators"); return numerators; } } public List Denominators{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("list of denominators"); return denominators; } } + public int ProdDenominator{ + get{ + SolvedCheck("result"); + return prodDenominator; + } + } //Functions //Constructor @@ -87,6 +87,7 @@ namespace ProjectEulerCS.Problems{ //Start the timer timer.Start(); + //Search every possible numerator/denominator pair for(int denominator = MIN_DENOMINATOR;denominator <= MAX_DENOMINATOR;++denominator){ for(int numerator = MIN_NUMERATOR;(numerator < denominator) &&(numerator <= MAX_NUMERATOR);++numerator){ @@ -126,13 +127,14 @@ namespace ProjectEulerCS.Problems{ } //Get the product of the numbers - int numProd = mee.Algorithms.GetProd(numerators); - int denomProd = mee.Algorithms.GetProd(denominators); + int numProd = mee.ArrayAlgorithms.GetProd(numerators); + int denomProd = mee.ArrayAlgorithms.GetProd(denominators); //Get the gcd to reduce to lowest terms - int gcd = mee.Algorithms.GCD(numProd, denomProd); + int gcd = mee.NumberAlgorithms.GCD(numProd, denomProd); //Save the denominator prodDenominator = denomProd / gcd; + //Stop the timer timer.Stop(); @@ -148,6 +150,7 @@ namespace ProjectEulerCS.Problems{ } } + /* Results: The denominator of the product is 100 It took an average of 221.559 microseconds to run this problem through 100 iterations diff --git a/ProjectEulerCS/Problems/Problem34.cs b/ProjectEulerCS/Problems/Problem34.cs index 406143a..0836d8d 100644 --- a/ProjectEulerCS/Problems/Problem34.cs +++ b/ProjectEulerCS/Problems/Problem34.cs @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem34.cs //Matthew Ellison // Created: 06-01-21 -//Modified: 06-01-21 +//Modified: 07-05-21 //Find the sum of all numbers which are equal to the sum of the factorial of their digits //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses /* @@ -37,27 +37,21 @@ namespace ProjectEulerCS.Problems{ //The results of the problem public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return $"The sum of all numbers that are the sum of their digit's factorials is {sum}"; } } //Returns the list of factorials from 0-9 public List Factorials{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("list of factorials"); return factorials; } } //Returns the sum of all numbers equal to the sum of their digit's factorials public int Sum{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("sum"); return sum; } } @@ -82,9 +76,10 @@ namespace ProjectEulerCS.Problems{ //Start the timer timer.Start(); + //Pre-compute the possible factorials from 0! to 9! for(int cnt = 0;cnt <= 9;++cnt){ - factorials[cnt] = mee.Algorithms.Factorial(cnt); + factorials[cnt] = mee.NumberAlgorithms.Factorial(cnt); } //Run through all possible numbers from 3-MAX_NUM and see if they equal the sum of their digit's factorials for(int cnt = 3;cnt < MAX_NUM;++cnt){ @@ -101,6 +96,7 @@ namespace ProjectEulerCS.Problems{ } } + //Stop the timer timer.Stop(); @@ -119,6 +115,7 @@ namespace ProjectEulerCS.Problems{ } } + /* Results: The sum of all numbers that are the sum of their digit's factorials is 40730 It took an average of 73.852 milliseconds to run this problem through 100 iterations diff --git a/ProjectEulerCS/Problems/Problem35.cs b/ProjectEulerCS/Problems/Problem35.cs index 87c6208..1eb7fc3 100644 --- a/ProjectEulerCS/Problems/Problem35.cs +++ b/ProjectEulerCS/Problems/Problem35.cs @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem35.cs //Matthew Ellison // Created: 06-05-21 -//Modified: 06-05-21 +//Modified: 07-05-21 //How many circular primes are there below one million? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses /* @@ -37,36 +37,28 @@ namespace ProjectEulerCS.Problems{ //The results of the problem public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return $"The number of all circular prime numbers under {MAX_NUM} is {circularPrimes.Count}"; } } //Returns the vector of primes < MAX_NUM public List Primes{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return primes; } } //Returns the vector of circular primes < MAX_NUM public List CircularPrimes{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return circularPrimes; } } //Returns the number of circular primes public int NumCircularPrimes{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("number of circular primes"); return circularPrimes.Count; } } @@ -97,8 +89,9 @@ namespace ProjectEulerCS.Problems{ //Start the timer timer.Start(); + //Get all primes under 1,000,000 - primes = mee.Algorithms.GetPrimes(MAX_NUM); + primes = mee.NumberAlgorithms.GetPrimes(MAX_NUM); //Go through all primes, get all their rotations, and check if those numbers are also primes foreach(int prime in primes){ bool allRotationsPrime = true; @@ -117,6 +110,7 @@ namespace ProjectEulerCS.Problems{ } } + //Stop the timer timer.Stop(); @@ -132,6 +126,7 @@ namespace ProjectEulerCS.Problems{ } } + /* Results: The number of all circular prime numbers under 999999 is 55 It took an average of 1.946 seconds to run this problem through 100 iterations diff --git a/ProjectEulerCS/Problems/Problem36.cs b/ProjectEulerCS/Problems/Problem36.cs index fb754b7..1ccbf28 100644 --- a/ProjectEulerCS/Problems/Problem36.cs +++ b/ProjectEulerCS/Problems/Problem36.cs @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem36.cs //Matthew Ellison // Created: 06-29-21 -//Modified: 06-29-21 +//Modified: 07-05-21 //Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2. //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses /* @@ -37,25 +37,19 @@ namespace ProjectEulerCS.Problems{ //The results of the problem public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return $"The sum of all base 10 and base 2 palindromic numbers < {MAX_NUM} is {sum}"; } } public List Palindromes{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("list of palindromes"); return palindromes; } } public int SumOfPalindromes{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("sum of all palindromes"); return sum; } } @@ -77,20 +71,22 @@ namespace ProjectEulerCS.Problems{ //Start the timer timer.Start(); + //Start with 1, check if it is a palindrome in base 10 and 2, and continue to MAX_NUM for(int num = 1;num < MAX_NUM;++num){ //Check if num is a palindrome - if(mee.Algorithms.IsPalindrome(num.ToString())){ + if(mee.StringAlgorithms.IsPalindrome(num.ToString())){ //Convert num to base 2 and see if that is a palindrome - string binNum = mee.Algorithms.ToBin(num); - if(mee.Algorithms.IsPalindrome(binNum)){ + string binNum = mee.NumberAlgorithms.ToBin(num); + if(mee.StringAlgorithms.IsPalindrome(binNum)){ //Add num to the list of palindromes palindromes.Add(num); } } } //Get the sum of all palindromes in the list - sum = mee.Algorithms.GetSum(palindromes); + sum = mee.ArrayAlgorithms.GetSum(palindromes); + //Stop the timer timer.Stop(); @@ -107,6 +103,7 @@ namespace ProjectEulerCS.Problems{ } } + /* Results: The sum of all base 10 and base 2 palindromic numbers < 999999 is 872187 It took an average of 140.447 milliseconds to run this problem through 100 iterations diff --git a/ProjectEulerCS/Problems/Problem37.cs b/ProjectEulerCS/Problems/Problem37.cs index 67298af..8e1cdb7 100644 --- a/ProjectEulerCS/Problems/Problem37.cs +++ b/ProjectEulerCS/Problems/Problem37.cs @@ -37,25 +37,19 @@ namespace ProjectEulerCS.Problems{ //The results of the problem public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return $"The sum of all left and right truncatable primes is {sum}"; } } public List TruncatablePrimes{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("list of truncatable primes"); return truncPrimes; } } public long SumOfTruncatablePrimes{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("sum of truncatable primes"); return sum; } } @@ -77,8 +71,9 @@ namespace ProjectEulerCS.Problems{ //Start the timer timer.Start(); + //Create the sieve and get the first prime number - IEnumerator sieve = mee.Algorithms.SieveOfEratosthenes().GetEnumerator(); + IEnumerator sieve = mee.NumberAlgorithms.SieveOfEratosthenes().GetEnumerator(); sieve.MoveNext(); //Loop through the sieve until you get to the LAST_PRIME_BEFORE_CHECK while(sieve.Current < LAST_PRIME_BEFORE_CHECK){ @@ -112,7 +107,7 @@ namespace ProjectEulerCS.Problems{ string primeSubstring = primeString[truncLoc..]; //Convert the string to an int and see if the number is still prime long newPrime = long.Parse(primeSubstring); - if(!mee.Algorithms.IsPrime(newPrime)){ + if(!mee.NumberAlgorithms.IsPrime(newPrime)){ isTruncPrime = false; break; } @@ -125,7 +120,7 @@ namespace ProjectEulerCS.Problems{ string primeSubstring = primeString.Substring(0, primeString.Length - truncLoc); //Conver the string to an int and see if the number is still prime long newPrime = long.Parse(primeSubstring); - if(!mee.Algorithms.IsPrime(newPrime)){ + if(!mee.NumberAlgorithms.IsPrime(newPrime)){ isTruncPrime = false; break; } @@ -137,7 +132,8 @@ namespace ProjectEulerCS.Problems{ } } //Get the sum of all elements in the trucPrimes vector - sum = mee.Algorithms.GetSum(truncPrimes); + sum = mee.ArrayAlgorithms.GetSum(truncPrimes); + //Stop the timer timer.Stop(); diff --git a/ProjectEulerCS/Problems/Problem4.cs b/ProjectEulerCS/Problems/Problem4.cs index 70d9622..eb565ec 100644 --- a/ProjectEulerCS/Problems/Problem4.cs +++ b/ProjectEulerCS/Problems/Problem4.cs @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem4.cs //Matthew Ellison // Created: 08-23-20 -//Modified: 08-27-20 +//Modified: 07-05-21 //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 /* - Copyright (C) 2020 Matthew Ellison + Copyright (C) 2021 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -36,17 +36,13 @@ namespace ProjectEulerCS.Problems{ private readonly List palindromes; //Holds all numbers that turn out to be palindromes public List Palindromes{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("palindromes"); return palindromes; } } public int LargestPalindrom{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("largest palindrome"); return palindromes[^1]; } } @@ -54,9 +50,7 @@ namespace ProjectEulerCS.Problems{ //The results of the problem public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return $"The largest palindrome is {palindromes[^1]}"; } } @@ -76,6 +70,7 @@ namespace ProjectEulerCS.Problems{ //Start the timer 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){ //You can start at the location of the first number because everything before that has already been tested. (100 * 101 == 101 * 100) @@ -98,6 +93,7 @@ namespace ProjectEulerCS.Problems{ //Sort the palindromes so that the last one is the largest palindromes.Sort(); + //Stop the timer timer.Stop(); @@ -112,6 +108,7 @@ namespace ProjectEulerCS.Problems{ } } + /* Results: The largest palindrome is 906609 It took an average of 51.682 milliseconds to run this problem through 100 iterations diff --git a/ProjectEulerCS/Problems/Problem5.cs b/ProjectEulerCS/Problems/Problem5.cs index 8fa08cb..7bc9de9 100644 --- a/ProjectEulerCS/Problems/Problem5.cs +++ b/ProjectEulerCS/Problems/Problem5.cs @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem5.cs //Matthew Ellison // Created: 08-23-20 -//Modified: 08-27-20 +//Modified: 07-05-21 //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 /* - Copyright (C) 2020 Matthew Ellison + Copyright (C) 2021 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -29,9 +29,7 @@ namespace ProjectEulerCS.Problems{ private int smallestNum; //The smallest number that is found public int Number{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("number"); return smallestNum; } } @@ -39,9 +37,7 @@ namespace ProjectEulerCS.Problems{ //The results of the problem public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return $"The smallest positive number evenly divisible by all numbers 1-20 is {smallestNum}"; } } @@ -62,6 +58,7 @@ namespace ProjectEulerCS.Problems{ //Start the timer 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 int currentNum = 20; //The number that it are currently checking against @@ -81,9 +78,10 @@ namespace ProjectEulerCS.Problems{ currentNum += 2; } } - + //Save the current num,ber as the smallest smallestNum = currentNum; + //Stop the timer timer.Stop(); @@ -98,6 +96,7 @@ namespace ProjectEulerCS.Problems{ } } + /* Results: The smallest positive number evenly divisible by all numbers 1-20 is 232792560 It took an average of 2.549 seconds to run this problem through 100 iterations diff --git a/ProjectEulerCS/Problems/Problem6.cs b/ProjectEulerCS/Problems/Problem6.cs index 8124cb0..7a1fd26 100644 --- a/ProjectEulerCS/Problems/Problem6.cs +++ b/ProjectEulerCS/Problems/Problem6.cs @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem6.cs //Matthew Ellison // Created: 08-23-20 -//Modified: 08-27-20 +//Modified: 07-05-21 //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 /* - Copyright (C) 2020 Matthew Ellison + Copyright (C) 2021 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -35,26 +35,20 @@ namespace ProjectEulerCS.Problems{ private long sumOfSquares; //Holds the sum of the squares of all the numbers public long SumOfSquares{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("sum of the squares"); return sumOfSquares; } } private long squareOfSum; //Holds the square of the sum of all the numbers public long SquareOfSum{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("square of the sums"); return squareOfSum; } } public long Difference{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("difference between the two numbers"); return Math.Abs(sumOfSquares - squareOfSum); } } @@ -62,9 +56,7 @@ namespace ProjectEulerCS.Problems{ //The results of the problem public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return $"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)}"; } } @@ -85,6 +77,7 @@ namespace ProjectEulerCS.Problems{ //Start the timer 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 @@ -93,6 +86,7 @@ namespace ProjectEulerCS.Problems{ //Squaring the sum that needs it squareOfSum *= squareOfSum; + //Stop the timer timer.Stop(); @@ -108,6 +102,7 @@ namespace ProjectEulerCS.Problems{ } } + /* Results: The difference between the sum of the squares and the square of the sum of all numbers from 1-100 is 25164150 It took an average of 234.000 nanoseconds to run this problem through 100 iterations diff --git a/ProjectEulerCS/Problems/Problem67.cs b/ProjectEulerCS/Problems/Problem67.cs index 894823c..534f6fa 100644 --- a/ProjectEulerCS/Problems/Problem67.cs +++ b/ProjectEulerCS/Problems/Problem67.cs @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem67.cs //Matthew Ellison // Created: 08-26-20 -//Modified: 08-26-20 +//Modified: 07-05-21 //Find the maximum total from top to bottom /* 59 @@ -107,7 +107,7 @@ */ //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses /* - Copyright (C) 2020 Matthew Ellison + Copyright (C) 2021 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -125,7 +125,6 @@ -using System; using System.Collections.Generic; @@ -240,6 +239,7 @@ namespace ProjectEulerCS.Problems{ } } + /* Results: The value of the longest path is 7273 It took an average of 216.985 milliseconds to run this problem through 100 iterations diff --git a/ProjectEulerCS/Problems/Problem7.cs b/ProjectEulerCS/Problems/Problem7.cs index 42c0d6a..3764092 100644 --- a/ProjectEulerCS/Problems/Problem7.cs +++ b/ProjectEulerCS/Problems/Problem7.cs @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem7.cs //Matthew Ellison // Created: 08-23-20 -//Modified: 08-27-20 +//Modified: 07-05-21 //What is the 10001th prime number? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses /* - Copyright (C) 2020 Matthew Ellison + Copyright (C) 2021 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -34,9 +34,7 @@ namespace ProjectEulerCS.Problems{ private List primes; public long Prime{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("prime"); return primes[^1]; } } @@ -44,9 +42,7 @@ namespace ProjectEulerCS.Problems{ //The results of the problem public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return $"The {NUMBER_OF_PRIMES}th prime number is {primes[^1]}"; } } @@ -67,8 +63,10 @@ namespace ProjectEulerCS.Problems{ //Start the timer timer.Start(); + //Setup the variables - primes = mee.Algorithms.GetNumPrimes(NUMBER_OF_PRIMES); //Holds the prime numbers + primes = mee.NumberAlgorithms.GetNumPrimes(NUMBER_OF_PRIMES); //Holds the prime numbers + //Stop the timer timer.Stop(); @@ -84,6 +82,7 @@ namespace ProjectEulerCS.Problems{ } } + /* Results: The 10001th prime number is 104743 It took an average of 3.559 milliseconds to run this problem through 100 iterations diff --git a/ProjectEulerCS/Problems/Problem8.cs b/ProjectEulerCS/Problems/Problem8.cs index 49ae650..bfda423 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-27-20 +//Modified: 07-05-21 //Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product? /* 73167176531330624919225119674426574742355349194934 @@ -27,7 +27,7 @@ */ //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses /* - Copyright (C) 2020 Matthew Ellison + Copyright (C) 2021 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -57,18 +57,14 @@ namespace ProjectEulerCS.Problems{ private string maxNums; //Holds the string of the largest product public string LargestNums{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("numbers that make the largest product"); return maxNums; } } private long maxProduct; //Holds the largest product of 13 numbers public long LargestProduct{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("product of the numbers"); return maxProduct; } } @@ -76,9 +72,7 @@ namespace ProjectEulerCS.Problems{ //The results of the problem public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return $"The greatest product is {maxProduct}\nThe numbers are {maxNums}"; } } @@ -100,6 +94,7 @@ namespace ProjectEulerCS.Problems{ //Start the timer timer.Start(); + //Cycle through the string of numbers looking for the maximum product for(int cnt = 12;cnt < NUMBER.Length;++cnt){ long currentProduct = Int64.Parse(NUMBER[cnt - 12].ToString()) * Int64.Parse(NUMBER[cnt - 11].ToString()) * Int64.Parse(NUMBER[cnt - 10].ToString()) * Int64.Parse(NUMBER[cnt - 9].ToString()) * Int64.Parse(NUMBER[cnt - 8].ToString()) * Int64.Parse(NUMBER[cnt - 7].ToString()) * Int64.Parse(NUMBER[cnt - 6].ToString()) * Int64.Parse(NUMBER[cnt - 5].ToString()) * Int64.Parse(NUMBER[cnt - 4].ToString()) * Int64.Parse(NUMBER[cnt - 3].ToString()) * Int64.Parse(NUMBER[cnt - 2].ToString()) * Int64.Parse(NUMBER[cnt - 1].ToString()) * Int64.Parse(NUMBER[cnt].ToString()); @@ -111,6 +106,7 @@ namespace ProjectEulerCS.Problems{ } } + //Stop the timer timer.Stop(); @@ -126,6 +122,7 @@ namespace ProjectEulerCS.Problems{ } } + /* Results: The greatest product is 23514624000 The numbers are 5576689664895 diff --git a/ProjectEulerCS/Problems/Problem9.cs b/ProjectEulerCS/Problems/Problem9.cs index 2acebbc..87e54d2 100644 --- a/ProjectEulerCS/Problems/Problem9.cs +++ b/ProjectEulerCS/Problems/Problem9.cs @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem9.cs //Matthew Ellison // Created: 08-23-20 -//Modified: 08-27-20 +//Modified: 07-05-21 //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 /* - Copyright (C) 2020 Matthew Ellison + Copyright (C) 2021 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -34,36 +34,28 @@ namespace ProjectEulerCS{ private int a; //Holds the size of the first side public int SideA{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("first side"); return a; } } private int b; //Holds the size of the second side public int SideB{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("second side"); return b; } } private double c; //Holds the size of the hyp public int SideC{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("third side"); return (int)c; } } private bool found; //A flag to determine if we have found the solution yet public int Product{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("product of all three sides"); return a * b * (int)c; } } @@ -71,9 +63,7 @@ namespace ProjectEulerCS{ //The results of the problem public override string Result{ get{ - if(!solved){ - throw new Unsolved(); - } + SolvedCheck("result"); return $"The Pythagorean triplet is {a} + {b} + {Math.Round(c)}\nThe numbers' product is {a * b * Math.Round(c)}"; } } @@ -96,6 +86,7 @@ namespace ProjectEulerCS{ //Start the timer timer.Start(); + //Loop through all possible a's while((a < GOAL_SUM) && !found){ b = a + 1; //b must be larget than a @@ -116,6 +107,7 @@ namespace ProjectEulerCS{ } } + //Stop the timer timer.Stop(); @@ -138,6 +130,7 @@ namespace ProjectEulerCS{ } } + /* Results: The Pythagorean triplet is 200 + 375 + 425 The numbers' product is 31875000