Added a benchmark tool

This commit is contained in:
2020-07-08 20:38:37 -04:00
parent a7d960c3b1
commit 766af92f1a
68 changed files with 546 additions and 134 deletions

View File

@@ -34,7 +34,6 @@
uint64_t Problem1::MAX_NUMBER = 1000;
Problem1::Problem1() : Problem("What is the sum of all the multiples of 3 or 5 that are less than 1000?"), fullSum(0){
}
void Problem1::solve(){
@@ -84,3 +83,9 @@ uint64_t Problem1::getSum() const{
}
return fullSum;
}
void Problem1::reset(){
Problem::reset();
fullSum = 0;
numbers.clear();
}

View File

@@ -34,7 +34,6 @@
uint64_t Problem10::GOAL_NUMBER = 2000000;
Problem10::Problem10() : Problem("Find the sum of all the primes below two million"), sum(0){
}
void Problem10::solve(){
@@ -72,3 +71,8 @@ uint64_t Problem10::getSum() const{
}
return sum;
}
void Problem10::reset(){
Problem::reset();
sum = 0;
}

View File

@@ -75,7 +75,6 @@ std::vector<int> Problem11::grid[20] = {{ 8, 02, 22, 97, 38, 15, 00, 40, 00, 75,
{01, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 01, 89, 19, 67, 48}};
Problem11::Problem11() : Problem("What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20x20 grid?"){
}
void Problem11::solve(){
@@ -202,3 +201,8 @@ int Problem11::getProduct() const{
}
return mee::getProduct(greatestProduct);
}
void Problem11::reset(){
Problem::reset();
greatestProduct.clear();
}

View File

@@ -33,7 +33,6 @@
uint64_t Problem12::GOAL_DIVISORS = 500;
Problem12::Problem12() : Problem("What is the value of the first triangle number to have over five hundred divisors?"), sum(1), counter(2){
}
void Problem12::solve(){
@@ -109,3 +108,10 @@ size_t Problem12::getNumberOfDivisors() const{
}
return divisors.size();
}
void Problem12::reset(){
Problem::reset();
divisors.clear();
sum = 1;
counter = 2;
}

View File

@@ -138,9 +138,11 @@
#include "../Headers/Problem13.hpp"
std::vector<mpz_class> Problem13::nums;
Problem13::Problem13() : Problem("Work out the first ten digits of the sum of the one-hundred 50-digit numbers"), sum(0){
reserveVectors();
}
void Problem13::reserveVectors(){
//Make sure the vector is the correct size
nums.reserve(100);
nums.resize(100);
@@ -299,3 +301,10 @@ mpz_class Problem13::getSum() const{
}
return sum;
}
void Problem13::reset(){
Problem::reset();
sum = 0;
nums.clear();
reserveVectors();
}

View File

@@ -54,7 +54,6 @@ uint64_t Problem14::checkSeries(uint64_t num){
}
Problem14::Problem14() : Problem("Which starting number, under one million, produces the longest chain using the itterative sequence?"), maxLength(0), maxNum(0){
}
void Problem14::solve(){
@@ -109,3 +108,9 @@ uint64_t Problem14::getStartingNumber() const{
}
return maxNum;
}
void Problem14::reset(){
Problem::reset();
maxLength = 0;
maxNum = 0;
}

View File

@@ -52,7 +52,6 @@ void Problem15::move(int currentX, int currentY, uint64_t& numOfRoutes){
}
Problem15::Problem15() : Problem("How many routes from the top left corner to the bottom right corner are there through a 20×20 grid if you can only move right and down?"), numOfRoutes(0){
}
void Problem15::solve(){
@@ -97,3 +96,8 @@ uint64_t Problem15::getNumberOfRoutes() const{
}
return numOfRoutes;
}
void Problem15::reset(){
Problem::reset();
numOfRoutes = 0;
}

View File

@@ -36,7 +36,6 @@ int Problem16::NUM_TO_POWER = 2; //The number that is going to be raised to a po
int Problem16::POWER = 1000; //The power that the number is going to be raised to
Problem16::Problem16() : Problem("What is the sum of the digits of the number 2^1000?"), num(0), sumOfElements(0){
}
void Problem16::solve(){
@@ -95,3 +94,9 @@ int Problem16::getSum() const{
}
return sumOfElements;
}
void Problem16::reset(){
Problem::reset();
num = 0;
sumOfElements = 0;
}

View File

@@ -33,7 +33,6 @@
int Problem17::TOP_NUM = 1000;
Problem17::Problem17() : Problem("If all the numbers from 1 to 1000 inclusive were written out in words, how many letters would be used?"), letterCount(0){
}
std::string Problem17::makeWord(int num){
@@ -210,3 +209,8 @@ uint64_t Problem17::getLetterCount() const{
}
return letterCount;
}
void Problem17::reset(){
Problem::reset();
letterCount = 0;
}

View File

@@ -66,14 +66,6 @@ std::vector<int> Problem18::list[NUM_ROWS] =
{04, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 04, 23}};
Problem18::Problem18() : Problem("Find the maximum total from the top to the bottom of the pyramid."), actualTotal(0){
//The method that I am using looks for the smallest numbers, so I need to invert the numbers in this list
invert();
//Now l[i][j] == 100 - l[i][j];
//Add the top point because that is already the only path
foundPoints.emplace_back(0, 0, list[0][0], false);
//Add the second row as possible points
possiblePoints.emplace_back(0, 1, (list[0][0] + list[1][0]), true);
possiblePoints.emplace_back(1, 1, (list[0][0] + list[1][1]), false);
}
void Problem18::invert(){
@@ -93,6 +85,15 @@ void Problem18::solve(){
//Start the timer
timer.start();
//The method that I am using looks for the smallest numbers, so I need to invert the numbers in this list
invert();
//Now l[i][j] == 100 - l[i][j];
//Add the top point because that is already the only path
foundPoints.emplace_back(0, 0, list[0][0], false);
//Add the second row as possible points
possiblePoints.emplace_back(0, 1, (list[0][0] + list[1][0]), true);
possiblePoints.emplace_back(1, 1, (list[0][0] + list[1][1]), false);
bool foundBottom = false; //Used when you find a point at the bottom
//Loop until you find the bottom
@@ -224,3 +225,10 @@ int Problem18::getTotal() const{
}
return actualTotal;
}
void Problem18::reset(){
Problem::reset();
foundPoints.clear();
possiblePoints.clear();
actualTotal = 0;
}

View File

@@ -44,7 +44,6 @@ unsigned int Problem19::START_YEAR = 1901; //The start year
unsigned int Problem19::END_YEAR = 2000; //The stop year
Problem19::Problem19() : Problem("How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?"), totalSundays(0){
}
Problem19::DAYS Problem19::getDay(unsigned int month, unsigned int day, unsigned int year){
@@ -189,3 +188,8 @@ uint64_t Problem19::getTotalSundays() const{
}
return totalSundays;
}
void Problem19::reset(){
Problem::reset();
totalSundays = 0;
}

View File

@@ -34,7 +34,6 @@
uint64_t Problem2::TOP_NUM = 4000000;
Problem2::Problem2() : Problem("What is the sum of the even Fibonacci numbers less than 4,000,000?"), fullSum(0){
}
void Problem2::solve(){
@@ -80,3 +79,8 @@ uint64_t Problem2::getSum() const{
}
return fullSum;
}
void Problem2::reset(){
Problem::reset();
fullSum = 0;
}

View File

@@ -101,3 +101,9 @@ uint64_t Problem20::getSum() const{
}
return sum;
}
void Problem20::reset(){
Problem::reset();
sum = 0;
num = 1;
}

View File

@@ -34,6 +34,10 @@
int Problem21::LIMIT = 10000; //The top number that will be evaluated
Problem21::Problem21() : Problem("Evaluate the sum of all the amicable numbers under 10000"){
reserveVectors();
}
void Problem21::reserveVectors(){
divisorSum.reserve(LIMIT); //Reserving it now makes it faster later
divisorSum.resize(LIMIT); //Make sure there are enough spaces
}
@@ -113,3 +117,10 @@ uint64_t Problem21::getSum() const{
}
return mee::getSum(amicable);
}
void Problem21::reset(){
Problem::reset();
divisorSum.clear();
amicable.clear();
reserveVectors();
}

View File

@@ -402,6 +402,10 @@ std::vector<std::string> Problem22::names = { "MARY","PATRICIA","LINDA","BARBARA
"HAI","ELDEN","DORSEY","DARELL","BRODERICK","ALONSO"};
Problem22::Problem22() : Problem("What is the total of all the name scores in the file?"){
reserveVectors();
}
void Problem22::reserveVectors(){
//Make sure the vector is the right size
sums.reserve(names.size());
sums.resize(names.size());
@@ -468,3 +472,10 @@ uint64_t Problem22::getNameScoreSum() const{
}
return mee::getSum(prod);
}
void Problem22::reset(){
Problem::reset();
sums.clear();
prod.clear();
reserveVectors();
}

View File

@@ -35,6 +35,10 @@
int Problem23::MAX_NUM = 28123;
Problem23::Problem23() : Problem("Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers"), sum(0){
reserveVectors();
}
void Problem23::reserveVectors(){
//This makes sure the vector is the correct size
divisorSums.reserve(MAX_NUM);
divisorSums.resize(MAX_NUM);
@@ -116,3 +120,10 @@ uint64_t Problem23::getSum() const{
}
return sum;
}
void Problem23::reset(){
Problem::reset();
sum = 0;
divisorSums.clear();
reserveVectors();
}

View File

@@ -84,3 +84,8 @@ std::string Problem24::getPermutation() const{
}
return permutations.at(NEEDED_PERM - 1);
}
void Problem24::reset(){
Problem::reset();
permutations.clear();
}

View File

@@ -36,7 +36,8 @@
unsigned int Problem25::NUM_DIGITS = 1000; //The number of digits to calculate up to
Problem25::Problem25() : Problem("What is the index of the first term in the Fibonacci sequence to contain 1000 digits?"), number(0), index(2){
number = 0;
index = 0;
}
void Problem25::solve(){
@@ -117,3 +118,9 @@ uint64_t Problem25::getIndexInt() const{
}
return index.get_ui();
}
void Problem25::reset(){
Problem::reset();
number = 0;
index = 2;
}

View File

@@ -32,7 +32,8 @@
unsigned int Problem26::TOP_NUMBER = 999;
Problem26::Problem26() : Problem("Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part."), longestCycle(0), longestNumber(1){
longestCycle = 0;
longestNumber = 0;
}
void Problem26::solve(){
@@ -117,3 +118,9 @@ unsigned int Problem26::getLongestNumber() const{
}
return longestNumber;
}
void Problem26::reset(){
Problem::reset();
longestCycle = 0;
longestNumber = 1;
}

View File

@@ -30,7 +30,6 @@
Problem27::Problem27() : Problem("Considering quadratics of the form n^2 + an + b, where |a| < 1000 and |b| <= 1000, find the product of the coefficients a and b that produce the maximum number of primes for consecutive values of n starting with n = 0."){
topA = topB = topN = 0;
primes = mee::getPrimes((int64_t)(12000));
}
void Problem27::solve(){
@@ -42,6 +41,8 @@ void Problem27::solve(){
//Start the timer
timer.start();
primes = mee::getPrimes((int64_t)(12000));
//Start with the lowest possible A and check all possibilities after that
for(int64_t a = -999;a <= 999;++a){
//Start with the lowest possible B and check all possibilities after that
@@ -108,3 +109,9 @@ int64_t Problem27::getTopN() const{
}
return topN;
}
void Problem27::reset(){
Problem::reset();
topA = topB = topN = 0;
primes.clear();
}

View File

@@ -30,6 +30,10 @@
Problem28::Problem28() : Problem("What is the sum of the number on the diagonals in a 1001 x 1001 spiral formed by starting with the number 1 and moving to the right in a clockwise direction?"){
setupGrid();
sumOfDiagonals = 0;
}
void Problem28::setupGrid(){
//Set the size of the grid to 1001 x 1001
for(int cnt = 0;cnt < 1001;++cnt){
grid.emplace_back();
@@ -37,11 +41,10 @@ Problem28::Problem28() : Problem("What is the sum of the number on the diagonals
grid.at(cnt).push_back(0);
}
}
sumOfDiagonals = 0;
}
//Sets up the grid
void Problem28::setupGrid(){
void Problem28::createGrid(){
bool finalLocation = false; //A flag to indicate if the final location to be filled has been reached
//Set the number that is going to be put at each location
int currentNum = 1;
@@ -117,7 +120,7 @@ void Problem28::solve(){
//Setup the grid
setupGrid();
createGrid();
//Find the sum of the diagonals in the grid
findSum();
@@ -158,3 +161,10 @@ uint64_t Problem28::getSum() const{
}
return sumOfDiagonals;
}
void Problem28::reset(){
Problem::reset();
sumOfDiagonals = 0;
grid.clear();
setupGrid();
}

View File

@@ -35,7 +35,6 @@
Problem29::Problem29() : Problem("How many distict terms are in the sequence generated by a^b for 2 <= a <= 100 and 2 <= b <= 100?"){
}
void Problem29::solve(){
@@ -118,3 +117,8 @@ std::vector<mpz_class> Problem29::getUnique() const{
}
return unique;
}
void Problem29::reset(){
Problem::reset();
unique.clear();
}

View File

@@ -34,8 +34,7 @@
uint64_t Problem3::GOAL_NUMBER = 600851475143;
Problem3::Problem3() : Problem("What is the largest prime factor of 600851475143?"){
Problem3::Problem3() : Problem("What is the largest prime factor of 600851475143?"){
}
void Problem3::solve(){
@@ -90,3 +89,8 @@ uint64_t Problem3::getGoalNumber() const{
}
return GOAL_NUMBER;
}
void Problem3::reset(){
Problem::reset();
factors.clear();
}

View File

@@ -31,7 +31,6 @@
Problem30::Problem30() : Problem("Find the sum of all the numbers that can be written as a sum of the fifth powers of their digits"){
}
//Returns a vector with the indivitual digits of the number passed into it
@@ -125,3 +124,8 @@ uint64_t Problem30::getSumOfList() const{
return sum;
}
void Problem30::reset(){
Problem::reset();
sumOfFifthNumbers.clear();
}

View File

@@ -84,3 +84,8 @@ std::string Problem31::getString() const{
int Problem31::getPermutations() const{
return permutations;
}
void Problem31::reset(){
Problem::reset();
permutations = 0;
}

View File

@@ -35,7 +35,6 @@ int Problem4::START_NUM = 100;
int Problem4::END_NUM = 999;
Problem4::Problem4() : Problem("Find the largest palindrome made from the product of two 3-digit numbers."){
}
void Problem4::solve(){
@@ -102,3 +101,8 @@ uint64_t Problem4::getLargestPalindrome() const{
}
return *(palindromes.end() - 1);
}
void Problem4::reset(){
Problem::reset();
palindromes.clear();
}

View File

@@ -32,7 +32,6 @@
Problem5::Problem5() : Problem("What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?"), smallestNum(0){
}
void Problem5::solve(){
@@ -92,3 +91,8 @@ int Problem5::getNumber() const{
}
return smallestNum;
}
void Problem5::reset(){
Problem::reset();
smallestNum = 0;
}

View File

@@ -35,7 +35,6 @@ int Problem6::START_NUM = 1;
int Problem6::END_NUM = 100;
Problem6::Problem6() : Problem("Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum."), sumOfSquares(0), squareOfSum(0){
}
void Problem6::solve(){
@@ -94,3 +93,8 @@ uint64_t Problem6::getDifference() const{
}
return abs(sumOfSquares - squareOfSum);
}
void Problem6::reset(){
Problem::reset();
sumOfSquares = squareOfSum = 0;
}

View File

@@ -237,15 +237,6 @@ std::vector<int> Problem67::list[NUM_ROWS] = {
};
Problem67::Problem67() : Problem("Find the maximum total from the top to the bottom of the pyramid."){
//The method that I am using looks for the smallest numbers, so I need to invert the numbers in this list
invert();
//Now l[i][j] == 100 - l[i][j];
//Add the top point because that is already the only path
foundPoints.emplace_back(0, 0, list[0][0], false);
//Add the second row as possible points
possiblePoints.emplace_back(0, 1, (list[0][0] + list[1][0]), true);
possiblePoints.emplace_back(1, 1, list[0][0] + list[1][1], false);
}
void Problem67::invert(){
@@ -265,6 +256,16 @@ void Problem67::solve(){
//Start the timer
timer.start();
//The method that I am using looks for the smallest numbers, so I need to invert the numbers in this list
invert();
//Now l[i][j] == 100 - l[i][j];
//Add the top point because that is already the only path
foundPoints.emplace_back(0, 0, list[0][0], false);
//Add the second row as possible points
possiblePoints.emplace_back(0, 1, (list[0][0] + list[1][0]), true);
possiblePoints.emplace_back(1, 1, list[0][0] + list[1][1], false);
bool foundBottom = false; //Used when you find a point at the bottom
//Loop until you find the bottom
@@ -402,4 +403,12 @@ int Problem67::getTotal() const{
throw unsolved();
}
return actualTotal;
}
}
//Clears all of the variables so the problem can be run again
void Problem67::reset(){
Problem::reset();
foundPoints.clear();
possiblePoints.clear();
actualTotal = 0;
}

View File

@@ -35,7 +35,6 @@
uint64_t Problem7::NUMBER_OF_PRIMES = 10001;
Problem7::Problem7() : Problem("What is the 10001th prime number?"){
}
void Problem7::solve(){
@@ -73,3 +72,7 @@ uint64_t Problem7::getPrime() const{
}
return *primes.end();
}
void Problem7::reset(){
Problem::reset();
}

View File

@@ -56,7 +56,6 @@
std::string Problem8::number = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
Problem8::Problem8() : Problem("Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?"), maxProduct(0){
}
void Problem8::solve(){
@@ -112,3 +111,9 @@ uint64_t Problem8::getLargestProduct() const{
}
return maxProduct;
}
void Problem8::reset(){
Problem::reset();
maxNums = "";
maxProduct = 0;
}

View File

@@ -31,7 +31,6 @@
Problem9::Problem9() : Problem("There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product of abc."), a(1), b(0), c(0), found(false){
}
void Problem9::solve(){
@@ -117,3 +116,11 @@ int Problem9::getProduct() const{
}
return a * b * (int)c;
}
void Problem9::reset(){
Problem::reset();
a = 1;
b = 0;
c = 0;
found = false;
}