diff --git a/Headers/Problem.hpp b/Headers/Problem.hpp index 40b6d1e..9103dfd 100644 --- a/Headers/Problem.hpp +++ b/Headers/Problem.hpp @@ -24,6 +24,7 @@ #ifndef PROBLEM_HPP #define PROBLEM_HPP +#include #include #include "Stopwatch.hpp" @@ -34,6 +35,7 @@ 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){ @@ -47,6 +49,7 @@ public: virtual std::string getDescription() const{ return description; } + //Get the time it took to run the algorithm as a string virtual std::string getTime(){ //If the problem hasn't been solved throw an exception if(!solved){ @@ -54,6 +57,7 @@ public: } return timer.getStr(); } + //Get a copy of the timer that was used to time the algorithm virtual mee::Stopwatch getTimer(){ //If the problem hasn't been solved throw an exception if(!solved){ @@ -63,12 +67,20 @@ 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 - virtual std::string getString() const = 0; //Return a string with the solution to the problem }; #endif //PROBLEM_HPP diff --git a/Headers/Problem1.hpp b/Headers/Problem1.hpp index 8711836..1859f10 100644 --- a/Headers/Problem1.hpp +++ b/Headers/Problem1.hpp @@ -45,7 +45,6 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getString() const; //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/Problem10.hpp index 8635241..8f1e3f1 100644 --- a/Headers/Problem10.hpp +++ b/Headers/Problem10.hpp @@ -44,7 +44,6 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getString() const; //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/Problem11.hpp index 2f26f09..fba714f 100644 --- a/Headers/Problem11.hpp +++ b/Headers/Problem11.hpp @@ -66,7 +66,6 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getString() const; //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/Problem12.hpp index afd7468..7b0dd57 100644 --- a/Headers/Problem12.hpp +++ b/Headers/Problem12.hpp @@ -46,7 +46,6 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getString() const; //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/Problem13.hpp index 33febb2..0ee0bc6 100644 --- a/Headers/Problem13.hpp +++ b/Headers/Problem13.hpp @@ -154,7 +154,6 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getString() const; //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/Problem14.hpp index 4d97cac..ecb9de4 100644 --- a/Headers/Problem14.hpp +++ b/Headers/Problem14.hpp @@ -53,7 +53,6 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getString() const; //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/Problem15.hpp index 9fee0f0..acd043b 100644 --- a/Headers/Problem15.hpp +++ b/Headers/Problem15.hpp @@ -50,7 +50,6 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getString() const; //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/Problem16.hpp index 4865a1c..bb6e9e5 100644 --- a/Headers/Problem16.hpp +++ b/Headers/Problem16.hpp @@ -49,7 +49,6 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getString() const; //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/Problem17.hpp index f299763..06f033d 100644 --- a/Headers/Problem17.hpp +++ b/Headers/Problem17.hpp @@ -50,7 +50,6 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getString() const; //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/Problem18.hpp index 0207557..fbf8bdc 100644 --- a/Headers/Problem18.hpp +++ b/Headers/Problem18.hpp @@ -79,7 +79,6 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getString() const; //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/Problem19.hpp index 1ce7752..fbf058e 100644 --- a/Headers/Problem19.hpp +++ b/Headers/Problem19.hpp @@ -62,7 +62,6 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getString() const; //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/Problem2.hpp index 6f38c64..49e32a8 100644 --- a/Headers/Problem2.hpp +++ b/Headers/Problem2.hpp @@ -44,7 +44,6 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getString() const; //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/Problem20.hpp index 458b217..f9b03b1 100644 --- a/Headers/Problem20.hpp +++ b/Headers/Problem20.hpp @@ -47,7 +47,6 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getString() const; //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/Problem21.hpp index 62bcd57..de46eb5 100644 --- a/Headers/Problem21.hpp +++ b/Headers/Problem21.hpp @@ -49,7 +49,6 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getString() const; //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/Problem22.hpp index 17c9b04..054f547 100644 --- a/Headers/Problem22.hpp +++ b/Headers/Problem22.hpp @@ -49,7 +49,6 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getString() const; //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/Problem23.hpp index f38a939..9538412 100644 --- a/Headers/Problem23.hpp +++ b/Headers/Problem23.hpp @@ -50,7 +50,6 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getString() const; //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/Problem24.hpp index f8a4186..f95abfc 100644 --- a/Headers/Problem24.hpp +++ b/Headers/Problem24.hpp @@ -44,7 +44,6 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getString() const; //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/Problem25.hpp index 837a4d9..9761ffb 100644 --- a/Headers/Problem25.hpp +++ b/Headers/Problem25.hpp @@ -48,7 +48,6 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getString() const; //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/Problem26.hpp index ff4a99c..5925cef 100644 --- a/Headers/Problem26.hpp +++ b/Headers/Problem26.hpp @@ -45,7 +45,6 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getString() const; //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/Problem27.hpp index 999770b..8382ba0 100644 --- a/Headers/Problem27.hpp +++ b/Headers/Problem27.hpp @@ -46,7 +46,6 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getString() const; //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/Problem28.hpp index d288faf..4dd80a6 100644 --- a/Headers/Problem28.hpp +++ b/Headers/Problem28.hpp @@ -50,7 +50,6 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getString() const; //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/Problem29.hpp index 1df5d6d..8cbca47 100644 --- a/Headers/Problem29.hpp +++ b/Headers/Problem29.hpp @@ -53,7 +53,6 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getString() const; //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/Problem3.hpp index ebce77e..1625a5f 100644 --- a/Headers/Problem3.hpp +++ b/Headers/Problem3.hpp @@ -45,7 +45,6 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getString() const; //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/Problem30.hpp index 827ae27..1af3161 100644 --- a/Headers/Problem30.hpp +++ b/Headers/Problem30.hpp @@ -50,7 +50,6 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getString() const; //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/Problem31.hpp index ddb164a..a4901c8 100644 --- a/Headers/Problem31.hpp +++ b/Headers/Problem31.hpp @@ -44,7 +44,6 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getString() const; //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/Problem4.hpp b/Headers/Problem4.hpp index 5a90898..62a0e59 100644 --- a/Headers/Problem4.hpp +++ b/Headers/Problem4.hpp @@ -45,7 +45,6 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getString() const; //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/Problem5.hpp index 019c8d4..32a8a0f 100644 --- a/Headers/Problem5.hpp +++ b/Headers/Problem5.hpp @@ -43,7 +43,6 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getString() const; //Return a string with the solution to the problem int getNumber() const; //Returns the requested number }; diff --git a/Headers/Problem6.hpp b/Headers/Problem6.hpp index c971a40..6c11f3e 100644 --- a/Headers/Problem6.hpp +++ b/Headers/Problem6.hpp @@ -46,7 +46,6 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getString() const; //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/Problem67.hpp index dbed840..f4e2e09 100644 --- a/Headers/Problem67.hpp +++ b/Headers/Problem67.hpp @@ -162,7 +162,6 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getString() const; //Return a string with the solution to the problem std::string getPyramid() const; //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/Problem7.hpp b/Headers/Problem7.hpp index 4ebe74c..10f417b 100644 --- a/Headers/Problem7.hpp +++ b/Headers/Problem7.hpp @@ -45,7 +45,6 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getString() const; //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/Problem8.hpp index ce4732e..d2b3063 100644 --- a/Headers/Problem8.hpp +++ b/Headers/Problem8.hpp @@ -68,7 +68,6 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getString() const; //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/Problem9.hpp index 8c89b52..91fd5a5 100644 --- a/Headers/Problem9.hpp +++ b/Headers/Problem9.hpp @@ -45,7 +45,6 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getString() const; //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/ProblemSelection.hpp b/ProblemSelection.hpp index 4950f21..d3a26a1 100644 --- a/ProblemSelection.hpp +++ b/ProblemSelection.hpp @@ -120,7 +120,7 @@ void solveProblem(Problem* problem){ //Solve the problem problem->solve(); //Print the results - std::cout << problem->getString() + std::cout << problem->getResults() << "\nIt took " << problem->getTime() << " to solve this problem.\n\n" << std::endl; } diff --git a/Source/Problem1.cpp b/Source/Problem1.cpp index cea180a..1da1afc 100644 --- a/Source/Problem1.cpp +++ b/Source/Problem1.cpp @@ -62,6 +62,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; } @@ -72,20 +75,8 @@ void Problem1::reset(){ fullSum = 0; } - //Gets -//Return a string with the solution to the problem -std::string Problem1::getString() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - //Create a string with the results and return it - std::stringstream results; - results << "The sum of all the numbers < " << MAX_NUMBER + 1 << " that are divisible by 3 or 5 is " << fullSum; - return results.str(); -} - +//Returns the requested sum uint64_t Problem1::getSum() const{ //If the prblem hasn't been solved throw an exception if(!solved){ diff --git a/Source/Problem10.cpp b/Source/Problem10.cpp index 9ef9171..be8157d 100644 --- a/Source/Problem10.cpp +++ b/Source/Problem10.cpp @@ -53,6 +53,9 @@ 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; } @@ -63,17 +66,6 @@ void Problem10::reset(){ sum = 0; } -//Return a string with the solution to the problem -std::string Problem10::getString() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - std::stringstream results; - results << "The sum of all the primes less than " << GOAL_NUMBER + 1 << " is " << sum; - return results.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/Source/Problem11.cpp index 8fc26f9..46d2b29 100644 --- a/Source/Problem11.cpp +++ b/Source/Problem11.cpp @@ -174,6 +174,10 @@ 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; } @@ -184,18 +188,6 @@ void Problem11::reset(){ greatestProduct.clear(); } -//Return a string with the solution to the problem -std::string Problem11::getString() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - std::stringstream results; - results << "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 results.str(); -} - //Returns the numbers that were being searched std::vector Problem11::getNumbers() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem12.cpp b/Source/Problem12.cpp index 940fd0e..ee0c4b3 100644 --- a/Source/Problem12.cpp +++ b/Source/Problem12.cpp @@ -66,6 +66,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; } @@ -78,17 +81,6 @@ void Problem12::reset(){ counter = 2; } -//Return a string with the solution to the problem -std::string Problem12::getString() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - std::stringstream results; - results << "The triangular number " << sum << " is a sum of all numbers >= " << counter - 1 << " and has " << divisors.size() << " divisors"; - return results.str(); -} - //Returns the triangular number int64_t Problem12::getTriangularNumber() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem13.cpp b/Source/Problem13.cpp index b8e6fc6..27f3508 100644 --- a/Source/Problem13.cpp +++ b/Source/Problem13.cpp @@ -275,6 +275,10 @@ 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; } @@ -287,19 +291,6 @@ void Problem13::reset(){ reserveVectors(); } -//Return a string with the solution to the problem -std::string Problem13::getString() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - - std::stringstream results; - results << "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 results.str(); -} - //Returns the list 50-digit numbers std::vector Problem13::getNumbers() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem14.cpp b/Source/Problem14.cpp index 9c43b04..d6754a6 100644 --- a/Source/Problem14.cpp +++ b/Source/Problem14.cpp @@ -82,6 +82,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; } @@ -93,18 +96,6 @@ void Problem14::reset(){ maxNum = 0; } -//Return a string with the solution to the problem -std::string Problem14::getString() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - - std::stringstream results; - results << "The number " << maxNum << " produced a chain of " << maxLength << " steps"; - return results.str(); -} - //Returns the length of the requested chain uint64_t Problem14::getLength() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem15.cpp b/Source/Problem15.cpp index e5b5a0d..7eaf852 100644 --- a/Source/Problem15.cpp +++ b/Source/Problem15.cpp @@ -78,6 +78,9 @@ 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; } @@ -88,18 +91,6 @@ void Problem15::reset(){ numOfRoutes = 0; } -//Return a string with the solution to the problem -std::string Problem15::getString() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - - std::stringstream results; - results << "The number of routes is " << numOfRoutes; - return results.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/Source/Problem16.cpp index b9367ad..41d84b2 100644 --- a/Source/Problem16.cpp +++ b/Source/Problem16.cpp @@ -65,6 +65,10 @@ 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; } @@ -76,20 +80,6 @@ void Problem16::reset(){ sumOfElements = 0; } - -//Return a string with the solution to the problem -std::string Problem16::getString() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - - std::stringstream results; - results << NUM_TO_POWER << '^' << POWER << " = " << num - << "\nThe sum of the elements is " << sumOfElements; - return results.str(); -} - //Returns the number that was calculated mpz_class Problem16::getNumber() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem17.cpp b/Source/Problem17.cpp index 78cfd86..cb42864 100644 --- a/Source/Problem17.cpp +++ b/Source/Problem17.cpp @@ -195,6 +195,9 @@ 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; } @@ -205,17 +208,6 @@ void Problem17::reset(){ letterCount = 0; } -//Return a string with the solution to the problem -std::string Problem17::getString() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - std::stringstream results; - results << "The number of letters is " << letterCount; - return results.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/Source/Problem18.cpp index a3f18bb..1588539 100644 --- a/Source/Problem18.cpp +++ b/Source/Problem18.cpp @@ -135,6 +135,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; } @@ -147,18 +150,6 @@ void Problem18::reset(){ actualTotal = 0; } -//Return a string with the solution to the problem -std::string Problem18::getString() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - - std::stringstream results; - results << "The value of the longest path is " << actualTotal; - return results.str(); -} - //Returns the pyramid that was traversed as a string std::string Problem18::getPyramid(){ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem19.cpp b/Source/Problem19.cpp index c71156a..8f26ccb 100644 --- a/Source/Problem19.cpp +++ b/Source/Problem19.cpp @@ -171,6 +171,9 @@ 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; } @@ -181,17 +184,6 @@ void Problem19::reset(){ totalSundays = 0; } -//Return a string with the solution to the problem -std::string Problem19::getString() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - std::stringstream results; - results << "There are " << totalSundays << " Sundays that landed on the first of the months from " << START_YEAR << " to " << END_YEAR; - return results.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/Source/Problem2.cpp index 5c3ce9e..5834b27 100644 --- a/Source/Problem2.cpp +++ b/Source/Problem2.cpp @@ -61,6 +61,9 @@ 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; } @@ -71,17 +74,6 @@ void Problem2::reset(){ fullSum = 0; } -//Return a string with the solution to the problem -std::string Problem2::getString() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - std::stringstream results; - results << "The sum of the even Fibonacci numbers less than 4,000,000 is " << fullSum; - return results.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/Source/Problem20.cpp index e1582b5..cbec545 100644 --- a/Source/Problem20.cpp +++ b/Source/Problem20.cpp @@ -64,6 +64,10 @@ 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; } @@ -75,19 +79,6 @@ void Problem20::reset(){ num = 1; } -//Return a string with the solution to the problem -std::string Problem20::getString() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - std::stringstream results; - //Print the results - results << "100! = " << num.get_str() - << "\nThe sum of the digits is: " << sum; - return results.str(); -} - //Returns the number 100! mpz_class Problem20::getNumber() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem21.cpp b/Source/Problem21.cpp index b57f9ae..5f6681f 100644 --- a/Source/Problem21.cpp +++ b/Source/Problem21.cpp @@ -87,6 +87,13 @@ 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; } @@ -99,22 +106,6 @@ void Problem21::reset(){ reserveVectors(); } -//Return a string with the solution to the problem -std::string Problem21::getString() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - - std::stringstream results; - results << "All amicable numbers less than 10000 are\n"; - for(unsigned int cnt = 0;cnt < amicable.size();++cnt){ - results << amicable.at(cnt) << '\n'; - } - results << "The sum of all of these amicable numbers is " << mee::getSum(amicable); - return results.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 diff --git a/Source/Problem22.cpp b/Source/Problem22.cpp index a431b55..4a9e025 100644 --- a/Source/Problem22.cpp +++ b/Source/Problem22.cpp @@ -445,6 +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; } @@ -457,17 +460,6 @@ void Problem22::reset(){ reserveVectors(); } -//Return a string with the solution to the problem -std::string Problem22::getString() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - std::stringstream results; - results << "The answer to the question is " << mee::getSum(prod); - return results.str(); -} - //Returns the vector of the names being scored std::vector Problem22::getNames() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem23.cpp b/Source/Problem23.cpp index e05e35f..45f200a 100644 --- a/Source/Problem23.cpp +++ b/Source/Problem23.cpp @@ -104,6 +104,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; } @@ -116,17 +119,6 @@ void Problem23::reset(){ reserveVectors(); } -//Return a string with the solution to the problem -std::string Problem23::getString() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - std::stringstream results; - results << "The answer is " << sum; - return results.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/Source/Problem24.cpp index 86b3f83..7eca710 100644 --- a/Source/Problem24.cpp +++ b/Source/Problem24.cpp @@ -55,6 +55,9 @@ 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; } @@ -65,18 +68,6 @@ void Problem24::reset(){ permutations.clear(); } -//Return a string with the solution to the problem -std::string Problem24::getString() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - std::stringstream results; - //Print the results - results << "The 1 millionth permutation is " << permutations.at(NEEDED_PERM - 1); - return results.str(); -} - //Returns a vector with all of the permutations std::vector Problem24::getPermutationsList() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem25.cpp b/Source/Problem25.cpp index 613effe..b178c66 100644 --- a/Source/Problem25.cpp +++ b/Source/Problem25.cpp @@ -61,6 +61,10 @@ 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; } @@ -72,19 +76,6 @@ void Problem25::reset(){ index = 2; } -//Return a string with the solution to the problem -std::string Problem25::getString() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - - std::stringstream results; - results << "The first Fibonacci number with " << NUM_DIGITS << " digits is " << number - << "\nIts index is " << index; - return results.str(); -} - //Returns the Fibonacci number asked for mpz_class Problem25::getNumber() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem26.cpp b/Source/Problem26.cpp index 04292eb..03c647c 100644 --- a/Source/Problem26.cpp +++ b/Source/Problem26.cpp @@ -89,6 +89,10 @@ 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; } @@ -100,20 +104,6 @@ void Problem26::reset(){ longestNumber = 1; } -//Return a string with the solution to the problem -std::string Problem26::getString() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - - std::stringstream results; - results << "The longest cycle is " << longestCycle << " digits long\n" - << "It is started with the number " << longestNumber; - - return results.str(); -} - //Returns the length of the longest cycle unsigned int Problem26::getLongestCycle() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem27.cpp b/Source/Problem27.cpp index 167d907..c05db9f 100644 --- a/Source/Problem27.cpp +++ b/Source/Problem27.cpp @@ -70,6 +70,11 @@ 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; } @@ -81,21 +86,6 @@ void Problem27::reset(){ primes.clear(); } -//Return a string with the solution to the problem -std::string Problem27::getString() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - - std::stringstream results; - results << "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 results.str(); -} - //Returns the top A that was generated int64_t Problem27::getTopA() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem28.cpp b/Source/Problem28.cpp index 0c7c7ae..e4f2289 100644 --- a/Source/Problem28.cpp +++ b/Source/Problem28.cpp @@ -132,6 +132,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; } @@ -144,19 +147,6 @@ void Problem28::reset(){ setupGrid(); } -//Return a string with the solution to the problem -std::string Problem28::getString() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - - std::stringstream results; - results << "The sum of the diagonals in the given grid is " << sumOfDiagonals; - - return results.str(); -} - //Returns the grid std::vector> Problem28::getGrid() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem29.cpp b/Source/Problem29.cpp index bb00061..cead5f1 100644 --- a/Source/Problem29.cpp +++ b/Source/Problem29.cpp @@ -73,6 +73,9 @@ 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; } @@ -83,19 +86,6 @@ void Problem29::reset(){ unique.clear(); } -//Return a string with the solution to the problem -std::string Problem29::getString() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - - std::stringstream results; - results << "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 results.str(); -} - //Returns the lowest possible value for a unsigned int Problem29::getBottomA() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem3.cpp b/Source/Problem3.cpp index 77da2cf..c4ec9ba 100644 --- a/Source/Problem3.cpp +++ b/Source/Problem3.cpp @@ -54,6 +54,9 @@ 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; } @@ -64,17 +67,6 @@ void Problem3::reset(){ factors.clear(); } -//Return a string with the solution to the problem -std::string Problem3::getString() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - std::stringstream results; - results << "The largest factor of the number " << GOAL_NUMBER << " is " << factors[factors.size() - 1]; - return results.str(); -} - //Returns the list of factors of the number std::vector Problem3::getFactors() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem30.cpp b/Source/Problem30.cpp index ecd31e6..b44f22e 100644 --- a/Source/Problem30.cpp +++ b/Source/Problem30.cpp @@ -85,6 +85,9 @@ 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 @@ -93,19 +96,6 @@ void Problem30::reset(){ sumOfFifthNumbers.clear(); } -//Return a string with the solution to the problem -std::string Problem30::getString() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - - std::stringstream results; - results << "The sum of all the numbers that can be written as the sum of the fifth powers of their digits is " << getSumOfList(); - - return results.str(); -} - //This returns the top number to be checked uint64_t Problem30::getTopNum() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem31.cpp b/Source/Problem31.cpp index b7448ff..2cb9ece 100644 --- a/Source/Problem31.cpp +++ b/Source/Problem31.cpp @@ -67,6 +67,9 @@ 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; } @@ -77,19 +80,6 @@ void Problem31::reset(){ permutations = 0; } -//Return a string with the solution to the problem -std::string Problem31::getString() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - - std::stringstream results; - results << "There are " << permutations << " ways to make 2 pounds with the given denominations of coins"; - - return results.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/Problem4.cpp b/Source/Problem4.cpp index 8a1e68f..bcfddfc 100644 --- a/Source/Problem4.cpp +++ b/Source/Problem4.cpp @@ -75,6 +75,9 @@ 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; } @@ -85,18 +88,6 @@ void Problem4::reset(){ palindromes.clear(); } -//Return a string with the solution to the problem -std::string Problem4::getString() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - //Print the results - std::stringstream results; - results << "The largest palindrome is " << *(palindromes.end() - 1); - return results.str(); -} - //Returns the list of all palindromes std::vector Problem4::getPalindromes() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem5.cpp b/Source/Problem5.cpp index e5f8d80..8424ec4 100644 --- a/Source/Problem5.cpp +++ b/Source/Problem5.cpp @@ -72,6 +72,9 @@ 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; } @@ -82,17 +85,6 @@ void Problem5::reset(){ smallestNum = 0; } -//Return a string with the solution to the problem -std::string Problem5::getString() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - std::stringstream results; - results << "The smallest positive number evenly divisible by all numbers 1-20 is " << smallestNum; - return results.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/Source/Problem6.cpp index 01a7d10..e5959e3 100644 --- a/Source/Problem6.cpp +++ b/Source/Problem6.cpp @@ -58,6 +58,9 @@ 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; } @@ -68,17 +71,6 @@ void Problem6::reset(){ sumOfSquares = squareOfSum = 0; } -//Return a string with the solution to the problem -std::string Problem6::getString() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - std::stringstream results; - results << "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 results.str(); -} - //Returns the sum of all the squares uint64_t Problem6::getSumOfSquares() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem67.cpp b/Source/Problem67.cpp index 2da92b8..9253ef4 100644 --- a/Source/Problem67.cpp +++ b/Source/Problem67.cpp @@ -311,6 +311,9 @@ void Problem67::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; } @@ -323,18 +326,6 @@ void Problem67::reset(){ actualTotal = 0; } -//Return a string with the solution to the problem -std::string Problem67::getString() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - - std::stringstream results; - results << "The value of the longest path is " << actualTotal; - return results.str(); -} - //Returns the pyramid that was traversed as a string std::string Problem67::getPyramid() const{ //If the problem hasn't been solved throw an exception diff --git a/Source/Problem7.cpp b/Source/Problem7.cpp index 1b3afde..af6f567 100644 --- a/Source/Problem7.cpp +++ b/Source/Problem7.cpp @@ -54,6 +54,9 @@ 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; } @@ -63,17 +66,6 @@ void Problem7::reset(){ Problem::reset(); } -//Return a string with the solution to the problem -std::string Problem7::getString() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - std::stringstream results; - results << "The " << NUMBER_OF_PRIMES << "th prime number is " << primes.at(primes.size() - 1); - return results.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/Source/Problem8.cpp index 68a2db0..91b2f51 100644 --- a/Source/Problem8.cpp +++ b/Source/Problem8.cpp @@ -84,6 +84,10 @@ 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; } @@ -95,18 +99,6 @@ void Problem8::reset(){ maxProduct = 0; } -//Return a string with the solution to the problem -std::string Problem8::getString() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - std::stringstream results; - results << "The greatest product is " << maxProduct - << "\nThe numbers are " << maxNums; - return results.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 diff --git a/Source/Problem9.cpp b/Source/Problem9.cpp index a3a8ec9..736103b 100644 --- a/Source/Problem9.cpp +++ b/Source/Problem9.cpp @@ -66,6 +66,15 @@ void Problem9::solve(){ //Stop the timer timer.stop(); + //Save the results + if(found){ + result << "The Pythagorean triplet is " << a << ' ' << b << ' ' << (int)c + << "\nThe numbers' product is " << a * b * (int)c; + } + else{ + result << "The number was not found!"; + } + //Throw a flag to show the problem is solved solved = true; } @@ -79,24 +88,6 @@ void Problem9::reset(){ found = false; } -//Return a string with the solution to the problem -std::string Problem9::getString() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - std::stringstream results; - if(found){ - results << "The Pythagorean triplet is " << a << ' ' << b << ' ' << (int)c - << "\nThe numbers' product is " << a * b * (int)c; - } - else{ - results << "The number was not found!"; - } - - return results.str(); -} - //Returns the length of the first side int Problem9::getSideA() const{ //If the problem hasn't been solved throw an exception diff --git a/benchmark.hpp b/benchmark.hpp index cf1b274..8f0ae36 100644 --- a/benchmark.hpp +++ b/benchmark.hpp @@ -42,7 +42,7 @@ std::string getBenchmarkResults(Problem* problem, double totalTime, unsigned int //Variables //Valid menu options enum BENCHMARK_OPTIONS {RUN_SPECIFIC = 1, RUN_ALL_SHORT, RUN_ALL, BENCHMARK_EXIT, BENCHMARK_SIZE}; -std::vector tooLong = {15}; //The list of problems that take "too long" to run. (Over 1 second on my machine) +std::vector tooLong = {5, 15, 23, 24, 27, 29}; //The list of problems that take "too long" to run. (Over 1 second on my machine) //The driver function for the benchmark selection @@ -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->getString(); + results << "\n\n" << problem->getResults(); results << "\nIt took an average of " << timeResults << " to run this problem over " << timesRun << " iterations\n\n" << std::endl; return results.str(); }