mirror of
https://bitbucket.org/Mattrixwv/projecteulercpp.git
synced 2025-12-07 01:23:57 -05:00
Moved files around to better match C++ norms
Changed results to match other languages
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/ProjectEulerCPP/Headers/Problem.hpp
|
||||
//ProjectEuler/ProjectEulerCPP/headers/Problem.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 07-04-19
|
||||
//Modified: 07-09-20
|
||||
//Modified: 08-28-20
|
||||
//This is an abstract base class to allow polymorphism for the individual problems
|
||||
/*
|
||||
Copyright (C) 2020 Matthew Ellison
|
||||
@@ -35,7 +35,6 @@ protected:
|
||||
mee::Stopwatch timer; //Used to determine your algorithm's run time
|
||||
bool solved; //Holds true after the problem has been solved
|
||||
class Unsolved{}; //An exception class thrown if you try to access something before it has been solved
|
||||
std::stringstream result; //Get the result of the problem
|
||||
public:
|
||||
//Constructors
|
||||
Problem() : solved(false){
|
||||
@@ -67,20 +66,14 @@ public:
|
||||
}
|
||||
//Reset the problem so it can be run again
|
||||
virtual void reset(){
|
||||
result.str(std::string());
|
||||
timer.reset();
|
||||
solved = false;
|
||||
}
|
||||
//Return a string with the solution to the problem
|
||||
virtual std::string getResults(){
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
throw Unsolved();
|
||||
}
|
||||
return result.str();
|
||||
}
|
||||
//Pure virtual functions
|
||||
virtual void solve() = 0; //Solve the problem
|
||||
//Solve the problem
|
||||
virtual void solve() = 0;
|
||||
//Return a string with the solution to the problem
|
||||
virtual std::string getResult() = 0;
|
||||
};
|
||||
|
||||
#endif //PROBLEM_HPP
|
||||
|
||||
149
Headers/ProblemSelection.hpp
Normal file
149
Headers/ProblemSelection.hpp
Normal file
@@ -0,0 +1,149 @@
|
||||
//ProjectEuler/ProjectEulerCPP/headers/ProblemSelection.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 07-08-20
|
||||
//Modified: 08-28-20
|
||||
//This is a header file with a few functions to help select and run problems
|
||||
/*
|
||||
Copyright (C) 2020 Matthew Ellison
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef PROBLEMSELECTION_HPP
|
||||
#define PROBLEMSELECTION_HPP
|
||||
|
||||
#include <iostream>
|
||||
#include "Algorithms.hpp"
|
||||
#include "Problems/Problem1.hpp"
|
||||
#include "Problems/Problem2.hpp"
|
||||
#include "Problems/Problem3.hpp"
|
||||
#include "Problems/Problem4.hpp"
|
||||
#include "Problems/Problem5.hpp"
|
||||
#include "Problems/Problem6.hpp"
|
||||
#include "Problems/Problem7.hpp"
|
||||
#include "Problems/Problem8.hpp"
|
||||
#include "Problems/Problem9.hpp"
|
||||
#include "Problems/Problem10.hpp"
|
||||
#include "Problems/Problem11.hpp"
|
||||
#include "Problems/Problem12.hpp"
|
||||
#include "Problems/Problem13.hpp"
|
||||
#include "Problems/Problem14.hpp"
|
||||
#include "Problems/Problem15.hpp"
|
||||
#include "Problems/Problem16.hpp"
|
||||
#include "Problems/Problem17.hpp"
|
||||
#include "Problems/Problem18.hpp"
|
||||
#include "Problems/Problem19.hpp"
|
||||
#include "Problems/Problem20.hpp"
|
||||
#include "Problems/Problem21.hpp"
|
||||
#include "Problems/Problem22.hpp"
|
||||
#include "Problems/Problem23.hpp"
|
||||
#include "Problems/Problem24.hpp"
|
||||
#include "Problems/Problem25.hpp"
|
||||
#include "Problems/Problem26.hpp"
|
||||
#include "Problems/Problem27.hpp"
|
||||
#include "Problems/Problem28.hpp"
|
||||
#include "Problems/Problem29.hpp"
|
||||
#include "Problems/Problem30.hpp"
|
||||
#include "Problems/Problem31.hpp"
|
||||
#include "Problems/Problem32.hpp"
|
||||
#include "Problems/Problem67.hpp"
|
||||
|
||||
|
||||
//Setup the problem numbers
|
||||
std::vector<unsigned int> PROBLEM_NUMBERS = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
|
||||
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
|
||||
21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
|
||||
31, 32, 67};
|
||||
|
||||
//This function returns a pointer to a problem of type number
|
||||
Problem* getProblem(unsigned int problemNumber){
|
||||
Problem* problem = nullptr; //Holds the problem we are about to create
|
||||
|
||||
//Decide which problem was asked for and create it
|
||||
switch(problemNumber){
|
||||
case 1 : problem = new Problem1; break;
|
||||
case 2 : problem = new Problem2; break;
|
||||
case 3 : problem = new Problem3; break;
|
||||
case 4 : problem = new Problem4; break;
|
||||
case 5 : problem = new Problem5; break;
|
||||
case 6 : problem = new Problem6; break;
|
||||
case 7 : problem = new Problem7; break;
|
||||
case 8 : problem = new Problem8; break;
|
||||
case 9 : problem = new Problem9; break;
|
||||
case 10 : problem = new Problem10; break;
|
||||
case 11 : problem = new Problem11; break;
|
||||
case 12 : problem = new Problem12; break;
|
||||
case 13 : problem = new Problem13; break;
|
||||
case 14 : problem = new Problem14; break;
|
||||
case 15 : problem = new Problem15; break;
|
||||
case 16 : problem = new Problem16; break;
|
||||
case 17 : problem = new Problem17; break;
|
||||
case 18 : problem = new Problem18; break;
|
||||
case 19 : problem = new Problem19; break;
|
||||
case 20 : problem = new Problem20; break;
|
||||
case 21 : problem = new Problem21; break;
|
||||
case 22 : problem = new Problem22; break;
|
||||
case 23 : problem = new Problem23; break;
|
||||
case 24 : problem = new Problem24; break;
|
||||
case 25 : problem = new Problem25; break;
|
||||
case 26 : problem = new Problem26; break;
|
||||
case 27 : problem = new Problem27; break;
|
||||
case 28 : problem = new Problem28; break;
|
||||
case 29 : problem = new Problem29; break;
|
||||
case 30 : problem = new Problem30; break;
|
||||
case 31 : problem = new Problem31; break;
|
||||
case 32 : problem = new Problem32; break;
|
||||
case 67 : problem = new Problem67; break;
|
||||
}
|
||||
|
||||
//Return the newly created problem
|
||||
return problem;
|
||||
}
|
||||
|
||||
void printDescription(Problem* problem){
|
||||
std::cout << problem->getDescription() << '\n';
|
||||
}
|
||||
|
||||
void solveProblem(Problem* problem){
|
||||
//Print the problem description
|
||||
printDescription(problem);
|
||||
//Solve the problem
|
||||
problem->solve();
|
||||
//Print the results
|
||||
std::cout << problem->getResult()
|
||||
<< "\nIt took " << problem->getTime() << " to solve this problem.\n\n" << std::endl;
|
||||
}
|
||||
|
||||
unsigned int getProblemNumber(){
|
||||
unsigned int problemNumber = 0;
|
||||
std::cout << "Enter a problem number: ";
|
||||
std::cin >> problemNumber;
|
||||
while(!mee::isFound(PROBLEM_NUMBERS, problemNumber) || std::cin.fail()){
|
||||
std::cout << "That is an invalid problem number!\nEnter a problem number: ";
|
||||
std::cin.clear();
|
||||
std::cin >> problemNumber;
|
||||
}
|
||||
return problemNumber;
|
||||
}
|
||||
|
||||
void listProblems(){
|
||||
std::cout << PROBLEM_NUMBERS[1];
|
||||
for(unsigned int problemNumber = 2;problemNumber < PROBLEM_NUMBERS.size();++problemNumber){
|
||||
std::cout << ", " << PROBLEM_NUMBERS[problemNumber];
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
#endif //PROBLEMSELECTION_HPP
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/ProjectEulerCPP/Headers/Problem1.hpp
|
||||
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem1.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-28-18
|
||||
//Modified: 07-09-20
|
||||
//Modified: 08-28-20
|
||||
//What is the sum of all the multiples of 3 or 5 that are less than 1000
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
|
||||
/*
|
||||
@@ -45,6 +45,7 @@ public:
|
||||
virtual void solve(); //Solve the problem
|
||||
virtual void reset(); //Reset the problem so it can be run again
|
||||
//Gets
|
||||
virtual std::string getResult(); //Return a string with the solution to the problem
|
||||
uint64_t getSum() const; //Returns the requested sum
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/ProjectEulerCPP/Headers/Problem10.hpp
|
||||
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem10.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-28-18
|
||||
//Modified: 07-09-20
|
||||
//Modified: 08-28-20
|
||||
//Find the sum of all the primes below two million
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
|
||||
/*
|
||||
@@ -44,6 +44,7 @@ public:
|
||||
virtual void solve(); //Solve the problem
|
||||
virtual void reset(); //Reset the problem so it can be run again
|
||||
//Gets
|
||||
virtual std::string getResult(); //Return a string with the solution to the problem
|
||||
uint64_t getSum() const; //Returns the sum that was requested
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/ProjectEulerCPP/Headers/Problem11.hpp
|
||||
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem11.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-29-18
|
||||
//Modified: 07-09-20
|
||||
//Modified: 08-28-20
|
||||
//What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid?
|
||||
/*
|
||||
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
|
||||
@@ -66,6 +66,7 @@ public:
|
||||
virtual void solve(); //Solve the problem
|
||||
virtual void reset(); //Reset the problem so it can be run again
|
||||
//Gets
|
||||
virtual std::string getResult(); //Return a string with the solution to the problem
|
||||
std::vector<int> getNumbers() const; //Returns the numbers that were being searched
|
||||
int getProduct() const; //Returns the product that was requested
|
||||
};
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/ProjectEulerCPP/Headers/Problem12.hpp
|
||||
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem12.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-27-18
|
||||
//Modified: 07-09-20
|
||||
//Modified: 08-28-20
|
||||
//What is the value of the first triangle number to have over five hundred divisors?
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
|
||||
/* Copyright (C) 2020 Matthew Ellison
|
||||
@@ -46,6 +46,7 @@ public:
|
||||
virtual void solve(); //Solve the problem
|
||||
virtual void reset(); //Reset the problem so it can be run again
|
||||
//Gets
|
||||
virtual std::string getResult(); //Return a string with the solution to the problem
|
||||
int64_t getTriangularNumber() const; //Returns the triangular number
|
||||
int64_t getLastNumberAdded() const; //Get the final number that was added to the triangular number
|
||||
std::vector<int64_t> getDivisorsOfTriangularNumber() const; //Returns the list of divisors of the requested number
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/ProjectEulerCPP/Headers/Problem13.hpp
|
||||
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem13.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-29-18
|
||||
//Modified: 07-09-20
|
||||
//Modified: 08-28-20
|
||||
//Work out the first ten digits of the sum of the following one-hundred 50-digit numbers
|
||||
/*
|
||||
37107287533902102798797998220837590246510135740250
|
||||
@@ -154,6 +154,7 @@ public:
|
||||
virtual void solve(); //Solve the problem
|
||||
virtual void reset(); //Reset the problem so it can be run again
|
||||
//Gets
|
||||
virtual std::string getResult(); //Return a string with the solution to the problem
|
||||
std::vector<mpz_class> getNumbers() const; //Returns the list 50-digit numbers
|
||||
mpz_class getSum() const; //Returns the sum of the 50-digit numbers
|
||||
};
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/ProjectEulerCPP/Headers/Problem14.hpp
|
||||
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem14.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-29-18
|
||||
//Modified: 07-09-20
|
||||
//Modified: 08-28-20
|
||||
/*
|
||||
The following iterative sequence is defined for the set of positive integers:
|
||||
n → n/2 (n is even)
|
||||
@@ -53,6 +53,7 @@ public:
|
||||
virtual void solve(); //Solve the problem
|
||||
virtual void reset(); //Reset the problem so it can be run again
|
||||
//Gets
|
||||
virtual std::string getResult(); //Return a string with the solution to the problem
|
||||
uint64_t getLength() const; //Returns the length of the requested chain
|
||||
uint64_t getStartingNumber() const; //Returns the starting number of the requested chain
|
||||
};
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/ProjectEulerCPP/Headers/Problem15.hpp
|
||||
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem15.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-29-18
|
||||
//Modified: 07-09-20
|
||||
//Modified: 08-28-20
|
||||
//How many routes from the top left corner to the bottom right corner are there through a 20×20 grid if you can only move right and down?
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
|
||||
/*
|
||||
@@ -50,6 +50,7 @@ public:
|
||||
virtual void solve(); //Solve the problem
|
||||
virtual void reset(); //Reset the problem so it can be run again
|
||||
//Gets
|
||||
virtual std::string getResult(); //Return a string with the solution to the problem
|
||||
uint64_t getNumberOfRoutes() const; //Returns the number of routes found
|
||||
};
|
||||
/* Results:
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/ProjectEulerCPP/Headers/Problem16.hpp
|
||||
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem16.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-28-18
|
||||
//Modified: 07-09-20
|
||||
//Modified: 08-28-20
|
||||
//What is the sum of the digits of the number 2^1000?
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
|
||||
//This file contains a header from the gmp library. The library is used for large integers.
|
||||
@@ -49,6 +49,7 @@ public:
|
||||
virtual void solve(); //Solve the problem
|
||||
virtual void reset(); //Reset the problem so it can be run again
|
||||
//Gets
|
||||
virtual std::string getResult(); //Return a string with the solution to the problem
|
||||
mpz_class getNumber() const; //Returns the number that was calculated
|
||||
int getSum() const; //Return the sum of the digits of the number
|
||||
};
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/ProjectEulerCPP/Headers/Problem17.hpp
|
||||
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem17.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 10-05-18
|
||||
//Modified: 07-09-20
|
||||
//Modified: 08-28-20
|
||||
//If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
|
||||
/*
|
||||
@@ -50,6 +50,7 @@ public:
|
||||
virtual void solve(); //Solve the problem
|
||||
virtual void reset(); //Reset the problem so it can be run again
|
||||
//Gets
|
||||
virtual std::string getResult(); //Return a string with the solution to the problem
|
||||
uint64_t getLetterCount() const; //Returns the number of letters asked for
|
||||
};
|
||||
/* Results:
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/ProjectEulerCPP/Headers/Problem18.hpp
|
||||
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem18.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 11-01-18
|
||||
//Modified: 07-09-20
|
||||
//Modified: 08-28-20
|
||||
//Find the maximum total from top to bottom
|
||||
/*
|
||||
75
|
||||
@@ -81,6 +81,7 @@ public:
|
||||
virtual void solve(); //Solve the problem
|
||||
virtual void reset(); //Reset the problem so it can be run again
|
||||
//Gets
|
||||
virtual std::string getResult(); //Return a string with the solution to the problem
|
||||
std::string getPyramid(); //Returns the pyramid that was traversed as a string
|
||||
std::string getTrail(); //Returns the trail the algorithm took as a string
|
||||
int getTotal() const; //Returns the total that was asked for
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/ProjectEulerCPP/Headers/Problem19.hpp
|
||||
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem19.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-28-18
|
||||
//Modified: 07-09-20
|
||||
//Modified: 08-28-20
|
||||
//How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
|
||||
/*
|
||||
You are given the following information, but you may prefer to do some research for yourself.
|
||||
@@ -62,6 +62,7 @@ public:
|
||||
virtual void solve(); //Solve the problem
|
||||
virtual void reset(); //Reset the problem so it can be run again
|
||||
//Gets
|
||||
virtual std::string getResult(); //Return a string with the solution to the problem
|
||||
uint64_t getTotalSundays() const; //Returns the total sundays that were asked for
|
||||
};
|
||||
/* Results
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/ProjectEulerCPP/Headers/Problem2.hpp
|
||||
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem2.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-28-18
|
||||
//Modified: 07-09-20
|
||||
//Modified: 08-28-20
|
||||
//The sum of the even Fibonacci numbers less than 4,000,000
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
|
||||
/*
|
||||
@@ -44,6 +44,7 @@ public:
|
||||
virtual void solve(); //Solve the problem
|
||||
virtual void reset(); //Reset the problem so it can be run again
|
||||
//Gets
|
||||
virtual std::string getResult(); //Return a string with the solution to the problem
|
||||
uint64_t getSum() const; //Returns the requested sum
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/ProjectEulerCPP/Headers/Problem20.hpp
|
||||
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem20.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 11-07-18
|
||||
//Modified: 07-09-20
|
||||
//Modified: 08-28-20
|
||||
//What is the sum of the digits of 100!?
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
|
||||
//This file contains a header from the gmp library. The library is used for large integers.
|
||||
@@ -47,6 +47,7 @@ public:
|
||||
virtual void solve(); //Solve the problem
|
||||
virtual void reset(); //Reset the problem so it can be run again
|
||||
//Gets
|
||||
virtual std::string getResult(); //Return a string with the solution to the problem
|
||||
mpz_class getNumber() const; //Returns the number 100!
|
||||
std::string getNumberString() const; //Returns the number 100! in a string
|
||||
uint64_t getSum() const; //Returns the sum of the digits of 100!
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/ProjectEulerCPP/Headers/Problem21.hpp
|
||||
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem21.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 11-08-18
|
||||
//Modified: 07-09-20
|
||||
//Modified: 08-28-20
|
||||
//Evaluate the sum of all the amicable numbers under 10000
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
|
||||
/*
|
||||
@@ -49,6 +49,7 @@ public:
|
||||
virtual void solve(); //Solve the problem
|
||||
virtual void reset(); //Reset the problem so it can be run again
|
||||
//Gets
|
||||
virtual std::string getResult(); //Return a string with the solution to the problem
|
||||
std::vector<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
|
||||
};
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/ProjectEulerCPP/Headers/Problem22.hpp
|
||||
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem22.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 11-09-18
|
||||
//Modified: 07-09-20
|
||||
//Modified: 08-28-20
|
||||
//What is the total of all the name scores in the file?
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
|
||||
/*
|
||||
@@ -49,6 +49,7 @@ public:
|
||||
virtual void solve(); //Solve the problem
|
||||
virtual void reset(); //Reset the problem so it can be run again
|
||||
//Gets
|
||||
virtual std::string getResult(); //Return a string with the solution to the problem
|
||||
std::vector<std::string> getNames() const; //Returns the vector of the names being scored
|
||||
uint64_t getNameScoreSum() const; //Returns the sum of the names scores
|
||||
};
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/ProjectEulerCPP/Headers/Problem23.hpp
|
||||
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem23.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 11-09-18
|
||||
//Modified: 07-09-20
|
||||
//Modified: 08-28-20
|
||||
//Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
|
||||
/*
|
||||
@@ -50,6 +50,7 @@ public:
|
||||
virtual void solve(); //Solve the problem
|
||||
virtual void reset(); //Reset the problem so it can be run again
|
||||
//Gets
|
||||
virtual std::string getResult(); //Return a string with the solution to the problem
|
||||
uint64_t getSum() const; //Returns the sum of the numbers asked for
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/ProjectEulerCPP/Headers/Problem24.hpp
|
||||
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem24.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 11-11-18
|
||||
//Modified: 07-09-20
|
||||
//Modified: 08-28-20
|
||||
//What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
|
||||
/*
|
||||
@@ -44,6 +44,7 @@ public:
|
||||
virtual void solve(); //Solve the problem
|
||||
virtual void reset(); //Reset the problem so it can be run again
|
||||
//Gets
|
||||
virtual std::string getResult(); //Return a string with the solution to the problem
|
||||
std::vector<std::string> getPermutationsList() const; //Returns a vector with all of the permutations
|
||||
std::string getPermutation() const; //Returns the specific permutations you are looking for
|
||||
};
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/ProjectEulerCPP/Headers/Problem25.hpp
|
||||
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem25.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 11-13-18
|
||||
//Modified: 07-09-20
|
||||
//Modified: 08-28-20
|
||||
//What is the index of the first term in the Fibonacci sequence to contain 1000 digits?
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
|
||||
//This file contains a header from the gmp library. The library is used for large integers.
|
||||
@@ -48,6 +48,7 @@ public:
|
||||
virtual void solve(); //Solve the problem
|
||||
virtual void reset(); //Reset the problem so it can be run again
|
||||
//Gets
|
||||
virtual std::string getResult(); //Return a string with the solution to the problem
|
||||
mpz_class getNumber() const; //Returns the Fibonacci number asked for
|
||||
std::string getNumberString() const; //Returns the Fibonacci number asked for as a string
|
||||
mpz_class getIndex() const; //Returns the index of the requested Fibonacci number
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/ProjectEulerCPP/Headers/Problem26.hpp
|
||||
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem26.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 07-28-19
|
||||
//Modified: 07-09-20
|
||||
//Modified: 08-28-20
|
||||
//Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
|
||||
/*
|
||||
@@ -45,6 +45,7 @@ public:
|
||||
virtual void solve(); //Solve the problem
|
||||
virtual void reset(); //Reset the problem so it can be run again
|
||||
//Gets
|
||||
virtual std::string getResult(); //Return a string with the solution to the problem
|
||||
unsigned int getLongestCycle() const; //Returns the length of the longest cycle
|
||||
unsigned int getLongestNumber() const; //Returns the denominator that starts the longest cycle
|
||||
};
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/ProjectEulerCPP/Headers/Problem27.hpp
|
||||
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem27.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-14-19
|
||||
//Modified: 07-09-20
|
||||
//Modified: 08-28-20
|
||||
//Find the product of the coefficients, |a| < 1000 and |b| <= 1000, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n=0.
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
|
||||
/*
|
||||
@@ -46,6 +46,7 @@ public:
|
||||
virtual void solve(); //Solve the problem
|
||||
virtual void reset(); //Reset the problem so it can be run again
|
||||
//Gets
|
||||
virtual std::string getResult(); //Return a string with the solution to the problem
|
||||
int64_t getTopA() const; //Returns the top A that was generated
|
||||
int64_t getTopB() const; //Returns the top B that was generated
|
||||
int64_t getTopN() const; //Returns the top N that was generated
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/ProjectEulerCPP/Headers/Problem28.hpp
|
||||
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem28.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-21-19
|
||||
//Modified: 07-09-20
|
||||
//Modified: 08-28-20
|
||||
//What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed by starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
|
||||
/*
|
||||
@@ -50,6 +50,7 @@ public:
|
||||
virtual void solve(); //Solve the problem
|
||||
virtual void reset(); //Reset the problem so it can be run again
|
||||
//Gets
|
||||
virtual std::string getResult(); //Return a string with the solution to the problem
|
||||
std::vector<std::vector<int>> getGrid() const; //Returns the grid
|
||||
uint64_t getSum() const; //Returns the sum of the diagonals
|
||||
};
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/ProjectEulerCPP/Headers/Problem29.hpp
|
||||
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem29.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 10-06-19
|
||||
//Modified: 07-09-20
|
||||
//Modified: 08-28-20
|
||||
//How many distinct terms are in the sequence generated by a^b for 2 <= a <= 100 and 2 <= b <= 100?
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
|
||||
//This file contains a header from the gmp library. The library is used for large integers.
|
||||
@@ -53,6 +53,7 @@ public:
|
||||
virtual void solve(); //Solve the problem
|
||||
virtual void reset(); //Reset the problem so it can be run again
|
||||
//Gets
|
||||
virtual std::string getResult(); //Return a string with the solution to the problem
|
||||
unsigned int getBottomA() const; //Returns the lowest possible value for a
|
||||
unsigned int getTopA() const; //Returns the highest possible value for a
|
||||
unsigned int getBottomB() const; //Returns the lowest possible value for b
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/ProjectEulerCPP/Headers/Problem3.hpp
|
||||
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem3.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-28-18
|
||||
//Modified: 07-09-20
|
||||
//Modified: 08-28-20
|
||||
//The largest prime factor of 600851475143
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
|
||||
/*
|
||||
@@ -45,6 +45,7 @@ public:
|
||||
virtual void solve(); //Solve the problem
|
||||
virtual void reset(); //Reset the problem so it can be run again
|
||||
//Gets
|
||||
virtual std::string getResult(); //Return a string with the solution to the problem
|
||||
std::vector<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
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/ProjectEulerCPP/Headers/Problem30.hpp
|
||||
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem30.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 10-27-19
|
||||
//Modified: 07-09-20
|
||||
//Modified: 08-28-20
|
||||
//Find the sum of all the numbers that can be written as the sum of the fifth powers of their digits.
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
|
||||
/*
|
||||
@@ -50,6 +50,7 @@ public:
|
||||
virtual void solve(); //Solve the problem
|
||||
virtual void reset(); //Reset the problem so it can be run again
|
||||
//Gets
|
||||
virtual std::string getResult(); //Return a string with the solution to the problem
|
||||
uint64_t getTopNum() const; //This returns the top number to be checked
|
||||
std::vector<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
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/ProjectEulerCPP/Headers/Problem31.hpp
|
||||
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem31.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 06-19-20
|
||||
//Modified: 07-09-20
|
||||
//Modified: 08-28-20
|
||||
//How many different ways can £2 be made using any number of coins?
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
|
||||
/*
|
||||
@@ -44,6 +44,7 @@ public:
|
||||
virtual void solve(); //Solve the problem
|
||||
virtual void reset(); //Reset the problem so it can be run again
|
||||
//Gets
|
||||
virtual std::string getResult(); //Return a string with the solution to the problem
|
||||
int getPermutations() const; //Returns the number of correct permutations of the coins
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/ProjectEulerCPP/Headers/Problem32.hpp
|
||||
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem32.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 07-27-20
|
||||
//Modified: 07-27-20
|
||||
//Modified: 08-28-20
|
||||
//Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
|
||||
/*
|
||||
@@ -66,13 +66,11 @@ public:
|
||||
//Constructor
|
||||
Problem32();
|
||||
//Operational functions
|
||||
//Solve the problem
|
||||
void solve();
|
||||
//Reset the problem so it can be run again
|
||||
void reset();
|
||||
void solve(); //Solve the problem
|
||||
void reset(); //Reset the problem so it can be run again
|
||||
//Gets
|
||||
//Returns the sum of the pandigitals
|
||||
int64_t getSumOfPandigitals();
|
||||
virtual std::string getResult(); //Return a string with the solution to the problem
|
||||
int64_t getSumOfPandigitals(); //Returns the sum of the pandigitals
|
||||
};
|
||||
|
||||
/* Results:
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/ProjectEulerCPP/Headers/Problem4.hpp
|
||||
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem4.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-28-18
|
||||
//Modified: 07-09-20
|
||||
//Modified: 08-28-20
|
||||
//Find the largest palindrome made from the product of two 3-digit numbers
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
|
||||
/*
|
||||
@@ -45,6 +45,7 @@ public:
|
||||
virtual void solve(); //Solve the problem
|
||||
virtual void reset(); //Reset the problem so it can be run again
|
||||
//Gets
|
||||
virtual std::string getResult(); //Return a string with the solution to the problem
|
||||
std::vector<uint64_t> getPalindromes() const; //Returns the list of all palindromes
|
||||
uint64_t getLargestPalindrome() const; //Returns the largest palindrome
|
||||
};
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/ProjectEulerCPP/Headers/Problem5.hpp
|
||||
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem5.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-28-18
|
||||
//Modified: 07-09-20
|
||||
//Modified: 08-28-20
|
||||
//What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
|
||||
/*
|
||||
@@ -43,6 +43,7 @@ public:
|
||||
virtual void solve(); //Solve the problem
|
||||
virtual void reset(); //Reset the problem so it can be run again
|
||||
//Gets
|
||||
virtual std::string getResult(); //Return a string with the solution to the problem
|
||||
int getNumber() const; //Returns the requested number
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/ProjectEulerCPP/Headers/Problem6.hpp
|
||||
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem6.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-28-18
|
||||
//Modified: 07-09-20
|
||||
//Modified: 08-28-20
|
||||
//Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
|
||||
/*
|
||||
@@ -46,6 +46,7 @@ public:
|
||||
virtual void solve(); //Solve the problem
|
||||
virtual void reset(); //Reset the problem so it can be run again
|
||||
//Gets
|
||||
virtual std::string getResult(); //Return a string with the solution to the problem
|
||||
uint64_t getSumOfSquares() const; //Returns the sum of all the squares
|
||||
uint64_t getSquareOfSum() const; //Returns the square of all of the sums
|
||||
uint64_t getDifference() const; //Returns the requested difference
|
||||
@@ -1,4 +1,4 @@
|
||||
//ProjectEuler/ProjectEulerCPP/Headers/Problem67.hpp
|
||||
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem67.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 11-02-18
|
||||
//Modified: 07-09-20
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/ProjectEulerCPP/Headers/Problem7.hpp
|
||||
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem7.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-28-18
|
||||
//Modified: 07-09-20
|
||||
//Modified: 08-28-20
|
||||
//What is the 10001th prime number?
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
|
||||
/*
|
||||
@@ -45,6 +45,7 @@ public:
|
||||
virtual void solve(); //Solve the problem
|
||||
virtual void reset(); //Reset the problem so it can be run again
|
||||
//Gets
|
||||
virtual std::string getResult(); //Return a string with the solution to the problem
|
||||
uint64_t getPrime() const; //Returns the requested prime number
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/ProjectEulerCPP/Headers/Problem8.hpp
|
||||
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem8.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-28-18
|
||||
//Modified: 07-09-20
|
||||
//Modified: 08-28-20
|
||||
//Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
|
||||
/*
|
||||
73167176531330624919225119674426574742355349194934
|
||||
@@ -68,6 +68,7 @@ public:
|
||||
virtual void solve(); //Solve the problem
|
||||
virtual void reset(); //Reset the problem so it can be run again
|
||||
//Gets
|
||||
virtual std::string getResult(); //Return a string with the solution to the problem
|
||||
std::string getLargestNums() const; //Returns the string of numbers that produces the largest product
|
||||
uint64_t getLargestProduct() const; //Returns the requested product
|
||||
};
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/ProjectEulerCPP/Headers/Problem9.hpp
|
||||
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem9.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-28-18
|
||||
//Modified: 07-09-20
|
||||
//Modified: 08-28-20
|
||||
//There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product of abc.
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
|
||||
/*
|
||||
@@ -45,6 +45,7 @@ public:
|
||||
virtual void solve(); //Solve the problem
|
||||
virtual void reset(); //Reset the problem so it can be run again
|
||||
//Gets
|
||||
virtual std::string getResult(); //Return a string with the solution to the problem
|
||||
int getSideA() const; //Returns the length of the first side
|
||||
int getSideB() const; //Returns the length of the second side
|
||||
int getSideC() const; //Returns the length of the hyp
|
||||
208
Headers/benchmark.hpp
Normal file
208
Headers/benchmark.hpp
Normal file
@@ -0,0 +1,208 @@
|
||||
//ProjectEuler/ProjectEulerCPP/headers/benchmark.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 07-08-20
|
||||
//Modified: 08-28-20
|
||||
//These are functions that help determine an average run time for the problems
|
||||
/*
|
||||
Copyright (C) 2020 Matthew Ellison
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef BENCHMARK_HPP
|
||||
#define BENCHMARK_HPP
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "ProblemSelection.hpp"
|
||||
|
||||
|
||||
void printBenchmarkMenu(); //Print the benchmark menu
|
||||
int getBenchmarkMenuSelection(); //Returns a valid menu option
|
||||
bool isValidBenchmarkMenu(int selection); //Determines if a value is a valid menu option. Helper for getBenchmarkMenuSelection
|
||||
void runSpecific(); //Determines which problem user wants to run and runs it
|
||||
void runAllShort(); //Runs all problems except a few that are specified because of run length
|
||||
void runAll(); //Runs all problems
|
||||
unsigned int getNumberOfTimesToRun(); //Asks how many times a problem is supposed to run and returns the value
|
||||
double runProblem(Problem* problem, unsigned int timesToRun); //Runs the problem the given number of times
|
||||
std::string getBenchmarkResults(Problem* problem, double totalTime, unsigned int timesRan); //Prints the benchmark results of a problem
|
||||
|
||||
//Variables
|
||||
//Valid menu options
|
||||
enum BENCHMARK_OPTIONS {RUN_SPECIFIC = 1, RUN_ALL_SHORT, RUN_ALL, BENCHMARK_EXIT, BENCHMARK_SIZE};
|
||||
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
|
||||
void benchmarkMenu(){
|
||||
int selection = 0;
|
||||
|
||||
printBenchmarkMenu();
|
||||
selection = getBenchmarkMenuSelection();
|
||||
|
||||
switch(selection){
|
||||
case BENCHMARK_OPTIONS::RUN_SPECIFIC: runSpecific(); break;
|
||||
case BENCHMARK_OPTIONS::RUN_ALL_SHORT: runAllShort(); break;
|
||||
case BENCHMARK_OPTIONS::RUN_ALL: runAll(); break;
|
||||
case BENCHMARK_OPTIONS::BENCHMARK_EXIT: break;
|
||||
}
|
||||
}
|
||||
|
||||
//Print the benchmark menu
|
||||
void printBenchmarkMenu(){
|
||||
std::cout << "1. Run a specific problem\n"
|
||||
<< "2. Run all problems that have a reasonably short run time\n"
|
||||
<< "3. Run all problems\n"
|
||||
<< "4. Exit the menu" << std::endl;
|
||||
}
|
||||
|
||||
//Returns a valid menu option
|
||||
int getBenchmarkMenuSelection(){
|
||||
int selection = 0;
|
||||
std::cin >> selection;
|
||||
while(std::cin.fail() || !isValidBenchmarkMenu(selection)){
|
||||
std::cout << "That is an invalid option!\nPress Enter to continue" << std::endl;
|
||||
std::cin.clear();
|
||||
std::cin.get();
|
||||
printBenchmarkMenu();
|
||||
std::cin >> selection;
|
||||
}
|
||||
return selection;
|
||||
}
|
||||
|
||||
//Determines if a value is a valid menu option. Helper for getBenchmarkMenuSelection
|
||||
bool isValidBenchmarkMenu(int selection){
|
||||
if((selection > 0) && (selection < BENCHMARK_OPTIONS::BENCHMARK_SIZE)){
|
||||
return true;
|
||||
}
|
||||
else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//Determines which problem user wants to run and runs it
|
||||
void runSpecific(){
|
||||
//Ask which problem the user wants to run
|
||||
unsigned int problemNumber = getProblemNumber();
|
||||
//Ask how many times to run the problem
|
||||
unsigned int timesToRun = getNumberOfTimesToRun();
|
||||
|
||||
//Get the problem and print its description
|
||||
Problem* problem = getProblem(problemNumber);
|
||||
std::cout << problemNumber << ". " << problem->getDescription() << '\n';
|
||||
|
||||
//Run the problem the specific number of times
|
||||
double totalTime = runProblem(problem, timesToRun);
|
||||
|
||||
//Print the results
|
||||
std::cout << getBenchmarkResults(problem, totalTime, timesToRun);
|
||||
|
||||
//Release the memory the problem is in
|
||||
delete problem;
|
||||
}
|
||||
//Runs all problems except a few that are specified because of run length
|
||||
void runAllShort(){
|
||||
//Ask how many times to run the problem
|
||||
unsigned int timesToRun = getNumberOfTimesToRun();
|
||||
|
||||
//Run through all valid problem numbers, skipping a few that are in the tooLong vector
|
||||
for(unsigned int cnt = 1;cnt < PROBLEM_NUMBERS.size();++cnt){
|
||||
unsigned int problemNumber = PROBLEM_NUMBERS[cnt];
|
||||
//If the problem number is contained in the list of problems that take too long skip it
|
||||
if(mee::isFound(tooLong, problemNumber)){
|
||||
continue;
|
||||
}
|
||||
|
||||
//Get the problem and print its description
|
||||
Problem* problem = getProblem(problemNumber);
|
||||
std::cout << problemNumber << ". " << problem->getDescription() << '\n';
|
||||
|
||||
//Run the problem the specified number of times
|
||||
double totalTime = runProblem(problem, timesToRun);
|
||||
|
||||
//Print the results
|
||||
std::cout << getBenchmarkResults(problem, totalTime, timesToRun);
|
||||
|
||||
//Release the memory the problem is in
|
||||
delete problem;
|
||||
}
|
||||
}
|
||||
//Runs all problems
|
||||
void runAll(){
|
||||
//Ask how many times to run the problem
|
||||
unsigned int timesToRun = getNumberOfTimesToRun();
|
||||
|
||||
//Run through all valid problem numbers, skipping a few that are in the tooLong vector
|
||||
for(unsigned int cnt = 1;cnt < PROBLEM_NUMBERS.size();++cnt){
|
||||
unsigned int problemNumber = PROBLEM_NUMBERS[cnt];
|
||||
|
||||
//Get the problem
|
||||
Problem* problem = getProblem(problemNumber);
|
||||
|
||||
//Run the problem the specified number of times
|
||||
std::cout << problemNumber << ". " << problem->getDescription() << '\n';
|
||||
double totalTime = runProblem(problem, timesToRun);
|
||||
|
||||
//Print the results
|
||||
std::cout << getBenchmarkResults(problem, totalTime, timesToRun);
|
||||
|
||||
//Release the memory the problem is in
|
||||
delete problem;
|
||||
}
|
||||
}
|
||||
|
||||
//Asks how many times a problem is supposed to run and returns the value
|
||||
unsigned int getNumberOfTimesToRun(){
|
||||
unsigned int numOfTimesToRun = 1;
|
||||
std::cout << "How many times do you want to run this problem? ";
|
||||
std::cin >> numOfTimesToRun;
|
||||
while((std::cin.fail()) || (numOfTimesToRun < 1)){
|
||||
std::cout << "That is an invalid number!\nHow many times do you want to run this problem? ";
|
||||
std::cin.clear();
|
||||
std::cin >> numOfTimesToRun;
|
||||
}
|
||||
return numOfTimesToRun;
|
||||
}
|
||||
|
||||
//Runs the problem the given number of times
|
||||
double runProblem(Problem* problem, unsigned int timesToRun){
|
||||
double totalTime = 0;
|
||||
std::cout << "Solving";
|
||||
for(unsigned int cnt = 0;cnt < timesToRun;++cnt){
|
||||
//Reset the data so you are actually counting the run time a second time
|
||||
problem->reset();
|
||||
(std::cout << '.').flush();
|
||||
//Solve the problem
|
||||
problem->solve();
|
||||
//Get the time data
|
||||
totalTime += (problem->getTimer()).getNano();
|
||||
}
|
||||
return totalTime;
|
||||
}
|
||||
|
||||
//Prints the benchmark results of a problem
|
||||
std::string getBenchmarkResults(Problem* problem, double totalTime, unsigned int timesRun){
|
||||
//Calculate the average run time of the problem
|
||||
totalTime /= timesRun;
|
||||
std::string timeResults = mee::Stopwatch::getStr(totalTime);
|
||||
//Tally the results
|
||||
std::stringstream results;
|
||||
results << "\n\n" << problem->getResult();
|
||||
results << "\nIt took an average of " << timeResults << " to run this problem over " << timesRun << " iterations\n\n" << std::endl;
|
||||
return results.str();
|
||||
}
|
||||
|
||||
|
||||
#endif //BENCHMARK_HPP
|
||||
Reference in New Issue
Block a user