diff --git a/Headers/Problem.hpp b/Headers/Problem.hpp index 9103dfd..bbb1ff8 100644 --- a/Headers/Problem.hpp +++ b/Headers/Problem.hpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Headers/Problem.hpp +//ProjectEuler/ProjectEulerCPP/headers/Problem.hpp //Matthew Ellison // Created: 07-04-19 -//Modified: 07-09-20 +//Modified: 08-28-20 //This is an abstract base class to allow polymorphism for the individual problems /* Copyright (C) 2020 Matthew Ellison @@ -35,7 +35,6 @@ protected: mee::Stopwatch timer; //Used to determine your algorithm's run time bool solved; //Holds true after the problem has been solved class Unsolved{}; //An exception class thrown if you try to access something before it has been solved - std::stringstream result; //Get the result of the problem public: //Constructors Problem() : solved(false){ @@ -67,20 +66,14 @@ public: } //Reset the problem so it can be run again virtual void reset(){ - result.str(std::string()); timer.reset(); solved = false; } - //Return a string with the solution to the problem - virtual std::string getResults(){ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - return result.str(); - } //Pure virtual functions - virtual void solve() = 0; //Solve the problem + //Solve the problem + virtual void solve() = 0; + //Return a string with the solution to the problem + virtual std::string getResult() = 0; }; #endif //PROBLEM_HPP diff --git a/ProblemSelection.hpp b/Headers/ProblemSelection.hpp similarity index 75% rename from ProblemSelection.hpp rename to Headers/ProblemSelection.hpp index bad5117..7b917cc 100644 --- a/ProblemSelection.hpp +++ b/Headers/ProblemSelection.hpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/ProblemSelection.hpp -//Mattrixwv +//ProjectEuler/ProjectEulerCPP/headers/ProblemSelection.hpp +//Matthew Ellison // Created: 07-08-20 -//Modified: 07-09-20 +//Modified: 08-28-20 //This is a header file with a few functions to help select and run problems /* Copyright (C) 2020 Matthew Ellison @@ -26,39 +26,39 @@ #include #include "Algorithms.hpp" -#include "Headers/Problem1.hpp" -#include "Headers/Problem2.hpp" -#include "Headers/Problem3.hpp" -#include "Headers/Problem4.hpp" -#include "Headers/Problem5.hpp" -#include "Headers/Problem6.hpp" -#include "Headers/Problem7.hpp" -#include "Headers/Problem8.hpp" -#include "Headers/Problem9.hpp" -#include "Headers/Problem10.hpp" -#include "Headers/Problem11.hpp" -#include "Headers/Problem12.hpp" -#include "Headers/Problem13.hpp" -#include "Headers/Problem14.hpp" -#include "Headers/Problem15.hpp" -#include "Headers/Problem16.hpp" -#include "Headers/Problem17.hpp" -#include "Headers/Problem18.hpp" -#include "Headers/Problem19.hpp" -#include "Headers/Problem20.hpp" -#include "Headers/Problem21.hpp" -#include "Headers/Problem22.hpp" -#include "Headers/Problem23.hpp" -#include "Headers/Problem24.hpp" -#include "Headers/Problem25.hpp" -#include "Headers/Problem26.hpp" -#include "Headers/Problem27.hpp" -#include "Headers/Problem28.hpp" -#include "Headers/Problem29.hpp" -#include "Headers/Problem30.hpp" -#include "Headers/Problem31.hpp" -#include "Headers/Problem32.hpp" -#include "Headers/Problem67.hpp" +#include "Problems/Problem1.hpp" +#include "Problems/Problem2.hpp" +#include "Problems/Problem3.hpp" +#include "Problems/Problem4.hpp" +#include "Problems/Problem5.hpp" +#include "Problems/Problem6.hpp" +#include "Problems/Problem7.hpp" +#include "Problems/Problem8.hpp" +#include "Problems/Problem9.hpp" +#include "Problems/Problem10.hpp" +#include "Problems/Problem11.hpp" +#include "Problems/Problem12.hpp" +#include "Problems/Problem13.hpp" +#include "Problems/Problem14.hpp" +#include "Problems/Problem15.hpp" +#include "Problems/Problem16.hpp" +#include "Problems/Problem17.hpp" +#include "Problems/Problem18.hpp" +#include "Problems/Problem19.hpp" +#include "Problems/Problem20.hpp" +#include "Problems/Problem21.hpp" +#include "Problems/Problem22.hpp" +#include "Problems/Problem23.hpp" +#include "Problems/Problem24.hpp" +#include "Problems/Problem25.hpp" +#include "Problems/Problem26.hpp" +#include "Problems/Problem27.hpp" +#include "Problems/Problem28.hpp" +#include "Problems/Problem29.hpp" +#include "Problems/Problem30.hpp" +#include "Problems/Problem31.hpp" +#include "Problems/Problem32.hpp" +#include "Problems/Problem67.hpp" //Setup the problem numbers @@ -122,7 +122,7 @@ void solveProblem(Problem* problem){ //Solve the problem problem->solve(); //Print the results - std::cout << problem->getResults() + std::cout << problem->getResult() << "\nIt took " << problem->getTime() << " to solve this problem.\n\n" << std::endl; } diff --git a/Headers/Problem1.hpp b/Headers/Problems/Problem1.hpp similarity index 91% rename from Headers/Problem1.hpp rename to Headers/Problems/Problem1.hpp index 1859f10..eb0a610 100644 --- a/Headers/Problem1.hpp +++ b/Headers/Problems/Problem1.hpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Headers/Problem1.hpp +//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem1.hpp //Matthew Ellison // Created: 09-28-18 -//Modified: 07-09-20 +//Modified: 08-28-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/myClasses /* @@ -45,6 +45,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets + virtual std::string getResult(); //Return a string with the solution to the problem uint64_t getSum() const; //Returns the requested sum }; diff --git a/Headers/Problem10.hpp b/Headers/Problems/Problem10.hpp similarity index 90% rename from Headers/Problem10.hpp rename to Headers/Problems/Problem10.hpp index 8f1e3f1..e4b9952 100644 --- a/Headers/Problem10.hpp +++ b/Headers/Problems/Problem10.hpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Headers/Problem10.hpp +//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem10.hpp //Matthew Ellison // Created: 09-28-18 -//Modified: 07-09-20 +//Modified: 08-28-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/myClasses /* @@ -44,6 +44,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets + virtual std::string getResult(); //Return a string with the solution to the problem uint64_t getSum() const; //Returns the sum that was requested }; diff --git a/Headers/Problem11.hpp b/Headers/Problems/Problem11.hpp similarity index 94% rename from Headers/Problem11.hpp rename to Headers/Problems/Problem11.hpp index fba714f..456355d 100644 --- a/Headers/Problem11.hpp +++ b/Headers/Problems/Problem11.hpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Headers/Problem11.hpp +//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem11.hpp //Matthew Ellison // Created: 09-29-18 -//Modified: 07-09-20 +//Modified: 08-28-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 @@ -66,6 +66,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets + virtual std::string getResult(); //Return a string with the solution to the problem std::vector getNumbers() const; //Returns the numbers that were being searched int getProduct() const; //Returns the product that was requested }; diff --git a/Headers/Problem12.hpp b/Headers/Problems/Problem12.hpp similarity index 92% rename from Headers/Problem12.hpp rename to Headers/Problems/Problem12.hpp index 7b0dd57..7ccac44 100644 --- a/Headers/Problem12.hpp +++ b/Headers/Problems/Problem12.hpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Headers/Problem12.hpp +//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem12.hpp //Matthew Ellison // Created: 09-27-18 -//Modified: 07-09-20 +//Modified: 08-28-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/myClasses /* Copyright (C) 2020 Matthew Ellison @@ -46,6 +46,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets + virtual std::string getResult(); //Return a string with the solution to the problem int64_t getTriangularNumber() const; //Returns the triangular number int64_t getLastNumberAdded() const; //Get the final number that was added to the triangular number std::vector getDivisorsOfTriangularNumber() const; //Returns the list of divisors of the requested number diff --git a/Headers/Problem13.hpp b/Headers/Problems/Problem13.hpp similarity index 97% rename from Headers/Problem13.hpp rename to Headers/Problems/Problem13.hpp index 0ee0bc6..8918bca 100644 --- a/Headers/Problem13.hpp +++ b/Headers/Problems/Problem13.hpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Headers/Problem13.hpp +//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem13.hpp //Matthew Ellison // Created: 09-29-18 -//Modified: 07-09-20 +//Modified: 08-28-20 //Work out the first ten digits of the sum of the following one-hundred 50-digit numbers /* 37107287533902102798797998220837590246510135740250 @@ -154,6 +154,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets + virtual std::string getResult(); //Return a string with the solution to the problem std::vector getNumbers() const; //Returns the list 50-digit numbers mpz_class getSum() const; //Returns the sum of the 50-digit numbers }; diff --git a/Headers/Problem14.hpp b/Headers/Problems/Problem14.hpp similarity index 92% rename from Headers/Problem14.hpp rename to Headers/Problems/Problem14.hpp index ecb9de4..7932489 100644 --- a/Headers/Problem14.hpp +++ b/Headers/Problems/Problem14.hpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Headers/Problem14.hpp +//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem14.hpp //Matthew Ellison // Created: 09-29-18 -//Modified: 07-09-20 +//Modified: 08-28-20 /* The following iterative sequence is defined for the set of positive integers: n → n/2 (n is even) @@ -53,6 +53,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets + virtual std::string getResult(); //Return a string with the solution to the problem uint64_t getLength() const; //Returns the length of the requested chain uint64_t getStartingNumber() const; //Returns the starting number of the requested chain }; diff --git a/Headers/Problem15.hpp b/Headers/Problems/Problem15.hpp similarity index 92% rename from Headers/Problem15.hpp rename to Headers/Problems/Problem15.hpp index acd043b..1f63216 100644 --- a/Headers/Problem15.hpp +++ b/Headers/Problems/Problem15.hpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Headers/Problem15.hpp +//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem15.hpp //Matthew Ellison // Created: 09-29-18 -//Modified: 07-09-20 +//Modified: 08-28-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/myClasses /* @@ -50,6 +50,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets + virtual std::string getResult(); //Return a string with the solution to the problem uint64_t getNumberOfRoutes() const; //Returns the number of routes found }; /* Results: diff --git a/Headers/Problem16.hpp b/Headers/Problems/Problem16.hpp similarity index 93% rename from Headers/Problem16.hpp rename to Headers/Problems/Problem16.hpp index bb6e9e5..6edc41c 100644 --- a/Headers/Problem16.hpp +++ b/Headers/Problems/Problem16.hpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Headers/Problem16.hpp +//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem16.hpp //Matthew Ellison // Created: 09-28-18 -//Modified: 07-09-20 +//Modified: 08-28-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/myClasses //This file contains a header from the gmp library. The library is used for large integers. @@ -49,6 +49,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets + virtual std::string getResult(); //Return a string with the solution to the problem mpz_class getNumber() const; //Returns the number that was calculated int getSum() const; //Return the sum of the digits of the number }; diff --git a/Headers/Problem17.hpp b/Headers/Problems/Problem17.hpp similarity index 92% rename from Headers/Problem17.hpp rename to Headers/Problems/Problem17.hpp index 06f033d..dde449b 100644 --- a/Headers/Problem17.hpp +++ b/Headers/Problems/Problem17.hpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Headers/Problem17.hpp +//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem17.hpp //Matthew Ellison // Created: 10-05-18 -//Modified: 07-09-20 +//Modified: 08-28-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/myClasses /* @@ -50,6 +50,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets + virtual std::string getResult(); //Return a string with the solution to the problem uint64_t getLetterCount() const; //Returns the number of letters asked for }; /* Results: diff --git a/Headers/Problem18.hpp b/Headers/Problems/Problem18.hpp similarity index 94% rename from Headers/Problem18.hpp rename to Headers/Problems/Problem18.hpp index f27d303..dfeaf00 100644 --- a/Headers/Problem18.hpp +++ b/Headers/Problems/Problem18.hpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Headers/Problem18.hpp +//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem18.hpp //Matthew Ellison // Created: 11-01-18 -//Modified: 07-09-20 +//Modified: 08-28-20 //Find the maximum total from top to bottom /* 75 @@ -81,6 +81,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets + virtual std::string getResult(); //Return a string with the solution to the problem std::string getPyramid(); //Returns the pyramid that was traversed as a string std::string getTrail(); //Returns the trail the algorithm took as a string int getTotal() const; //Returns the total that was asked for diff --git a/Headers/Problem19.hpp b/Headers/Problems/Problem19.hpp similarity index 93% rename from Headers/Problem19.hpp rename to Headers/Problems/Problem19.hpp index fbf058e..e10609e 100644 --- a/Headers/Problem19.hpp +++ b/Headers/Problems/Problem19.hpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Headers/Problem19.hpp +//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem19.hpp //Matthew Ellison // Created: 09-28-18 -//Modified: 07-09-20 +//Modified: 08-28-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. @@ -62,6 +62,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets + virtual std::string getResult(); //Return a string with the solution to the problem uint64_t getTotalSundays() const; //Returns the total sundays that were asked for }; /* Results diff --git a/Headers/Problem2.hpp b/Headers/Problems/Problem2.hpp similarity index 90% rename from Headers/Problem2.hpp rename to Headers/Problems/Problem2.hpp index 49e32a8..64a1d05 100644 --- a/Headers/Problem2.hpp +++ b/Headers/Problems/Problem2.hpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Headers/Problem2.hpp +//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem2.hpp //Matthew Ellison // Created: 09-28-18 -//Modified: 07-09-20 +//Modified: 08-28-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/myClasses /* @@ -44,6 +44,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets + virtual std::string getResult(); //Return a string with the solution to the problem uint64_t getSum() const; //Returns the requested sum }; diff --git a/Headers/Problem20.hpp b/Headers/Problems/Problem20.hpp similarity index 92% rename from Headers/Problem20.hpp rename to Headers/Problems/Problem20.hpp index f9b03b1..d1175d6 100644 --- a/Headers/Problem20.hpp +++ b/Headers/Problems/Problem20.hpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Headers/Problem20.hpp +//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem20.hpp //Matthew Ellison // Created: 11-07-18 -//Modified: 07-09-20 +//Modified: 08-28-20 //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/myClasses //This file contains a header from the gmp library. The library is used for large integers. @@ -47,6 +47,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets + virtual std::string getResult(); //Return a string with the solution to the problem mpz_class getNumber() const; //Returns the number 100! std::string getNumberString() const; //Returns the number 100! in a string uint64_t getSum() const; //Returns the sum of the digits of 100! diff --git a/Headers/Problem21.hpp b/Headers/Problems/Problem21.hpp similarity index 92% rename from Headers/Problem21.hpp rename to Headers/Problems/Problem21.hpp index de46eb5..d9c0344 100644 --- a/Headers/Problem21.hpp +++ b/Headers/Problems/Problem21.hpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Headers/Problem21.hpp +//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem21.hpp //Matthew Ellison // Created: 11-08-18 -//Modified: 07-09-20 +//Modified: 08-28-20 //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/myClasses /* @@ -49,6 +49,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets + virtual std::string getResult(); //Return a string with the solution to the problem std::vector getAmicable() const; //Returns a vector with all of the amicable numbers calculated uint64_t getSum() const; //Returns the sum of all of the amicable numbers }; diff --git a/Headers/Problem22.hpp b/Headers/Problems/Problem22.hpp similarity index 92% rename from Headers/Problem22.hpp rename to Headers/Problems/Problem22.hpp index 054f547..fc22d73 100644 --- a/Headers/Problem22.hpp +++ b/Headers/Problems/Problem22.hpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Headers/Problem22.hpp +//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem22.hpp //Matthew Ellison // Created: 11-09-18 -//Modified: 07-09-20 +//Modified: 08-28-20 //What is the total of all the name scores in the file? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses /* @@ -49,6 +49,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets + virtual std::string getResult(); //Return a string with the solution to the problem std::vector getNames() const; //Returns the vector of the names being scored uint64_t getNameScoreSum() const; //Returns the sum of the names scores }; diff --git a/Headers/Problem23.hpp b/Headers/Problems/Problem23.hpp similarity index 92% rename from Headers/Problem23.hpp rename to Headers/Problems/Problem23.hpp index 9538412..593956e 100644 --- a/Headers/Problem23.hpp +++ b/Headers/Problems/Problem23.hpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Headers/Problem23.hpp +//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem23.hpp //Matthew Ellison // Created: 11-09-18 -//Modified: 07-09-20 +//Modified: 08-28-20 //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/myClasses /* @@ -50,6 +50,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets + virtual std::string getResult(); //Return a string with the solution to the problem uint64_t getSum() const; //Returns the sum of the numbers asked for }; diff --git a/Headers/Problem24.hpp b/Headers/Problems/Problem24.hpp similarity index 91% rename from Headers/Problem24.hpp rename to Headers/Problems/Problem24.hpp index f95abfc..c54c5de 100644 --- a/Headers/Problem24.hpp +++ b/Headers/Problems/Problem24.hpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Headers/Problem24.hpp +//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem24.hpp //Matthew Ellison // Created: 11-11-18 -//Modified: 07-09-20 +//Modified: 08-28-20 //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/myClasses /* @@ -44,6 +44,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets + virtual std::string getResult(); //Return a string with the solution to the problem std::vector getPermutationsList() const; //Returns a vector with all of the permutations std::string getPermutation() const; //Returns the specific permutations you are looking for }; diff --git a/Headers/Problem25.hpp b/Headers/Problems/Problem25.hpp similarity index 95% rename from Headers/Problem25.hpp rename to Headers/Problems/Problem25.hpp index 9761ffb..5fc8d7a 100644 --- a/Headers/Problem25.hpp +++ b/Headers/Problems/Problem25.hpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Headers/Problem25.hpp +//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem25.hpp //Matthew Ellison // Created: 11-13-18 -//Modified: 07-09-20 +//Modified: 08-28-20 //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/myClasses //This file contains a header from the gmp library. The library is used for large integers. @@ -48,6 +48,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets + virtual std::string getResult(); //Return a string with the solution to the problem mpz_class getNumber() const; //Returns the Fibonacci number asked for std::string getNumberString() const; //Returns the Fibonacci number asked for as a string mpz_class getIndex() const; //Returns the index of the requested Fibonacci number diff --git a/Headers/Problem26.hpp b/Headers/Problems/Problem26.hpp similarity index 91% rename from Headers/Problem26.hpp rename to Headers/Problems/Problem26.hpp index 5925cef..cd675a1 100644 --- a/Headers/Problem26.hpp +++ b/Headers/Problems/Problem26.hpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Headers/Problem26.hpp +//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem26.hpp //Matthew Ellison // Created: 07-28-19 -//Modified: 07-09-20 +//Modified: 08-28-20 //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/myClasses /* @@ -45,6 +45,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets + virtual std::string getResult(); //Return a string with the solution to the problem unsigned int getLongestCycle() const; //Returns the length of the longest cycle unsigned int getLongestNumber() const; //Returns the denominator that starts the longest cycle }; diff --git a/Headers/Problem27.hpp b/Headers/Problems/Problem27.hpp similarity index 92% rename from Headers/Problem27.hpp rename to Headers/Problems/Problem27.hpp index 8382ba0..3a7c057 100644 --- a/Headers/Problem27.hpp +++ b/Headers/Problems/Problem27.hpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Headers/Problem27.hpp +//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem27.hpp //Matthew Ellison // Created: 09-14-19 -//Modified: 07-09-20 +//Modified: 08-28-20 //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/myClasses /* @@ -46,6 +46,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets + virtual std::string getResult(); //Return a string with the solution to the problem int64_t getTopA() const; //Returns the top A that was generated int64_t getTopB() const; //Returns the top B that was generated int64_t getTopN() const; //Returns the top N that was generated diff --git a/Headers/Problem28.hpp b/Headers/Problems/Problem28.hpp similarity index 92% rename from Headers/Problem28.hpp rename to Headers/Problems/Problem28.hpp index 4dd80a6..b0055ea 100644 --- a/Headers/Problem28.hpp +++ b/Headers/Problems/Problem28.hpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Headers/Problem28.hpp +//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem28.hpp //Matthew Ellison // Created: 09-21-19 -//Modified: 07-09-20 +//Modified: 08-28-20 //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/myClasses /* @@ -50,6 +50,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets + virtual std::string getResult(); //Return a string with the solution to the problem std::vector> getGrid() const; //Returns the grid uint64_t getSum() const; //Returns the sum of the diagonals }; diff --git a/Headers/Problem29.hpp b/Headers/Problems/Problem29.hpp similarity index 94% rename from Headers/Problem29.hpp rename to Headers/Problems/Problem29.hpp index 8cbca47..c852e94 100644 --- a/Headers/Problem29.hpp +++ b/Headers/Problems/Problem29.hpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Headers/Problem29.hpp +//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem29.hpp //Matthew Ellison // Created: 10-06-19 -//Modified: 07-09-20 +//Modified: 08-28-20 //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/myClasses //This file contains a header from the gmp library. The library is used for large integers. @@ -53,6 +53,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets + virtual std::string getResult(); //Return a string with the solution to the problem unsigned int getBottomA() const; //Returns the lowest possible value for a unsigned int getTopA() const; //Returns the highest possible value for a unsigned int getBottomB() const; //Returns the lowest possible value for b diff --git a/Headers/Problem3.hpp b/Headers/Problems/Problem3.hpp similarity index 91% rename from Headers/Problem3.hpp rename to Headers/Problems/Problem3.hpp index 1625a5f..b2d9cd8 100644 --- a/Headers/Problem3.hpp +++ b/Headers/Problems/Problem3.hpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Headers/Problem3.hpp +//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem3.hpp //Matthew Ellison // Created: 09-28-18 -//Modified: 07-09-20 +//Modified: 08-28-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/myClasses /* @@ -45,6 +45,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets + virtual std::string getResult(); //Return a string with the solution to the problem std::vector getFactors() const; //Returns the list of factors of the number uint64_t getLargestFactor() const; //Returns the largest factor of the number uint64_t getGoalNumber() const; //Returns the number diff --git a/Headers/Problem30.hpp b/Headers/Problems/Problem30.hpp similarity index 93% rename from Headers/Problem30.hpp rename to Headers/Problems/Problem30.hpp index 1af3161..99cb76e 100644 --- a/Headers/Problem30.hpp +++ b/Headers/Problems/Problem30.hpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Headers/Problem30.hpp +//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem30.hpp //Matthew Ellison // Created: 10-27-19 -//Modified: 07-09-20 +//Modified: 08-28-20 //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/myClasses /* @@ -50,6 +50,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets + virtual std::string getResult(); //Return a string with the solution to the problem uint64_t getTopNum() const; //This returns the top number to be checked std::vector getListOfSumOfFifths() const; //This returns a copy of the vector holding all the numbers that are the sum of the fifth power of their digits uint64_t getSumOfList() const; //This returns the sum of all entries in sumOfFifthNumbers diff --git a/Headers/Problem31.hpp b/Headers/Problems/Problem31.hpp similarity index 91% rename from Headers/Problem31.hpp rename to Headers/Problems/Problem31.hpp index a4901c8..a777961 100644 --- a/Headers/Problem31.hpp +++ b/Headers/Problems/Problem31.hpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Headers/Problem31.hpp +//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem31.hpp //Matthew Ellison // Created: 06-19-20 -//Modified: 07-09-20 +//Modified: 08-28-20 //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/myClasses /* @@ -44,6 +44,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets + virtual std::string getResult(); //Return a string with the solution to the problem int getPermutations() const; //Returns the number of correct permutations of the coins }; diff --git a/Headers/Problem32.hpp b/Headers/Problems/Problem32.hpp similarity index 88% rename from Headers/Problem32.hpp rename to Headers/Problems/Problem32.hpp index 1cc2ed8..1940586 100644 --- a/Headers/Problem32.hpp +++ b/Headers/Problems/Problem32.hpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Headers/Problem32.hpp +//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem32.hpp //Matthew Ellison // Created: 07-27-20 -//Modified: 07-27-20 +//Modified: 08-28-20 //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/myClasses /* @@ -66,13 +66,11 @@ public: //Constructor Problem32(); //Operational functions - //Solve the problem - void solve(); - //Reset the problem so it can be run again - void reset(); + void solve(); //Solve the problem + void reset(); //Reset the problem so it can be run again //Gets - //Returns the sum of the pandigitals - int64_t getSumOfPandigitals(); + virtual std::string getResult(); //Return a string with the solution to the problem + int64_t getSumOfPandigitals(); //Returns the sum of the pandigitals }; /* Results: diff --git a/Headers/Problem4.hpp b/Headers/Problems/Problem4.hpp similarity index 91% rename from Headers/Problem4.hpp rename to Headers/Problems/Problem4.hpp index 62a0e59..96047e3 100644 --- a/Headers/Problem4.hpp +++ b/Headers/Problems/Problem4.hpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Headers/Problem4.hpp +//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem4.hpp //Matthew Ellison // Created: 09-28-18 -//Modified: 07-09-20 +//Modified: 08-28-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/myClasses /* @@ -45,6 +45,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets + virtual std::string getResult(); //Return a string with the solution to the problem std::vector getPalindromes() const; //Returns the list of all palindromes uint64_t getLargestPalindrome() const; //Returns the largest palindrome }; diff --git a/Headers/Problem5.hpp b/Headers/Problems/Problem5.hpp similarity index 90% rename from Headers/Problem5.hpp rename to Headers/Problems/Problem5.hpp index 32a8a0f..107199e 100644 --- a/Headers/Problem5.hpp +++ b/Headers/Problems/Problem5.hpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Headers/Problem5.hpp +//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem5.hpp //Matthew Ellison // Created: 09-28-18 -//Modified: 07-09-20 +//Modified: 08-28-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/myClasses /* @@ -43,6 +43,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets + virtual std::string getResult(); //Return a string with the solution to the problem int getNumber() const; //Returns the requested number }; diff --git a/Headers/Problem6.hpp b/Headers/Problems/Problem6.hpp similarity index 92% rename from Headers/Problem6.hpp rename to Headers/Problems/Problem6.hpp index 6c11f3e..c0d43bf 100644 --- a/Headers/Problem6.hpp +++ b/Headers/Problems/Problem6.hpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Headers/Problem6.hpp +//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem6.hpp //Matthew Ellison // Created: 09-28-18 -//Modified: 07-09-20 +//Modified: 08-28-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/myClasses /* @@ -46,6 +46,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets + virtual std::string getResult(); //Return a string with the solution to the problem uint64_t getSumOfSquares() const; //Returns the sum of all the squares uint64_t getSquareOfSum() const; //Returns the square of all of the sums uint64_t getDifference() const; //Returns the requested difference diff --git a/Headers/Problem67.hpp b/Headers/Problems/Problem67.hpp similarity index 99% rename from Headers/Problem67.hpp rename to Headers/Problems/Problem67.hpp index fd17033..ff96574 100644 --- a/Headers/Problem67.hpp +++ b/Headers/Problems/Problem67.hpp @@ -1,4 +1,4 @@ -//ProjectEuler/ProjectEulerCPP/Headers/Problem67.hpp +//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem67.hpp //Matthew Ellison // Created: 11-02-18 //Modified: 07-09-20 diff --git a/Headers/Problem7.hpp b/Headers/Problems/Problem7.hpp similarity index 90% rename from Headers/Problem7.hpp rename to Headers/Problems/Problem7.hpp index 10f417b..3bc9c25 100644 --- a/Headers/Problem7.hpp +++ b/Headers/Problems/Problem7.hpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Headers/Problem7.hpp +//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem7.hpp //Matthew Ellison // Created: 09-28-18 -//Modified: 07-09-20 +//Modified: 08-28-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/myClasses /* @@ -45,6 +45,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets + virtual std::string getResult(); //Return a string with the solution to the problem uint64_t getPrime() const; //Returns the requested prime number }; diff --git a/Headers/Problem8.hpp b/Headers/Problems/Problem8.hpp similarity index 94% rename from Headers/Problem8.hpp rename to Headers/Problems/Problem8.hpp index d2b3063..f354092 100644 --- a/Headers/Problem8.hpp +++ b/Headers/Problems/Problem8.hpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Headers/Problem8.hpp +//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem8.hpp //Matthew Ellison // Created: 09-28-18 -//Modified: 07-09-20 +//Modified: 08-28-20 //Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product? /* 73167176531330624919225119674426574742355349194934 @@ -68,6 +68,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets + virtual std::string getResult(); //Return a string with the solution to the problem std::string getLargestNums() const; //Returns the string of numbers that produces the largest product uint64_t getLargestProduct() const; //Returns the requested product }; diff --git a/Headers/Problem9.hpp b/Headers/Problems/Problem9.hpp similarity index 92% rename from Headers/Problem9.hpp rename to Headers/Problems/Problem9.hpp index 91fd5a5..ec3132d 100644 --- a/Headers/Problem9.hpp +++ b/Headers/Problems/Problem9.hpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Headers/Problem9.hpp +//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem9.hpp //Matthew Ellison // Created: 09-28-18 -//Modified: 07-09-20 +//Modified: 08-28-20 //There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product of abc. //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses /* @@ -45,6 +45,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets + virtual std::string getResult(); //Return a string with the solution to the problem int getSideA() const; //Returns the length of the first side int getSideB() const; //Returns the length of the second side int getSideC() const; //Returns the length of the hyp diff --git a/benchmark.hpp b/Headers/benchmark.hpp similarity index 98% rename from benchmark.hpp rename to Headers/benchmark.hpp index 8f0ae36..d9bd5ff 100644 --- a/benchmark.hpp +++ b/Headers/benchmark.hpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/benchmark.hpp +//ProjectEuler/ProjectEulerCPP/headers/benchmark.hpp //Matthew Ellison // Created: 07-08-20 -//Modified: 07-09-20 +//Modified: 08-28-20 //These are functions that help determine an average run time for the problems /* Copyright (C) 2020 Matthew Ellison @@ -199,7 +199,7 @@ std::string getBenchmarkResults(Problem* problem, double totalTime, unsigned int std::string timeResults = mee::Stopwatch::getStr(totalTime); //Tally the results std::stringstream results; - results << "\n\n" << problem->getResults(); + results << "\n\n" << problem->getResult(); results << "\nIt took an average of " << timeResults << " to run this problem over " << timesRun << " iterations\n\n" << std::endl; return results.str(); } diff --git a/makefile b/makefile index 2db71ba..34e6e04 100644 --- a/makefile +++ b/makefile @@ -4,7 +4,10 @@ LIBFLAGS = -shared -std=c++17 -O3 -fPIC -Wall EXEFLAGS = -Wall -std=c++11 -O3 -Wl,-rpath,'$$ORIGIN/lib' LINKEDLIBS = -lgmp -lgmpxx PROBLEM_NUMBERS = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 67 -PROBLEM_FILES = $(patsubst %,Source/libProblem%.cpp,$(PROBLEM_NUMBERS)) +SOURCE_DIR = src +PROBLEM_DIR = $(SOURCE_DIR)/Problems +INCLUDE_DIR = headers +PROBLEM_FILES = $(patsubst %,$(PROBLEM_DIR)/libProblem%.cpp,$(PROBLEM_NUMBERS)) LIBDIR = ./lib LIBS = $(patsubst %, -lProblem%,$(PROBLEM_NUMBERS)) @@ -24,24 +27,24 @@ moveBin: mv ProjectEuler.exe lib/ProjectEuler.exe #Building the Libraries -$(LIBDIR)/libProblem%.so: Source/Problem%.cpp - $(CXX) $(LIBFLAGS) -o $@ $< $(LINKEDLIBS) -$(LIBDIR)/libProblem67.so: Source/Problem67.cpp - $(CXX) $(LIBFLAGS) -o $@ $< $(LINKEDLIBS) -L $(LIBDIR) -lProblem18 +$(LIBDIR)/libProblem%.so: $(PROBLEM_DIR)/Problem%.cpp + $(CXX) $(LIBFLAGS) -o $@ $< -I $(INCLUDE_DIR) $(LINKEDLIBS) +$(LIBDIR)/libProblem67.so: $(PROBLEM_DIR)/Problem67.cpp + $(CXX) $(LIBFLAGS) -o $@ $< -I $(INCLUDE_DIR) $(LINKEDLIBS) -L $(LIBDIR) -lProblem18 libsMulti: $(MAKE) libs -j $(NUMCORES) #Building the Libraries for Windows -$(LIBDIR)/libProblem%.dll: Source/Problem%.cpp - $(CXX) $(LIBFLAGS) -o $@ $< $(LINKEDLIBS) -$(LIBDIR)/libProblem67.dll: Source/Problem67.cpp - $(CXX) $(LIBFLAGS) -o $@ $< $(LINKEDLIBS) -L $(LIBDIR) -lProblem18 +$(LIBDIR)/libProblem%.dll: $(PROBLEM_DIR)/Problem%.cpp + $(CXX) $(LIBFLAGS) -o $@ $< -I $(INCLUDE_DIR) $(LINKEDLIBS) +$(LIBDIR)/libProblem67.dll: $(PROBLEM_DIR)/Problem67.cpp + $(CXX) $(LIBFLAGS) -o $@ $< -I $(INCLUDE_DIR) $(LINKEDLIBS) -L $(LIBDIR) -lProblem18 libsWindowsMulti: $(MAKE) libsWindows -j $(NUMCORESWIN) #Building the executable -ProjectEuler: main.cpp - $(CXX) $(EXEFLAGS) -o $@.exe $< -L $(LIBDIR) $(LIBS) $(LINKEDLIBS) +ProjectEuler: $(SOURCE_DIR)/main.cpp + $(CXX) $(EXEFLAGS) -o $@.exe $< -I $(INCLUDE_DIR) -L $(LIBDIR) $(LIBS) $(LINKEDLIBS) #Clean up/Remove all files and folders created diff --git a/Source/Problem1.cpp b/src/Problems/Problem1.cpp similarity index 90% rename from Source/Problem1.cpp rename to src/Problems/Problem1.cpp index 1da1afc..c76fd3f 100644 --- a/Source/Problem1.cpp +++ b/src/Problems/Problem1.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/Source/Problem1.cpp //Matthew Ellison // Created: 07-10-19 -//Modified: 07-09-20 +//Modified: 08-28-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/myClasses /* @@ -27,8 +27,7 @@ #include #include #include "Stopwatch.hpp" -#include "../Headers/Problem.hpp" -#include "../Headers/Problem1.hpp" +#include "Problems/Problem1.hpp" //The highest number to be tested @@ -62,13 +61,9 @@ void Problem1::solve(){ //Stop the timer timer.stop(); - //Save the results - result << "The sum of all the numbers < " << MAX_NUMBER + 1 << " that are divisible by 3 or 5 is " << fullSum; - //Throw a flag to show the problem is solved solved = true; } - //Reset the problem so it can be run again void Problem1::reset(){ Problem::reset(); @@ -76,6 +71,15 @@ void Problem1::reset(){ } //Gets +//Return a string with the solution to the problem +std::string Problem1::getResult(){ + if(!solved){ + throw Unsolved(); + } + std::stringstream result; + result << "The sum of all the numbers < " << MAX_NUMBER + 1 << " that are divisible by 3 or 5 is " << fullSum; + return result.str(); +} //Returns the requested sum uint64_t Problem1::getSum() const{ //If the prblem hasn't been solved throw an exception diff --git a/Source/Problem10.cpp b/src/Problems/Problem10.cpp similarity index 89% rename from Source/Problem10.cpp rename to src/Problems/Problem10.cpp index be8157d..762dd6a 100644 --- a/Source/Problem10.cpp +++ b/src/Problems/Problem10.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/Source/Problem10.cpp //Matthew Ellison // Created: 09-28-18 -//Modified: 07-09-20 +//Modified: 08-28-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/myClasses /* @@ -27,8 +27,7 @@ #include #include "Stopwatch.hpp" #include "Algorithms.hpp" -#include "../Headers/Problem.hpp" -#include "../Headers/Problem10.hpp" +#include "Problems/Problem10.hpp" //The largest number to check for primes @@ -53,19 +52,25 @@ void Problem10::solve(){ //Stop the timer timer.stop(); - //Save the results - result << "The sum of all the primes less than " << GOAL_NUMBER + 1 << " is " << sum; - //Throw a flag to show the problem is solved solved = true; } - //Reset the problem so it can be run again void Problem10::reset(){ Problem::reset(); sum = 0; } +//Gets +//Return a string with the solution to the problem +std::string Problem10::getResult(){ + if(!solved){ + throw Unsolved(); + } + std::stringstream result; + result << "The sum of all the primes less than " << GOAL_NUMBER + 1 << " is " << sum; + return result.str(); +} //Returns the sum that was requested uint64_t Problem10::getSum() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem11.cpp b/src/Problems/Problem11.cpp similarity index 97% rename from Source/Problem11.cpp rename to src/Problems/Problem11.cpp index 46d2b29..a36b546 100644 --- a/Source/Problem11.cpp +++ b/src/Problems/Problem11.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/Source/Problem11.cpp //Matthew Ellison // Created: 09-29-18 -//Modified: 07-09-20 +//Modified: 08-28-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 @@ -49,8 +49,7 @@ #include #include "Stopwatch.hpp" #include "Algorithms.hpp" -#include "../Headers/Problem.hpp" -#include "../Headers/Problem11.hpp" +#include "Problems/Problem11.hpp" //This is the grid of number that we will be working with @@ -174,20 +173,26 @@ void Problem11::solve(){ //Stop the timer timer.stop(); - //Save the results - result << "The greatest product of 4 number in a line is " << mee::getProduct(greatestProduct) - << "\nThe numbers are " << greatestProduct.at(0) << ' ' << greatestProduct.at(1) << ' ' << greatestProduct.at(2) << ' ' << greatestProduct.at(3); - //Throw a flag to show the problem is solved solved = true; } - //Reset the problem so it can be run again void Problem11::reset(){ Problem::reset(); greatestProduct.clear(); } +//Gets +//Return a string with the solution to the problem +std::string Problem11::getResult(){ + if(!solved){ + throw Unsolved(); + } + std::stringstream result; + result << "The greatest product of 4 number in a line is " << mee::getProduct(greatestProduct) + << "\nThe numbers are " << greatestProduct.at(0) << ' ' << greatestProduct.at(1) << ' ' << greatestProduct.at(2) << ' ' << greatestProduct.at(3); + return result.str(); +} //Returns the numbers that were being searched std::vector Problem11::getNumbers() const{ //If the problem hasn't been solved throw an exception @@ -196,7 +201,6 @@ std::vector Problem11::getNumbers() const{ } return greatestProduct; } - //Returns the product that was requested int Problem11::getProduct() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem12.cpp b/src/Problems/Problem12.cpp similarity index 93% rename from Source/Problem12.cpp rename to src/Problems/Problem12.cpp index ee0c4b3..a69d4fc 100644 --- a/Source/Problem12.cpp +++ b/src/Problems/Problem12.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/Source/Problem12.cpp //Matthew Ellison // Created: 09-27-18 -//Modified: 07-09-20 +//Modified: 08-28-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/myClasses /* Copyright (C) 2020 Matthew Ellison @@ -26,8 +26,7 @@ #include #include "Stopwatch.hpp" #include "Algorithms.hpp" -#include "../Headers/Problem.hpp" -#include "../Headers/Problem12.hpp" +#include "Problems/Problem12.hpp" //The number of divisors that you want @@ -66,13 +65,9 @@ void Problem12::solve(){ //Stop the timer timer.stop(); - //Save the results - result << "The triangular number " << sum << " is a sum of all numbers >= " << counter - 1 << " and has " << divisors.size() << " divisors"; - //Throw a flag to show the problem is solved solved = true; } - //Reset the problem so it can be run again void Problem12::reset(){ Problem::reset(); @@ -81,6 +76,16 @@ void Problem12::reset(){ counter = 2; } +//Gets +//Return a string with the solution to the problem +std::string Problem12::getResult(){ + if(!solved){ + throw Unsolved(); + } + std::stringstream result; + result << "The triangular number " << sum << " is a sum of all numbers >= " << counter - 1 << " and has " << divisors.size() << " divisors"; + return result.str(); +} //Returns the triangular number int64_t Problem12::getTriangularNumber() const{ //If the problem hasn't been solved throw an exception @@ -89,7 +94,6 @@ int64_t Problem12::getTriangularNumber() const{ } return sum; } - //Get the final number that was added to the triangular number int64_t Problem12::getLastNumberAdded() const{ //If the problem hasn't been solved throw an exception @@ -98,7 +102,6 @@ int64_t Problem12::getLastNumberAdded() const{ } return counter - 1; } - //Returns the list of divisors of the requested number std::vector Problem12::getDivisorsOfTriangularNumber() const{ //If the problem hasn't been solved throw an exception @@ -107,7 +110,6 @@ std::vector Problem12::getDivisorsOfTriangularNumber() const{ } return divisors; } - //Returns the number of divisors of the requested number size_t Problem12::getNumberOfDivisors() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem13.cpp b/src/Problems/Problem13.cpp similarity index 98% rename from Source/Problem13.cpp rename to src/Problems/Problem13.cpp index 04c4570..68bc6c2 100644 --- a/Source/Problem13.cpp +++ b/src/Problems/Problem13.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/Source/Problem13.cpp //Matthew Ellison // Created: 09-29-18 -//Modified: 07-09-20 +//Modified: 08-28-20 //Work out the first ten digits of the sum of the following one-hundred 50-digit numbers /* 37107287533902102798797998220837590246510135740250 @@ -134,8 +134,7 @@ #include "gmpxx.h" //This is part of the gmp library #include "Stopwatch.hpp" #include "Algorithms.hpp" -#include "../Headers/Problem.hpp" -#include "../Headers/Problem13.hpp" +#include "Problems/Problem13.hpp" //A function to set the nums vector @@ -273,14 +272,9 @@ void Problem13::solve(){ //Stop the timer timer.stop(); - //Save the results - result << "The sum of all " << nums.size() << " numbers is " << sum - << "\nThe first 10 digits of the sum of the numbers is " << sum.get_str().substr(0, 10); - //Throw a flag to show the problem is solved solved = true; } - //Reset the problem so it can be run again void Problem13::reset(){ Problem::reset(); @@ -289,6 +283,17 @@ void Problem13::reset(){ reserveVectors(); } +//Gets +//Return a string with the solution to the problem +std::string Problem13::getResult(){ + if(!solved){ + throw Unsolved(); + } + std::stringstream result; + result << "The sum of all " << nums.size() << " numbers is " << sum + << "\nThe first 10 digits of the sum of the numbers is " << sum.get_str().substr(0, 10); + return result.str(); +} //Returns the list 50-digit numbers std::vector Problem13::getNumbers() const{ //If the problem hasn't been solved throw an exception @@ -297,7 +302,6 @@ std::vector Problem13::getNumbers() const{ } return nums; } - //Returns the sum of the 50-digit numbers mpz_class Problem13::getSum() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem14.cpp b/src/Problems/Problem14.cpp similarity index 93% rename from Source/Problem14.cpp rename to src/Problems/Problem14.cpp index d6754a6..a70ea9a 100644 --- a/Source/Problem14.cpp +++ b/src/Problems/Problem14.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/Source/Problem14.cpp //Matthew Ellison // Created: 09-29-18 -//Modified: 07-09-20 +//Modified: 08-28-20 /* The following iterative sequence is defined for the set of positive integers: n → n/2 (n is even) @@ -31,8 +31,7 @@ Which starting number, under one million, produces the longest chain? #include #include #include "Stopwatch.hpp" -#include "../Headers/Problem.hpp" -#include "../Headers/Problem14.hpp" +#include "Problems/Problem14.hpp" //This is the top number that you will be checking against the series @@ -82,13 +81,9 @@ void Problem14::solve(){ //Stop the timer timer.stop(); - //Save the results - result << "The number " << maxNum << " produced a chain of " << maxLength << " steps"; - //Throw a flag to show the problem is solved solved = true; } - //Reset the problem so it can be run again void Problem14::reset(){ Problem::reset(); @@ -96,6 +91,16 @@ void Problem14::reset(){ maxNum = 0; } +//Gets +//Return a string with the solution to the problem +std::string Problem14::getResult(){ + if(!solved){ + throw Unsolved(); + } + std::stringstream result; + result << "The number " << maxNum << " produced a chain of " << maxLength << " steps"; + return result.str(); +} //Returns the length of the requested chain uint64_t Problem14::getLength() const{ //If the problem hasn't been solved throw an exception @@ -104,7 +109,6 @@ uint64_t Problem14::getLength() const{ } return maxLength; } - //Returns the starting number of the requested chain uint64_t Problem14::getStartingNumber() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem15.cpp b/src/Problems/Problem15.cpp similarity index 92% rename from Source/Problem15.cpp rename to src/Problems/Problem15.cpp index 7eaf852..ef1b369 100644 --- a/Source/Problem15.cpp +++ b/src/Problems/Problem15.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/Source/Problem15.hpp //Matthew Ellison // Created: 09-29-18 -//Modified: 07-09-20 +//Modified: 08-28-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/myClasses /* @@ -26,8 +26,7 @@ #include #include #include "Stopwatch.hpp" -#include "../Headers/Problem.hpp" -#include "../Headers/Problem15.hpp" +#include "Problems/Problem15.hpp" int Problem15::WIDTH = 20; //The width of the grid @@ -78,19 +77,25 @@ void Problem15::solve(){ //Stop the timer timer.stop(); - //Save the results - result << "The number of routes is " << numOfRoutes; - //Throw a flag to show the problem is solved solved = true; } - //Reset the problem so it can be run again void Problem15::reset(){ Problem::reset(); numOfRoutes = 0; } +//Gets +//Return a string with the solution to the problem +std::string Problem15::getResult(){ + if(!solved){ + throw Unsolved(); + } + std::stringstream result; + result << "The number of routes is " << numOfRoutes; + return result.str(); +} //Returns the number of routes found uint64_t Problem15::getNumberOfRoutes() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem16.cpp b/src/Problems/Problem16.cpp similarity index 92% rename from Source/Problem16.cpp rename to src/Problems/Problem16.cpp index 41d84b2..be01978 100644 --- a/Source/Problem16.cpp +++ b/src/Problems/Problem16.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/Source/Problem16.cpp //Matthew Ellison // Created: 09-28-18 -//Modified: 07-09-20 +//Modified: 08-28-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/myClasses //This file contains a header from the gmp library. The library is used for large integers. @@ -28,8 +28,7 @@ #include #include #include "Stopwatch.hpp" -#include "../Headers/Problem.hpp" -#include "../Headers/Problem16.hpp" +#include "Problems/Problem16.hpp" int Problem16::NUM_TO_POWER = 2; //The number that is going to be raised to a power @@ -65,14 +64,9 @@ void Problem16::solve(){ //Stop the timer timer.stop(); - //Save the results - result << NUM_TO_POWER << '^' << POWER << " = " << num - << "\nThe sum of the elements is " << sumOfElements; - //Throw a flag to show the problem is solved solved = true; } - //Reset the problem so it can be run again void Problem16::reset(){ Problem::reset(); @@ -80,6 +74,17 @@ void Problem16::reset(){ sumOfElements = 0; } +//Gets +//Return a string with the solution to the problem +std::string Problem16::getResult(){ + if(!solved){ + throw Unsolved(); + } + std::stringstream result; + result << NUM_TO_POWER << '^' << POWER << " = " << num + << "\nThe sum of the elements is " << sumOfElements; + return result.str(); +} //Returns the number that was calculated mpz_class Problem16::getNumber() const{ //If the problem hasn't been solved throw an exception @@ -88,7 +93,6 @@ mpz_class Problem16::getNumber() const{ } return num; } - //Return the sum of the digits of the number int Problem16::getSum() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem17.cpp b/src/Problems/Problem17.cpp similarity index 95% rename from Source/Problem17.cpp rename to src/Problems/Problem17.cpp index cb42864..e3ad750 100644 --- a/Source/Problem17.cpp +++ b/src/Problems/Problem17.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/Source/Problem17.cpp //Matthew Ellison // Created: 10-05-18 -//Modified: 07-09-20 +//Modified: 08-28-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/myClasses /* @@ -26,8 +26,7 @@ #include #include #include "Stopwatch.hpp" -#include "../Headers/Problem.hpp" -#include "../Headers/Problem17.hpp" +#include "Problems/Problem17.hpp" //This is the largest number to get the words of @@ -155,11 +154,11 @@ std::string Problem17::wordHelper(int num){ case 3: tempString += "three"; break; case 2: tempString += "two"; break; case 1: tempString += "one"; break; + //TODO: Throw an exception default: tempString += "ERROR"; break; } return tempString; } - //This counts the number of letters in the string that is passed in (ignoring numbers and punctuation) uint64_t Problem17::countLetters(std::string str){ uint64_t letterCount = 0; @@ -195,19 +194,25 @@ void Problem17::solve(){ //Stop the timer timer.stop(); - //Save the results - result << "The number of letters is " << letterCount; - //Throw a flag to show the problem is solved solved = true; } - //Reset the problem so it can be run again void Problem17::reset(){ Problem::reset(); letterCount = 0; } +//Gets +//Return a string with the solution to the problem +std::string Problem17::getResult(){ + if(!solved){ + throw Unsolved(); + } + std::stringstream result; + result << "The number of letters is " << letterCount; + return result.str(); +} //Returns the number of letters asked for uint64_t Problem17::getLetterCount() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem18.cpp b/src/Problems/Problem18.cpp similarity index 96% rename from Source/Problem18.cpp rename to src/Problems/Problem18.cpp index ae289a8..c67564d 100644 --- a/Source/Problem18.cpp +++ b/src/Problems/Problem18.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/Source/Problem18.hpp //Matthew Ellison // Created: 11-01-18 -//Modified: 07-09-20 +//Modified: 08-28-20 //Find the maximum total from top to bottom /* 75 @@ -45,7 +45,7 @@ #include #include #include "Stopwatch.hpp" -#include "../Headers/Problem18.hpp" +#include "Problems/Problem18.hpp" //Setup the list you are trying to find a path through @@ -139,13 +139,9 @@ void Problem18::solve(){ //Stop the timer timer.stop(); - //Save the results - result << "The value of the longest path is " << actualTotal; - //Throw a flag to show the problem is solved solved = true; } - //Reset the problem so it can be run again void Problem18::reset(){ Problem::reset(); @@ -154,6 +150,16 @@ void Problem18::reset(){ actualTotal = 0; } +//Gets +//Return a string with the solution to the problem +std::string Problem18::getResult(){ + if(!solved){ + throw Unsolved(); + } + std::stringstream result; + result << "The value of the longest path is " << actualTotal; + return result.str(); +} //Returns the pyramid that was traversed as a string std::string Problem18::getPyramid(){ //If the problem hasn't been solved throw an exception @@ -170,7 +176,6 @@ std::string Problem18::getPyramid(){ } return results.str(); } - //Returns the trail the algorithm took as a string std::string Problem18::getTrail(){ //If the problem hasn't been solved throw an exception @@ -230,7 +235,6 @@ std::string Problem18::getTrail(){ } return results.str(); } - //Returns the total that was asked for int Problem18::getTotal() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem19.cpp b/src/Problems/Problem19.cpp similarity index 95% rename from Source/Problem19.cpp rename to src/Problems/Problem19.cpp index 8f26ccb..ef66e5b 100644 --- a/Source/Problem19.cpp +++ b/src/Problems/Problem19.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/Source/Problem19.cpp //Matthew Ellison // Created: 09-28-18 -//Modified: 07-09-20 +//Modified: 08-28-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. @@ -37,7 +37,7 @@ A leap year occurs on any year evenly divisible by 4, but not on a century unles #include #include #include "Stopwatch.hpp" -#include "../Headers/Problem19.hpp" +#include "Problems/Problem19.hpp" unsigned int Problem19::START_YEAR = 1901; //The start year @@ -119,7 +119,6 @@ Problem19::DAYS Problem19::getDay(unsigned int month, unsigned int day, unsigned default: return ERROR; } } - //Returns true if the year passed to it is a leap year bool Problem19::isLeapYear(unsigned int year){ if(year < 1){ @@ -171,19 +170,25 @@ void Problem19::solve(){ //Stop the timer timer.stop(); - //Save the results - result << "There are " << totalSundays << " Sundays that landed on the first of the months from " << START_YEAR << " to " << END_YEAR; - //Throw a flag to show the problem is solved solved = true; } - //Reset the problem so it can be run again void Problem19::reset(){ Problem::reset(); totalSundays = 0; } +//Gets +//Return a string with the solution to the problem +std::string Problem19::getResult(){ + if(!solved){ + throw Unsolved(); + } + std::stringstream result; + result << "There are " << totalSundays << " Sundays that landed on the first of the months from " << START_YEAR << " to " << END_YEAR; + return result.str(); +} //Returns the total sundays that were asked for uint64_t Problem19::getTotalSundays() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem2.cpp b/src/Problems/Problem2.cpp similarity index 90% rename from Source/Problem2.cpp rename to src/Problems/Problem2.cpp index 5834b27..8e5c3e7 100644 --- a/Source/Problem2.cpp +++ b/src/Problems/Problem2.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/Source/Problem2.cpp //Matthew Ellison // Created: 09-28-18 -//Modified: 07-09-20 +//Modified: 08-28-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/myClasses /* @@ -27,8 +27,7 @@ #include #include "Stopwatch.hpp" #include "Algorithms.hpp" -#include "../Headers/Problem.hpp" -#include "../Headers/Problem2.hpp" +#include "Problems/Problem2.hpp" //Holds the largest number that we are looking for @@ -61,19 +60,25 @@ void Problem2::solve(){ //Stop the timer timer.stop(); - //Save the results - result << "The sum of the even Fibonacci numbers less than 4,000,000 is " << fullSum; - //Throw a flag to show the problem is solved solved = true; } - //Reset the problem so it can be run again void Problem2::reset(){ Problem::reset(); fullSum = 0; } +//Gets +//Return a string with the solution to the problem +std::string Problem2::getResult(){ + if(!solved){ + throw Unsolved(); + } + std::stringstream result; + result << "The sum of the even Fibonacci numbers less than 4,000,000 is " << fullSum; + return result.str(); +} //Returns the requested sum uint64_t Problem2::getSum() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem20.cpp b/src/Problems/Problem20.cpp similarity index 92% rename from Source/Problem20.cpp rename to src/Problems/Problem20.cpp index cbec545..42cbc30 100644 --- a/Source/Problem20.cpp +++ b/src/Problems/Problem20.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/Source/Problem20.cpp //Matthew Ellison // Created: 11-07-18 -//Modified: 07-09-20 +//Modified: 08-28-20 //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/myClasses //This file contains a header from the gmp library. The library is used for large integers. @@ -30,7 +30,7 @@ #include #include "gmpxx.h" #include "Stopwatch.hpp" -#include "../Headers/Problem20.hpp" +#include "Problems/Problem20.hpp" //Constructor @@ -64,14 +64,9 @@ void Problem20::solve(){ //Stop the timer timer.stop(); - //Save the results - result << "100! = " << num.get_str() - << "\nThe sum of the digits is: " << sum; - //Throw a flag to show the problem is solved solved = true; } - //Reset the problem so it can be run again void Problem20::reset(){ Problem::reset(); @@ -79,6 +74,17 @@ void Problem20::reset(){ num = 1; } +//Gets +//Return a string with the solution to the problem +std::string Problem20::getResult(){ + if(!solved){ + throw Unsolved(); + } + std::stringstream result; + result << "100! = " << num.get_str() + << "\nThe sum of the digits is: " << sum; + return result.str(); +} //Returns the number 100! mpz_class Problem20::getNumber() const{ //If the problem hasn't been solved throw an exception @@ -87,7 +93,6 @@ mpz_class Problem20::getNumber() const{ } return num; } - //Returns the number 100! in a string std::string Problem20::getNumberString() const{ //If the problem hasn't been solved throw an exception @@ -96,7 +101,6 @@ std::string Problem20::getNumberString() const{ } return num.get_str(); } - //Returns the sum of the digits of 100! uint64_t Problem20::getSum() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem21.cpp b/src/Problems/Problem21.cpp similarity index 94% rename from Source/Problem21.cpp rename to src/Problems/Problem21.cpp index 5f6681f..e3f8360 100644 --- a/Source/Problem21.cpp +++ b/src/Problems/Problem21.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/Source/Problem21.cpp //Matthew Ellison // Created: 11-08-18 -//Modified: 07-09-20 +//Modified: 08-28-20 //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/myClasses /* @@ -28,7 +28,7 @@ #include #include "Algorithms.hpp" #include "Stopwatch.hpp" -#include "../Headers/Problem21.hpp" +#include "Problems/Problem21.hpp" //The top number that will be evaluated @@ -87,17 +87,9 @@ void Problem21::solve(){ //Stop the timer timer.stop(); - //Save the results - result << "All amicable numbers less than 10000 are\n"; - for(unsigned int cnt = 0;cnt < amicable.size();++cnt){ - result << amicable.at(cnt) << '\n'; - } - result << "The sum of all of these amicable numbers is " << mee::getSum(amicable); - //Throw a flag to show the problem is solved solved = true; } - //Reset the problem so it can be run again void Problem21::reset(){ Problem::reset(); @@ -106,6 +98,20 @@ void Problem21::reset(){ reserveVectors(); } +//Gets +//Return a string with the solution to the problem +std::string Problem21::getResult(){ + if(!solved){ + throw Unsolved(); + } + std::stringstream result; + result << "All amicable numbers less than 10000 are\n"; + for(unsigned int cnt = 0;cnt < amicable.size();++cnt){ + result << amicable.at(cnt) << '\n'; + } + result << "The sum of all of these amicable numbers is " << mee::getSum(amicable); + return result.str(); +} //Returns a vector with all of the amicable numbers calculated std::vector Problem21::getAmicable() const{ //If the problem hasn't been solved throw an exception @@ -114,7 +120,6 @@ std::vector Problem21::getAmicable() const{ } return amicable; } - //Returns the sum of all of the amicable numbers uint64_t Problem21::getSum() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem22.cpp b/src/Problems/Problem22.cpp similarity index 99% rename from Source/Problem22.cpp rename to src/Problems/Problem22.cpp index 4a9e025..c3afc29 100644 --- a/Source/Problem22.cpp +++ b/src/Problems/Problem22.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/Source/Problem22.cpp //Matthew Ellison // Created: 11-09-18 -//Modified: 07-09-20 +//Modified: 08-28-20 //What is the total of all the name scores in the file? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses /* @@ -29,7 +29,7 @@ #include #include "Stopwatch.hpp" #include "Algorithms.hpp" -#include "../Headers/Problem22.hpp" +#include "Problems/Problem22.hpp" //Holds the names that will be scored @@ -445,13 +445,9 @@ void Problem22::solve(){ //Stop the timer timer.stop(); - //Save the results - result << "The answer to the question is " << mee::getSum(prod); - //Throw a flag to show the problem is solved solved = true; } - //Reset the problem so it can be run again void Problem22::reset(){ Problem::reset(); @@ -460,6 +456,17 @@ void Problem22::reset(){ reserveVectors(); } + +//Gets +//Return a string with the solution to the problem +std::string Problem22::getResult(){ + if(!solved){ + throw Unsolved(); + } + std::stringstream result; + result << "The answer to the question is " << mee::getSum(prod); + return result.str(); +} //Returns the vector of the names being scored std::vector Problem22::getNames() const{ //If the problem hasn't been solved throw an exception @@ -468,7 +475,6 @@ std::vector Problem22::getNames() const{ } return names; } - //Returns the sum of the names' scores uint64_t Problem22::getNameScoreSum() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem23.cpp b/src/Problems/Problem23.cpp similarity index 93% rename from Source/Problem23.cpp rename to src/Problems/Problem23.cpp index 45f200a..6059146 100644 --- a/Source/Problem23.cpp +++ b/src/Problems/Problem23.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/Source/Problem23.cpp //Matthew Ellison // Created: 11-09-18 -//Modified: 07-09-20 +//Modified: 08-28-20 //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/myClasses /* @@ -29,7 +29,7 @@ #include #include "Stopwatch.hpp" #include "Algorithms.hpp" -#include "../Headers/Problem23.hpp" +#include "Problems/Problem23.hpp" //The largest possible number that can not be written as the sum of two abundant numbers @@ -54,7 +54,6 @@ bool Problem23::isSum(const std::vector& abund, int num){ //If you have run through the entire list and did not find a sum then it is false return false; } - //Reserve the size of the vector to speed up insertion void Problem23::reserveVectors(){ //This makes sure the vector is the correct size @@ -104,13 +103,9 @@ void Problem23::solve(){ //Stop the timer timer.stop(); - //Save the results - result << "The answer is " << sum; - //Throw a flag to show the problem is solved solved = true; } - //Reset the problem so it can be run again void Problem23::reset(){ Problem::reset(); @@ -119,6 +114,16 @@ void Problem23::reset(){ reserveVectors(); } +//Gets +//Return a string with the solution to the problem +std::string Problem23::getResult(){ + if(!solved){ + throw Unsolved(); + } + std::stringstream result; + result << "The answer is " << sum; + return result.str(); +} //Returns the sum of the numbers asked for uint64_t Problem23::getSum() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem24.cpp b/src/Problems/Problem24.cpp similarity index 91% rename from Source/Problem24.cpp rename to src/Problems/Problem24.cpp index 7eca710..5261443 100644 --- a/Source/Problem24.cpp +++ b/src/Problems/Problem24.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/Source/Problem24.cpp //Matthew Ellison // Created: 11-11-18 -//Modified: 07-09-20 +//Modified: 08-28-20 //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/myClasses /* @@ -27,7 +27,7 @@ #include #include "Algorithms.hpp" #include "Stopwatch.hpp" -#include "../Headers/Problem24.hpp" +#include "Problems/Problem24.hpp" //The number of the permutation that you need @@ -55,19 +55,25 @@ void Problem24::solve(){ //Stop the timer timer.stop(); - //Save the results - result << "The 1 millionth permutation is " << permutations.at(NEEDED_PERM - 1); - //Throw a flag to show the problem is solved solved = true; } - //Reset the problem so it can be run again void Problem24::reset(){ Problem::reset(); permutations.clear(); } +//Gets +//Return a string with the solution to the problem +std::string Problem24::getResult(){ + if(!solved){ + throw Unsolved(); + } + std::stringstream result; + result << "The 1 millionth permutation is " << permutations.at(NEEDED_PERM - 1); + return result.str(); +} //Returns a vector with all of the permutations std::vector Problem24::getPermutationsList() const{ //If the problem hasn't been solved throw an exception @@ -76,7 +82,6 @@ std::vector Problem24::getPermutationsList() const{ } return permutations; } - //Returns the specific permutations you are looking for std::string Problem24::getPermutation() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem25.cpp b/src/Problems/Problem25.cpp similarity index 93% rename from Source/Problem25.cpp rename to src/Problems/Problem25.cpp index b178c66..8cb86ac 100644 --- a/Source/Problem25.cpp +++ b/src/Problems/Problem25.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/Source/Problem25.cpp //Matthew Ellison // Created: 11-13-18 -//Modified: 07-09-20 +//Modified: 08-28-20 //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/myClasses //This file contains a header from the gmp library. The library is used for large integers. @@ -30,7 +30,7 @@ #include "gmpxx.h" #include "Algorithms.hpp" #include "Stopwatch.hpp" -#include "../Headers/Problem25.hpp" +#include "Problems/Problem25.hpp" //The number of digits to calculate up to @@ -61,14 +61,9 @@ void Problem25::solve(){ //Stop the timer timer.stop(); - //Save the results - result << "The first Fibonacci number with " << NUM_DIGITS << " digits is " << number - << "\nIts index is " << index; - //Throw a flag to show the problem is solved solved = true; } - //Reset the problem so it can be run again void Problem25::reset(){ Problem::reset(); @@ -76,6 +71,17 @@ void Problem25::reset(){ index = 2; } +//Gets +//Return a string with the solution to the problem +std::string Problem25::getResult(){ + if(!solved){ + throw Unsolved(); + } + std::stringstream result; + result << "The first Fibonacci number with " << NUM_DIGITS << " digits is " << number + << "\nIts index is " << index; + return result.str(); +} //Returns the Fibonacci number asked for mpz_class Problem25::getNumber() const{ //If the problem hasn't been solved throw an exception @@ -84,7 +90,6 @@ mpz_class Problem25::getNumber() const{ } return number; } - //Returns the Fibonacci number asked for as a string std::string Problem25::getNumberString() const{ //If the problem hasn't been solved throw an exception @@ -93,7 +98,6 @@ std::string Problem25::getNumberString() const{ } return number.get_str(); } - //Returns the index of the requested Fibonacci number mpz_class Problem25::getIndex() const{ //If the problem hasn't been solved throw an exception @@ -102,7 +106,6 @@ mpz_class Problem25::getIndex() const{ } return index; } - //Returns the index of the requested Fibonacci number as a string std::string Problem25::getIndexString() const{ //If the problem hasn't been solved throw an exception @@ -111,7 +114,6 @@ std::string Problem25::getIndexString() const{ } return index.get_str(); } - //Returns the index of the requested Fibonacci number as a uint64_t uint64_t Problem25::getIndexInt() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem26.cpp b/src/Problems/Problem26.cpp similarity index 92% rename from Source/Problem26.cpp rename to src/Problems/Problem26.cpp index 03c647c..66a9136 100644 --- a/Source/Problem26.cpp +++ b/src/Problems/Problem26.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/Source/Problem26.cpp //Matthew Ellison // Created: 07-28-19 -//Modified: 07-09-20 +//Modified: 08-28-20 //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/myClasses /* @@ -26,7 +26,7 @@ #include #include #include "Algorithms.hpp" -#include "../Headers/Problem26.hpp" +#include "Problems/Problem26.hpp" //Holds the highest denominator we will check @@ -89,14 +89,9 @@ void Problem26::solve(){ //Stop the timer timer.stop(); - //Save the results - result << "The longest cycle is " << longestCycle << " digits long\n" - << "It is started with the number " << longestNumber; - //Throw a flag to show the problem is solved solved = true; } - //Reset the problem so it can be run again void Problem26::reset(){ Problem::reset(); @@ -104,6 +99,17 @@ void Problem26::reset(){ longestNumber = 1; } +//Gets +//Return a string with the solution to the problem +std::string Problem26::getResult(){ + if(!solved){ + throw Unsolved(); + } + std::stringstream result; + result << "The longest cycle is " << longestCycle << " digits long" + << "\nIt is started with the number " << longestNumber; + return result.str(); +} //Returns the length of the longest cycle unsigned int Problem26::getLongestCycle() const{ //If the problem hasn't been solved throw an exception @@ -112,7 +118,6 @@ unsigned int Problem26::getLongestCycle() const{ } return longestCycle; } - //Returns the denominator that starts the longest cycle unsigned int Problem26::getLongestNumber() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem27.cpp b/src/Problems/Problem27.cpp similarity index 90% rename from Source/Problem27.cpp rename to src/Problems/Problem27.cpp index c05db9f..ef569d0 100644 --- a/Source/Problem27.cpp +++ b/src/Problems/Problem27.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/Source/Problem27.cpp //Matthew Ellison // Created: 09-14-19 -//Modified: 07-09-20 +//Modified: 08-28-20 //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/myClasses /* @@ -25,7 +25,7 @@ #include #include #include "Algorithms.hpp" -#include "../Headers/Problem27.hpp" +#include "Problems/Problem27.hpp" //Constructor @@ -70,15 +70,9 @@ void Problem27::solve(){ //Stop the timer timer.stop(); - //Save the results - result << "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; - //Throw a flag to show the problem is solved solved = true; } - //Reset the problem so it can be run again void Problem27::reset(){ Problem::reset(); @@ -86,6 +80,18 @@ void Problem27::reset(){ primes.clear(); } +//Gets +//Return a string with the solution to the problem +std::string Problem27::getResult(){ + if(!solved){ + throw Unsolved(); + } + std::stringstream result; + result << "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; + return result.str(); +} //Returns the top A that was generated int64_t Problem27::getTopA() const{ //If the problem hasn't been solved throw an exception @@ -94,7 +100,6 @@ int64_t Problem27::getTopA() const{ } return topA; } - //Returns the top B that was generated int64_t Problem27::getTopB() const{ //If the problem hasn't been solved throw an exception @@ -103,7 +108,6 @@ int64_t Problem27::getTopB() const{ } return topB; } - //Returns the top N that was generated int64_t Problem27::getTopN() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem28.cpp b/src/Problems/Problem28.cpp similarity index 95% rename from Source/Problem28.cpp rename to src/Problems/Problem28.cpp index e4f2289..10fc98a 100644 --- a/Source/Problem28.cpp +++ b/src/Problems/Problem28.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/Source/Problem28.cpp //Matthew Ellison // Created: 09-21-19 -//Modified: 07-09-20 +//Modified: 08-28-20 //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/myClasses /* @@ -26,7 +26,7 @@ #include #include #include -#include "../Headers/Problem28.hpp" +#include "Problems/Problem28.hpp" //This sets up the grid to hold the correct number of variables @@ -39,7 +39,6 @@ void Problem28::setupGrid(){ } } } - //Puts all of the numbers in the grid up the grid void Problem28::createGrid(){ bool finalLocation = false; //A flag to indicate if the final location to be filled has been reached @@ -84,7 +83,6 @@ void Problem28::createGrid(){ } } } - //Finds the sum of the diagonals in the grid void Problem28::findSum(){ //Start at the top corners and work your way down moving toward the opposite side @@ -132,13 +130,9 @@ void Problem28::solve(){ //Stop the timer timer.stop(); - //Save the results - result << "The sum of the diagonals in the given grid is " << sumOfDiagonals; - //Throw a flag to show the problem is solved solved = true; } - //Reset the problem so it can be run again void Problem28::reset(){ Problem::reset(); @@ -147,6 +141,16 @@ void Problem28::reset(){ setupGrid(); } +//Gets +//Return a string with the solution to the problem +std::string Problem28::getResult(){ + if(!solved){ + throw Unsolved(); + } + std::stringstream result; + result << "The sum of the diagonals in the given grid is " << sumOfDiagonals; + return result.str(); +} //Returns the grid std::vector> Problem28::getGrid() const{ //If the problem hasn't been solved throw an exception @@ -155,7 +159,6 @@ std::vector> Problem28::getGrid() const{ } return grid; } - //Returns the sum of the diagonals uint64_t Problem28::getSum() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem29.cpp b/src/Problems/Problem29.cpp similarity index 94% rename from Source/Problem29.cpp rename to src/Problems/Problem29.cpp index cead5f1..acb04f3 100644 --- a/Source/Problem29.cpp +++ b/src/Problems/Problem29.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/Source/Problem29.cpp //Matthew Ellison // Created: 10-06-19 -//Modified: 07-09-20 +//Modified: 08-28-20 //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/myClasses //This file contains a header from the gmp library. The library is used for large integers. @@ -30,7 +30,7 @@ #include #include #include -#include "../Headers/Problem29.hpp" +#include "Problems/Problem29.hpp" #include "Algorithms.hpp" @@ -73,19 +73,25 @@ void Problem29::solve(){ //Stop the timer timer.stop(); - //Save the results - result << "The number of unique values generated by a^b for " << BOTTOM_A << " <= a <= " << TOP_A << " and " << BOTTOM_B << " <= b <= " << TOP_B << " is " << unique.size(); - //Throw a flag to show the problem is solved solved = true; } - //Reset the problem so it can be run again void Problem29::reset(){ Problem::reset(); unique.clear(); } +//Gets +//Return a string with the solution to the problem +std::string Problem29::getResult(){ + if(!solved){ + throw Unsolved(); + } + std::stringstream result; + result << "The number of unique values generated by a^b for " << BOTTOM_A << " <= a <= " << TOP_A << " and " << BOTTOM_B << " <= b <= " << TOP_B << " is " << unique.size(); + return result.str(); +} //Returns the lowest possible value for a unsigned int Problem29::getBottomA() const{ //If the problem hasn't been solved throw an exception @@ -94,7 +100,6 @@ unsigned int Problem29::getBottomA() const{ } return BOTTOM_A; } - //Returns the highest possible value for a unsigned int Problem29::getTopA() const{ //If the problem hasn't been solved throw an exception @@ -103,7 +108,6 @@ unsigned int Problem29::getTopA() const{ } return TOP_A; } - //Returns the lowest possible value for b unsigned int Problem29::getBottomB() const{ //If the problem hasn't been solved throw an exception @@ -112,7 +116,6 @@ unsigned int Problem29::getBottomB() const{ } return BOTTOM_B; } - //Returns the highest possible value for b unsigned int Problem29::getTopB() const{ //If the problem hasn't been solved throw an exception @@ -121,7 +124,6 @@ unsigned int Problem29::getTopB() const{ } return TOP_B; } - //Returns a vector of all the unique values for a^b std::vector Problem29::getUnique() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem3.cpp b/src/Problems/Problem3.cpp similarity index 91% rename from Source/Problem3.cpp rename to src/Problems/Problem3.cpp index c4ec9ba..e9767d6 100644 --- a/Source/Problem3.cpp +++ b/src/Problems/Problem3.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/Source/Problem3.cpp //Matthew Ellison // Created: 09-28-18 -//Modified: 07-09-20 +//Modified: 08-28-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/myClasses /* @@ -28,8 +28,7 @@ #include #include "Stopwatch.hpp" #include "Algorithms.hpp" -#include "../Headers/Problem.hpp" -#include "../Headers/Problem3.hpp" +#include "Problems/Problem3.hpp" //The number of which you are trying to find the factors @@ -54,19 +53,25 @@ void Problem3::solve(){ //Stop the timer timer.stop(); - //Save the results - result << "The largest factor of the number " << GOAL_NUMBER << " is " << factors[factors.size() - 1]; - //Throw a flag to show the problem is solved solved = true; } - //Reset the problem so it can be run again void Problem3::reset(){ Problem::reset(); factors.clear(); } +//Gets +//Return a string with the solution to the problem +std::string Problem3::getResult(){ + if(!solved){ + throw Unsolved(); + } + std::stringstream result; + result << "The largest factor of the number " << GOAL_NUMBER << " is " << factors[factors.size() - 1]; + return result.str(); +} //Returns the list of factors of the number std::vector Problem3::getFactors() const{ //If the problem hasn't been solved throw an exception @@ -75,7 +80,6 @@ std::vector Problem3::getFactors() const{ } return factors; } - //Returns the largest factor of the number uint64_t Problem3::getLargestFactor() const{ //If the problem hasn't been solved throw an exception @@ -85,7 +89,6 @@ uint64_t Problem3::getLargestFactor() const{ return *factors.end(); } - //Returns the number uint64_t Problem3::getGoalNumber() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem30.cpp b/src/Problems/Problem30.cpp similarity index 94% rename from Source/Problem30.cpp rename to src/Problems/Problem30.cpp index b44f22e..1d66fd5 100644 --- a/Source/Problem30.cpp +++ b/src/Problems/Problem30.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/Source/Problem30.cpp //Matthew Ellison // Created: 10-27-19 -//Modified: 07-09-20 +//Modified: 08-28-20 //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/myClasses /* @@ -27,7 +27,7 @@ #include #include #include -#include "../Headers/Problem30.hpp" +#include "Problems/Problem30.hpp" //This is the largest number that will be checked @@ -85,17 +85,23 @@ void Problem30::solve(){ //Throw a flag to show the problem is solved solved = true; - - //Save the results - result << "The sum of all the numbers that can be written as the sum of the fifth powers of their digits is " << getSumOfList(); } - //Reset the problem so it can be run again void Problem30::reset(){ Problem::reset(); sumOfFifthNumbers.clear(); } +//Gets +//Return a string with the solution to the problem +std::string Problem30::getResult(){ + if(!solved){ + throw Unsolved(); + } + std::stringstream result; + result << "The sum of all the numbers that can be written as the sum of the fifth powers of their digits is " << getSumOfList(); + return result.str(); +} //This returns the top number to be checked uint64_t Problem30::getTopNum() const{ //If the problem hasn't been solved throw an exception @@ -105,7 +111,6 @@ uint64_t Problem30::getTopNum() const{ return TOP_NUM; } - //This returns a copy of the vector holding all the numbers that are the sum of the fifth power of their digits std::vector Problem30::getListOfSumOfFifths() const{ //If the problem hasn't been solved throw an exception @@ -115,7 +120,6 @@ std::vector Problem30::getListOfSumOfFifths() const{ return sumOfFifthNumbers; } - //This returns the sum of all entries in sumOfFifthNumbers uint64_t Problem30::getSumOfList() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem31.cpp b/src/Problems/Problem31.cpp similarity index 91% rename from Source/Problem31.cpp rename to src/Problems/Problem31.cpp index 2cb9ece..4ec887f 100644 --- a/Source/Problem31.cpp +++ b/src/Problems/Problem31.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/Source/Problem31.cpp //Matthew Ellison // Created: 06-19-20 -//Modified: 07-11-20 +//Modified: 08-28-20 //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/myClasses /* @@ -25,7 +25,7 @@ #include #include #include -#include "../Headers/Problem31.hpp" +#include "Problems/Problem31.hpp" //The value of coins we want @@ -67,19 +67,25 @@ void Problem31::solve(){ //Stop the timer timer.stop(); - //Save the results - result << "There are " << permutations << " ways to make 2 pounds with the given denominations of coins"; - //Throw a flag to show the problem is solved solved = true; } - //Reset the problem so it can be run again void Problem31::reset(){ Problem::reset(); permutations = 0; } +//Gets +//Return a string with the solution to the problem +std::string Problem31::getResult(){ + if(!solved){ + throw Unsolved(); + } + std::stringstream result; + result << "There are " << permutations << " ways to make 2 pounds with the given denominations of coins"; + return result.str(); +} //Returns the number of correct permutations of the coins int Problem31::getPermutations() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem32.cpp b/src/Problems/Problem32.cpp similarity index 94% rename from Source/Problem32.cpp rename to src/Problems/Problem32.cpp index 6960bde..f14354f 100644 --- a/Source/Problem32.cpp +++ b/src/Problems/Problem32.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/Source/Problem32.cpp //Matthew Ellison // Created: 07-27-20 -//Modified: 07-27-20 +//Modified: 08-28-20 //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/myClasses /* @@ -27,7 +27,7 @@ #include #include #include "Algorithms.hpp" -#include "../Headers/Problem32.hpp" +#include "Problems/Problem32.hpp" #include int Problem32::TOP_MULTIPLICAND = 99; //The largest multiplicand to check @@ -94,9 +94,6 @@ void Problem32::solve(){ //Stop the timer timer.stop(); - //Save the results - result << "There are " << listOfProducts.size() << " unique 1-9 pandigitals\nThe sum of the products of these pandigitals is " << sumOfPandigitals; - //Throw a flag to show the problem is solved solved = true; } @@ -106,7 +103,17 @@ void Problem32::reset(){ listOfProducts.clear(); sumOfPandigitals = 0; } + //Gets +//Return a string with the solution to the problem +std::string Problem32::getResult(){ + if(!solved){ + throw Unsolved(); + } + std::stringstream result; + result << "There are " << listOfProducts.size() << " unique 1-9 pandigitals\nThe sum of the products of these pandigitals is " << sumOfPandigitals; + return result.str(); +} //Returns the sum of the pandigitals int64_t Problem32::getSumOfPandigitals(){ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem4.cpp b/src/Problems/Problem4.cpp similarity index 93% rename from Source/Problem4.cpp rename to src/Problems/Problem4.cpp index bcfddfc..cc062f6 100644 --- a/Source/Problem4.cpp +++ b/src/Problems/Problem4.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/Source/Problem4.cpp //Matthew Ellison // Created: 09-28-18 -//Modified: 07-09-20 +//Modified: 08-28-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/myClasses /* @@ -27,8 +27,7 @@ #include #include #include "Stopwatch.hpp" -#include "../Headers/Problem.hpp" -#include "../Headers/Problem4.hpp" +#include "Problems/Problem4.hpp" //The first number to check @@ -75,19 +74,25 @@ void Problem4::solve(){ //Stop the timer timer.stop(); - //Save the results - result << "The largest palindrome is " << *(palindromes.end() - 1); - //Throw a flag to show the problem is solved solved = true; } - //Resets the problem so it can be run again void Problem4::reset(){ Problem::reset(); palindromes.clear(); } +//Gets +//Return a string with the solution to the problem +std::string Problem4::getResult(){ + if(!solved){ + throw Unsolved(); + } + std::stringstream result; + result << "The largest palindrome is " << *(palindromes.end() - 1); + return result.str(); +} //Returns the list of all palindromes std::vector Problem4::getPalindromes() const{ //If the problem hasn't been solved throw an exception @@ -96,7 +101,6 @@ std::vector Problem4::getPalindromes() const{ } return palindromes; } - //Returns the largest palindrome uint64_t Problem4::getLargestPalindrome() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem5.cpp b/src/Problems/Problem5.cpp similarity index 92% rename from Source/Problem5.cpp rename to src/Problems/Problem5.cpp index 8424ec4..42d6fa4 100644 --- a/Source/Problem5.cpp +++ b/src/Problems/Problem5.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/Source/Problem5.cpp //Matthew Ellison // Created: 09-28-18 -//Modified: 07-09-20 +//Modified: 08-28-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/myClasses /* @@ -27,8 +27,7 @@ #include #include #include "Stopwatch.hpp" -#include "../Headers/Problem.hpp" -#include "../Headers/Problem5.hpp" +#include "Problems/Problem5.hpp" //Constructor @@ -72,19 +71,25 @@ void Problem5::solve(){ //Stop the timer timer.stop(); - //Save the results - result << "The smallest positive number evenly divisible by all numbers 1-20 is " << smallestNum; - //Throw a flag to show the problem is solved solved = true; } - //Reset the problem so it can be run again void Problem5::reset(){ Problem::reset(); smallestNum = 0; } +//Gets +//Return a string with the solution to the problem +std::string Problem5::getResult(){ + if(!solved){ + throw Unsolved(); + } + std::stringstream result; + result << "The smallest positive number evenly divisible by all numbers 1-20 is " << smallestNum; + return result.str(); +} //Returns the requested number int Problem5::getNumber() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem6.cpp b/src/Problems/Problem6.cpp similarity index 92% rename from Source/Problem6.cpp rename to src/Problems/Problem6.cpp index e5959e3..d44d537 100644 --- a/Source/Problem6.cpp +++ b/src/Problems/Problem6.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/Source/Problem6.cpp //Matthew Ellison // Created: 09-28-18 -//Modified: 07-09-20 +//Modified: 08-28-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/myClasses /* @@ -27,8 +27,7 @@ #include #include #include "Stopwatch.hpp" -#include "../Headers/Problem.hpp" -#include "../Headers/Problem6.hpp" +#include "Problems/Problem6.hpp" int Problem6::START_NUM = 1; //The first number to check @@ -58,19 +57,25 @@ void Problem6::solve(){ //Stop the timer timer.stop(); - //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 " << abs(sumOfSquares - squareOfSum); - //Throw a flag to show the problem is solved solved = true; } - //Reset the problem so it can be run again void Problem6::reset(){ Problem::reset(); sumOfSquares = squareOfSum = 0; } +//Gets +//Return a string with the solution to the problem +std::string Problem6::getResult(){ + if(!solved){ + throw Unsolved(); + } + std::stringstream result; + result << "The difference between the sum of the squares and the square of the sum of all numbers from 1-100 is " << abs(sumOfSquares - squareOfSum); + return result.str(); +} //Returns the sum of all the squares uint64_t Problem6::getSumOfSquares() const{ //If the problem hasn't been solved throw an exception @@ -79,7 +84,6 @@ uint64_t Problem6::getSumOfSquares() const{ } return sumOfSquares; } - //Returns the square of all of the sums uint64_t Problem6::getSquareOfSum() const{ //If the problem hasn't been solved throw an exception @@ -88,7 +92,6 @@ uint64_t Problem6::getSquareOfSum() const{ } return squareOfSum; } - //Returns the requested difference uint64_t Problem6::getDifference() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem67.cpp b/src/Problems/Problem67.cpp similarity index 99% rename from Source/Problem67.cpp rename to src/Problems/Problem67.cpp index fe5fa86..cdddb7c 100644 --- a/Source/Problem67.cpp +++ b/src/Problems/Problem67.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/Source/Problem67.cpp //Matthew Ellison // Created: 11-02-18 -//Modified: 07-09-20 +//Modified: 08-28-20 //The way to do this is using a breadth first search /* Find the maximum total from top to bottom @@ -126,7 +126,7 @@ Find the maximum total from top to bottom #include -#include "../Headers/Problem67.hpp" +#include "Problems/Problem67.hpp" //This is the list you are trying to find a path through diff --git a/Source/Problem7.cpp b/src/Problems/Problem7.cpp similarity index 89% rename from Source/Problem7.cpp rename to src/Problems/Problem7.cpp index af6f567..e190115 100644 --- a/Source/Problem7.cpp +++ b/src/Problems/Problem7.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/Source/Problem7.cpp //Matthew Ellison // Created: 09-28-18 -//Modified: 07-09-20 +//Modified: 08-28-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/myClasses /* @@ -28,8 +28,7 @@ #include #include "Stopwatch.hpp" #include "Algorithms.hpp" -#include "../Headers/Problem.hpp" -#include "../Headers/Problem7.hpp" +#include "Problems/Problem7.hpp" //The index of the prime number to find @@ -54,18 +53,24 @@ void Problem7::solve(){ //Stop the timer timer.stop(); - //Save the results - result << "The " << NUMBER_OF_PRIMES << "th prime number is " << primes.at(primes.size() - 1); - //Throw a flag to show the problem is solved solved = true; } - //Reset the problem so it can be run again void Problem7::reset(){ Problem::reset(); } +//Gets +//Return a string with the solution to the problem +std::string Problem7::getResult(){ + if(!solved){ + throw Unsolved(); + } + std::stringstream result; + result << "The " << NUMBER_OF_PRIMES << "th prime number is " << primes.at(primes.size() - 1); + return result.str(); +} //Returns the requested prime number uint64_t Problem7::getPrime() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem8.cpp b/src/Problems/Problem8.cpp similarity index 95% rename from Source/Problem8.cpp rename to src/Problems/Problem8.cpp index 91b2f51..cbe0154 100644 --- a/Source/Problem8.cpp +++ b/src/Problems/Problem8.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/Source/Problem8.cpp //Matthew Ellison // Created: 09-28-18 -//Modified: 07-09-20 +//Modified: 08-28-20 //Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product? /* 73167176531330624919225119674426574742355349194934 @@ -49,8 +49,7 @@ #include #include #include "Stopwatch.hpp" -#include "../Headers/Problem.hpp" -#include "../Headers/Problem8.hpp" +#include "Problems/Problem8.hpp" //The number that we are working with @@ -84,14 +83,9 @@ void Problem8::solve(){ //Stop the timer timer.stop(); - //Save the results - result << "The greatest product is " << maxProduct - << "\nThe numbers are " << maxNums; - //Throw a flag to show the problem is solved solved = true; } - //Reset the problem so it can be run void Problem8::reset(){ Problem::reset(); @@ -99,6 +93,17 @@ void Problem8::reset(){ maxProduct = 0; } +//Gets +//Return a string with the solution to the problem +std::string Problem8::getResult(){ + if(!solved){ + throw Unsolved(); + } + std::stringstream result; + result << "The greatest product is " << maxProduct + << "\nThe numbers are " << maxNums; + return result.str(); +} //Returns the string of numbers that produces the largest product std::string Problem8::getLargestNums() const{ //If the problem hasn't been solved throw an exception @@ -107,7 +112,6 @@ std::string Problem8::getLargestNums() const{ } return maxNums; } - //Returns the requested product uint64_t Problem8::getLargestProduct() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem9.cpp b/src/Problems/Problem9.cpp similarity index 88% rename from Source/Problem9.cpp rename to src/Problems/Problem9.cpp index 736103b..4d9e976 100644 --- a/Source/Problem9.cpp +++ b/src/Problems/Problem9.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/Source/Problem9.cpp //Matthew Ellison // Created: 09-28-18 -//Modified: 07-09-20 +//Modified: 08-28-20 //There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product of abc. //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses /* @@ -26,8 +26,7 @@ #include #include #include "Stopwatch.hpp" -#include "../Headers/Problem.hpp" -#include "../Headers/Problem9.hpp" +#include "Problems/Problem9.hpp" //Constructor @@ -68,17 +67,15 @@ void Problem9::solve(){ //Save the results if(found){ - result << "The Pythagorean triplet is " << a << ' ' << b << ' ' << (int)c - << "\nThe numbers' product is " << a * b * (int)c; + solved = true; } else{ - result << "The number was not found!"; + //TODO: Throw an exception } //Throw a flag to show the problem is solved - solved = true; + } - //Reset the problem so it can be run again void Problem9::reset(){ Problem::reset(); @@ -88,6 +85,17 @@ void Problem9::reset(){ found = false; } +//Gets +//Return a string with the solution to the problem +std::string Problem9::getResult(){ + if(!solved){ + throw Unsolved(); + } + std::stringstream result; + result << "The Pythagorean triplet is " << a << ' ' << b << ' ' << (int)c + << "\nThe numbers' product is " << a * b * (int)c; + return result.str(); +} //Returns the length of the first side int Problem9::getSideA() const{ //If the problem hasn't been solved throw an exception @@ -96,7 +104,6 @@ int Problem9::getSideA() const{ } return a; } - //Returns the length of the second side int Problem9::getSideB() const{ //If the problem hasn't been solved throw an exception @@ -105,7 +112,6 @@ int Problem9::getSideB() const{ } return b; } - //Returns the length of the hyp int Problem9::getSideC() const{ //If the problem hasn't been solved throw an exception @@ -114,7 +120,6 @@ int Problem9::getSideC() const{ } return (int)c; } - //Returns the product of the 3 sides int Problem9::getProduct() const{ //If the problem hasn't been solved throw an exception diff --git a/main.cpp b/src/main.cpp similarity index 99% rename from main.cpp rename to src/main.cpp index 2749905..3c6b85c 100644 --- a/main.cpp +++ b/src/main.cpp @@ -26,7 +26,7 @@ #include #include "benchmark.hpp" #include "Algorithms.hpp" -#include "Headers/Problem.hpp" +#include "Problem.hpp" #include "ProblemSelection.hpp"