mirror of
https://bitbucket.org/Mattrixwv/projecteulercpp.git
synced 2025-12-06 17:13:59 -05:00
Updated how results are retrieved
This commit is contained in:
@@ -24,6 +24,7 @@
|
||||
#ifndef PROBLEM_HPP
|
||||
#define PROBLEM_HPP
|
||||
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#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
|
||||
|
||||
@@ -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
|
||||
};
|
||||
|
||||
|
||||
@@ -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
|
||||
};
|
||||
|
||||
|
||||
@@ -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<int> getNumbers() const; //Returns the numbers that were being searched
|
||||
int getProduct() const; //Returns the product that was requested
|
||||
};
|
||||
|
||||
@@ -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<int64_t> getDivisorsOfTriangularNumber() const; //Returns the list of divisors of the requested number
|
||||
|
||||
@@ -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<mpz_class> getNumbers() const; //Returns the list 50-digit numbers
|
||||
mpz_class getSum() const; //Returns the sum of the 50-digit numbers
|
||||
};
|
||||
|
||||
@@ -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
|
||||
};
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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
|
||||
};
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
};
|
||||
|
||||
|
||||
@@ -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!
|
||||
|
||||
@@ -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<uint64_t> 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
|
||||
};
|
||||
|
||||
@@ -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<std::string> getNames() const; //Returns the vector of the names being scored
|
||||
uint64_t getNameScoreSum() const; //Returns the sum of the names scores
|
||||
};
|
||||
|
||||
@@ -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
|
||||
};
|
||||
|
||||
|
||||
@@ -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<std::string> getPermutationsList() const; //Returns a vector with all of the permutations
|
||||
std::string getPermutation() const; //Returns the specific permutations you are looking for
|
||||
};
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
};
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<std::vector<int>> getGrid() const; //Returns the grid
|
||||
uint64_t getSum() const; //Returns the sum of the diagonals
|
||||
};
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<uint64_t> 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
|
||||
|
||||
@@ -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<uint64_t> 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
|
||||
|
||||
@@ -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
|
||||
};
|
||||
|
||||
|
||||
@@ -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<uint64_t> getPalindromes() const; //Returns the list of all palindromes
|
||||
uint64_t getLargestPalindrome() const; //Returns the largest palindrome
|
||||
};
|
||||
|
||||
@@ -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
|
||||
};
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
};
|
||||
|
||||
|
||||
@@ -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
|
||||
};
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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){
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<int> Problem11::getNumbers() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<mpz_class> Problem13::getNumbers() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<uint64_t> Problem21::getAmicable() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
|
||||
@@ -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<std::string> Problem22::getNames() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<std::string> Problem24::getPermutationsList() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<std::vector<int>> Problem28::getGrid() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<uint64_t> Problem3::getFactors() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<uint64_t> Problem4::getPalindromes() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<unsigned int> tooLong = {15}; //The list of problems that take "too long" to run. (Over 1 second on my machine)
|
||||
std::vector<unsigned int> 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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user