Moved files around to better match C++ norms

Changed results to match other languages
This commit is contained in:
2020-08-28 12:22:15 -04:00
parent e4a3f442e4
commit 514afd1bd2
71 changed files with 565 additions and 402 deletions

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Headers/Problem.hpp //ProjectEuler/ProjectEulerCPP/headers/Problem.hpp
//Matthew Ellison //Matthew Ellison
// Created: 07-04-19 // 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 //This is an abstract base class to allow polymorphism for the individual problems
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2020 Matthew Ellison
@@ -35,7 +35,6 @@ protected:
mee::Stopwatch timer; //Used to determine your algorithm's run time mee::Stopwatch timer; //Used to determine your algorithm's run time
bool solved; //Holds true after the problem has been solved 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 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: public:
//Constructors //Constructors
Problem() : solved(false){ Problem() : solved(false){
@@ -67,20 +66,14 @@ public:
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
virtual void reset(){ virtual void reset(){
result.str(std::string());
timer.reset(); timer.reset();
solved = false; 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 //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 #endif //PROBLEM_HPP

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/ProblemSelection.hpp //ProjectEuler/ProjectEulerCPP/headers/ProblemSelection.hpp
//Mattrixwv //Matthew Ellison
// Created: 07-08-20 // Created: 07-08-20
//Modified: 07-09-20 //Modified: 08-28-20
//This is a header file with a few functions to help select and run problems //This is a header file with a few functions to help select and run problems
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2020 Matthew Ellison
@@ -26,39 +26,39 @@
#include <iostream> #include <iostream>
#include "Algorithms.hpp" #include "Algorithms.hpp"
#include "Headers/Problem1.hpp" #include "Problems/Problem1.hpp"
#include "Headers/Problem2.hpp" #include "Problems/Problem2.hpp"
#include "Headers/Problem3.hpp" #include "Problems/Problem3.hpp"
#include "Headers/Problem4.hpp" #include "Problems/Problem4.hpp"
#include "Headers/Problem5.hpp" #include "Problems/Problem5.hpp"
#include "Headers/Problem6.hpp" #include "Problems/Problem6.hpp"
#include "Headers/Problem7.hpp" #include "Problems/Problem7.hpp"
#include "Headers/Problem8.hpp" #include "Problems/Problem8.hpp"
#include "Headers/Problem9.hpp" #include "Problems/Problem9.hpp"
#include "Headers/Problem10.hpp" #include "Problems/Problem10.hpp"
#include "Headers/Problem11.hpp" #include "Problems/Problem11.hpp"
#include "Headers/Problem12.hpp" #include "Problems/Problem12.hpp"
#include "Headers/Problem13.hpp" #include "Problems/Problem13.hpp"
#include "Headers/Problem14.hpp" #include "Problems/Problem14.hpp"
#include "Headers/Problem15.hpp" #include "Problems/Problem15.hpp"
#include "Headers/Problem16.hpp" #include "Problems/Problem16.hpp"
#include "Headers/Problem17.hpp" #include "Problems/Problem17.hpp"
#include "Headers/Problem18.hpp" #include "Problems/Problem18.hpp"
#include "Headers/Problem19.hpp" #include "Problems/Problem19.hpp"
#include "Headers/Problem20.hpp" #include "Problems/Problem20.hpp"
#include "Headers/Problem21.hpp" #include "Problems/Problem21.hpp"
#include "Headers/Problem22.hpp" #include "Problems/Problem22.hpp"
#include "Headers/Problem23.hpp" #include "Problems/Problem23.hpp"
#include "Headers/Problem24.hpp" #include "Problems/Problem24.hpp"
#include "Headers/Problem25.hpp" #include "Problems/Problem25.hpp"
#include "Headers/Problem26.hpp" #include "Problems/Problem26.hpp"
#include "Headers/Problem27.hpp" #include "Problems/Problem27.hpp"
#include "Headers/Problem28.hpp" #include "Problems/Problem28.hpp"
#include "Headers/Problem29.hpp" #include "Problems/Problem29.hpp"
#include "Headers/Problem30.hpp" #include "Problems/Problem30.hpp"
#include "Headers/Problem31.hpp" #include "Problems/Problem31.hpp"
#include "Headers/Problem32.hpp" #include "Problems/Problem32.hpp"
#include "Headers/Problem67.hpp" #include "Problems/Problem67.hpp"
//Setup the problem numbers //Setup the problem numbers
@@ -122,7 +122,7 @@ void solveProblem(Problem* problem){
//Solve the problem //Solve the problem
problem->solve(); problem->solve();
//Print the results //Print the results
std::cout << problem->getResults() std::cout << problem->getResult()
<< "\nIt took " << problem->getTime() << " to solve this problem.\n\n" << std::endl; << "\nIt took " << problem->getTime() << " to solve this problem.\n\n" << std::endl;
} }

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Headers/Problem1.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem1.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // 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 //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 //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 solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again virtual void reset(); //Reset the problem so it can be run again
//Gets //Gets
virtual std::string getResult(); //Return a string with the solution to the problem
uint64_t getSum() const; //Returns the requested sum uint64_t getSum() const; //Returns the requested sum
}; };

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Headers/Problem10.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem10.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // Created: 09-28-18
//Modified: 07-09-20 //Modified: 08-28-20
//Find the sum of all the primes below two million //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 //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 solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again virtual void reset(); //Reset the problem so it can be run again
//Gets //Gets
virtual std::string getResult(); //Return a string with the solution to the problem
uint64_t getSum() const; //Returns the sum that was requested uint64_t getSum() const; //Returns the sum that was requested
}; };

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Headers/Problem11.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem11.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-29-18 // 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? //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 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 solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again virtual void reset(); //Reset the problem so it can be run again
//Gets //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 std::vector<int> getNumbers() const; //Returns the numbers that were being searched
int getProduct() const; //Returns the product that was requested int getProduct() const; //Returns the product that was requested
}; };

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Headers/Problem12.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem12.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-27-18 // 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? //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 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
/* Copyright (C) 2020 Matthew Ellison /* Copyright (C) 2020 Matthew Ellison
@@ -46,6 +46,7 @@ public:
virtual void solve(); //Solve the problem virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again virtual void reset(); //Reset the problem so it can be run again
//Gets //Gets
virtual std::string getResult(); //Return a string with the solution to the problem
int64_t getTriangularNumber() const; //Returns the triangular number int64_t getTriangularNumber() const; //Returns the triangular number
int64_t getLastNumberAdded() const; //Get the final number that was added to 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 std::vector<int64_t> getDivisorsOfTriangularNumber() const; //Returns the list of divisors of the requested number

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Headers/Problem13.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem13.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-29-18 // 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 //Work out the first ten digits of the sum of the following one-hundred 50-digit numbers
/* /*
37107287533902102798797998220837590246510135740250 37107287533902102798797998220837590246510135740250
@@ -154,6 +154,7 @@ public:
virtual void solve(); //Solve the problem virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again virtual void reset(); //Reset the problem so it can be run again
//Gets //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 std::vector<mpz_class> getNumbers() const; //Returns the list 50-digit numbers
mpz_class getSum() const; //Returns the sum of the 50-digit numbers mpz_class getSum() const; //Returns the sum of the 50-digit numbers
}; };

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Headers/Problem14.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem14.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-29-18 // Created: 09-29-18
//Modified: 07-09-20 //Modified: 08-28-20
/* /*
The following iterative sequence is defined for the set of positive integers: The following iterative sequence is defined for the set of positive integers:
n n/2 (n is even) n n/2 (n is even)
@@ -53,6 +53,7 @@ public:
virtual void solve(); //Solve the problem virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again virtual void reset(); //Reset the problem so it can be run again
//Gets //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 getLength() const; //Returns the length of the requested chain
uint64_t getStartingNumber() const; //Returns the starting number of the requested chain uint64_t getStartingNumber() const; //Returns the starting number of the requested chain
}; };

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Headers/Problem15.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem15.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-29-18 // 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? //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 //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 solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again virtual void reset(); //Reset the problem so it can be run again
//Gets //Gets
virtual std::string getResult(); //Return a string with the solution to the problem
uint64_t getNumberOfRoutes() const; //Returns the number of routes found uint64_t getNumberOfRoutes() const; //Returns the number of routes found
}; };
/* Results: /* Results:

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Headers/Problem16.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem16.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // Created: 09-28-18
//Modified: 07-09-20 //Modified: 08-28-20
//What is the sum of the digits of the number 2^1000? //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 //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. //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 solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again virtual void reset(); //Reset the problem so it can be run again
//Gets //Gets
virtual std::string getResult(); //Return a string with the solution to the problem
mpz_class getNumber() const; //Returns the number that was calculated mpz_class getNumber() const; //Returns the number that was calculated
int getSum() const; //Return the sum of the digits of the number int getSum() const; //Return the sum of the digits of the number
}; };

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Headers/Problem17.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem17.hpp
//Matthew Ellison //Matthew Ellison
// Created: 10-05-18 // 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? //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 //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 solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again virtual void reset(); //Reset the problem so it can be run again
//Gets //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 uint64_t getLetterCount() const; //Returns the number of letters asked for
}; };
/* Results: /* Results:

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Headers/Problem18.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem18.hpp
//Matthew Ellison //Matthew Ellison
// Created: 11-01-18 // Created: 11-01-18
//Modified: 07-09-20 //Modified: 08-28-20
//Find the maximum total from top to bottom //Find the maximum total from top to bottom
/* /*
75 75
@@ -81,6 +81,7 @@ public:
virtual void solve(); //Solve the problem virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again virtual void reset(); //Reset the problem so it can be run again
//Gets //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 getPyramid(); //Returns the pyramid that was traversed as a string
std::string getTrail(); //Returns the trail the algorithm took 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 int getTotal() const; //Returns the total that was asked for

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Headers/Problem19.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem19.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // 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)? //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. 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 solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again virtual void reset(); //Reset the problem so it can be run again
//Gets //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 uint64_t getTotalSundays() const; //Returns the total sundays that were asked for
}; };
/* Results /* Results

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Headers/Problem2.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem2.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // Created: 09-28-18
//Modified: 07-09-20 //Modified: 08-28-20
//The sum of the even Fibonacci numbers less than 4,000,000 //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 //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 solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again virtual void reset(); //Reset the problem so it can be run again
//Gets //Gets
virtual std::string getResult(); //Return a string with the solution to the problem
uint64_t getSum() const; //Returns the requested sum uint64_t getSum() const; //Returns the requested sum
}; };

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Headers/Problem20.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem20.hpp
//Matthew Ellison //Matthew Ellison
// Created: 11-07-18 // Created: 11-07-18
//Modified: 07-09-20 //Modified: 08-28-20
//What is the sum of the digits of 100!? //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 //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. //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 solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again virtual void reset(); //Reset the problem so it can be run again
//Gets //Gets
virtual std::string getResult(); //Return a string with the solution to the problem
mpz_class getNumber() const; //Returns the number 100! mpz_class getNumber() const; //Returns the number 100!
std::string getNumberString() const; //Returns the number 100! in a string std::string getNumberString() const; //Returns the number 100! in a string
uint64_t getSum() const; //Returns the sum of the digits of 100! uint64_t getSum() const; //Returns the sum of the digits of 100!

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Headers/Problem21.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem21.hpp
//Matthew Ellison //Matthew Ellison
// Created: 11-08-18 // Created: 11-08-18
//Modified: 07-09-20 //Modified: 08-28-20
//Evaluate the sum of all the amicable numbers under 10000 //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 //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 solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again virtual void reset(); //Reset the problem so it can be run again
//Gets //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 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 uint64_t getSum() const; //Returns the sum of all of the amicable numbers
}; };

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Headers/Problem22.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem22.hpp
//Matthew Ellison //Matthew Ellison
// Created: 11-09-18 // Created: 11-09-18
//Modified: 07-09-20 //Modified: 08-28-20
//What is the total of all the name scores in the file? //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 //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 solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again virtual void reset(); //Reset the problem so it can be run again
//Gets //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 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 uint64_t getNameScoreSum() const; //Returns the sum of the names scores
}; };

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Headers/Problem23.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem23.hpp
//Matthew Ellison //Matthew Ellison
// Created: 11-09-18 // 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 //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 //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 solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again virtual void reset(); //Reset the problem so it can be run again
//Gets //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 uint64_t getSum() const; //Returns the sum of the numbers asked for
}; };

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Headers/Problem24.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem24.hpp
//Matthew Ellison //Matthew Ellison
// Created: 11-11-18 // 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? //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 //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 solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again virtual void reset(); //Reset the problem so it can be run again
//Gets //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::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 std::string getPermutation() const; //Returns the specific permutations you are looking for
}; };

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Headers/Problem25.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem25.hpp
//Matthew Ellison //Matthew Ellison
// Created: 11-13-18 // 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? //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 //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. //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 solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again virtual void reset(); //Reset the problem so it can be run again
//Gets //Gets
virtual std::string getResult(); //Return a string with the solution to the problem
mpz_class getNumber() const; //Returns the Fibonacci number asked for mpz_class getNumber() const; //Returns the Fibonacci number asked for
std::string getNumberString() const; //Returns the Fibonacci number asked for as a string 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 mpz_class getIndex() const; //Returns the index of the requested Fibonacci number

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Headers/Problem26.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem26.hpp
//Matthew Ellison //Matthew Ellison
// Created: 07-28-19 // 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. //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 //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 solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again virtual void reset(); //Reset the problem so it can be run again
//Gets //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 getLongestCycle() const; //Returns the length of the longest cycle
unsigned int getLongestNumber() const; //Returns the denominator that starts the longest cycle unsigned int getLongestNumber() const; //Returns the denominator that starts the longest cycle
}; };

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Headers/Problem27.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem27.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-14-19 // 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. //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 //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 solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again virtual void reset(); //Reset the problem so it can be run again
//Gets //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 getTopA() const; //Returns the top A that was generated
int64_t getTopB() const; //Returns the top B 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 int64_t getTopN() const; //Returns the top N that was generated

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Headers/Problem28.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem28.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-21-19 // 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 //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 //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 solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again virtual void reset(); //Reset the problem so it can be run again
//Gets //Gets
virtual std::string getResult(); //Return a string with the solution to the problem
std::vector<std::vector<int>> getGrid() const; //Returns the grid std::vector<std::vector<int>> getGrid() const; //Returns the grid
uint64_t getSum() const; //Returns the sum of the diagonals uint64_t getSum() const; //Returns the sum of the diagonals
}; };

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Headers/Problem29.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem29.hpp
//Matthew Ellison //Matthew Ellison
// Created: 10-06-19 // 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? //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 //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. //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 solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again virtual void reset(); //Reset the problem so it can be run again
//Gets //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 getBottomA() const; //Returns the lowest possible value for a
unsigned int getTopA() const; //Returns the highest 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 unsigned int getBottomB() const; //Returns the lowest possible value for b

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Headers/Problem3.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem3.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // Created: 09-28-18
//Modified: 07-09-20 //Modified: 08-28-20
//The largest prime factor of 600851475143 //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 //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 solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again virtual void reset(); //Reset the problem so it can be run again
//Gets //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 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 getLargestFactor() const; //Returns the largest factor of the number
uint64_t getGoalNumber() const; //Returns the number uint64_t getGoalNumber() const; //Returns the number

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Headers/Problem30.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem30.hpp
//Matthew Ellison //Matthew Ellison
// Created: 10-27-19 // 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. //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 //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 solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again virtual void reset(); //Reset the problem so it can be run again
//Gets //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 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 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 uint64_t getSumOfList() const; //This returns the sum of all entries in sumOfFifthNumbers

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Headers/Problem31.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem31.hpp
//Matthew Ellison //Matthew Ellison
// Created: 06-19-20 // 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? //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 //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 solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again virtual void reset(); //Reset the problem so it can be run again
//Gets //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 int getPermutations() const; //Returns the number of correct permutations of the coins
}; };

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Headers/Problem32.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem32.hpp
//Matthew Ellison //Matthew Ellison
// Created: 07-27-20 // 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. //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 //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 //Constructor
Problem32(); Problem32();
//Operational functions //Operational functions
//Solve the problem void solve(); //Solve the problem
void solve(); void reset(); //Reset the problem so it can be run again
//Reset the problem so it can be run again
void reset();
//Gets //Gets
//Returns the sum of the pandigitals virtual std::string getResult(); //Return a string with the solution to the problem
int64_t getSumOfPandigitals(); int64_t getSumOfPandigitals(); //Returns the sum of the pandigitals
}; };
/* Results: /* Results:

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Headers/Problem4.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem4.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // 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 //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 //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 solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again virtual void reset(); //Reset the problem so it can be run again
//Gets //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 std::vector<uint64_t> getPalindromes() const; //Returns the list of all palindromes
uint64_t getLargestPalindrome() const; //Returns the largest palindrome uint64_t getLargestPalindrome() const; //Returns the largest palindrome
}; };

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Headers/Problem5.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem5.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // 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? //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 //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 solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again virtual void reset(); //Reset the problem so it can be run again
//Gets //Gets
virtual std::string getResult(); //Return a string with the solution to the problem
int getNumber() const; //Returns the requested number int getNumber() const; //Returns the requested number
}; };

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Headers/Problem6.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem6.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // 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. //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 //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 solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again virtual void reset(); //Reset the problem so it can be run again
//Gets //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 getSumOfSquares() const; //Returns the sum of all the squares
uint64_t getSquareOfSum() const; //Returns the square of all of the sums uint64_t getSquareOfSum() const; //Returns the square of all of the sums
uint64_t getDifference() const; //Returns the requested difference uint64_t getDifference() const; //Returns the requested difference

View File

@@ -1,4 +1,4 @@
//ProjectEuler/ProjectEulerCPP/Headers/Problem67.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem67.hpp
//Matthew Ellison //Matthew Ellison
// Created: 11-02-18 // Created: 11-02-18
//Modified: 07-09-20 //Modified: 07-09-20

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Headers/Problem7.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem7.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // Created: 09-28-18
//Modified: 07-09-20 //Modified: 08-28-20
//What is the 10001th prime number? //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 //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 solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again virtual void reset(); //Reset the problem so it can be run again
//Gets //Gets
virtual std::string getResult(); //Return a string with the solution to the problem
uint64_t getPrime() const; //Returns the requested prime number uint64_t getPrime() const; //Returns the requested prime number
}; };

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Headers/Problem8.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem8.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // 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? //Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
/* /*
73167176531330624919225119674426574742355349194934 73167176531330624919225119674426574742355349194934
@@ -68,6 +68,7 @@ public:
virtual void solve(); //Solve the problem virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again virtual void reset(); //Reset the problem so it can be run again
//Gets //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 std::string getLargestNums() const; //Returns the string of numbers that produces the largest product
uint64_t getLargestProduct() const; //Returns the requested product uint64_t getLargestProduct() const; //Returns the requested product
}; };

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Headers/Problem9.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem9.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // 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. //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 //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 solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again virtual void reset(); //Reset the problem so it can be run again
//Gets //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 getSideA() const; //Returns the length of the first side
int getSideB() const; //Returns the length of the second side int getSideB() const; //Returns the length of the second side
int getSideC() const; //Returns the length of the hyp int getSideC() const; //Returns the length of the hyp

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/benchmark.hpp //ProjectEuler/ProjectEulerCPP/headers/benchmark.hpp
//Matthew Ellison //Matthew Ellison
// Created: 07-08-20 // Created: 07-08-20
//Modified: 07-09-20 //Modified: 08-28-20
//These are functions that help determine an average run time for the problems //These are functions that help determine an average run time for the problems
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2020 Matthew Ellison
@@ -199,7 +199,7 @@ std::string getBenchmarkResults(Problem* problem, double totalTime, unsigned int
std::string timeResults = mee::Stopwatch::getStr(totalTime); std::string timeResults = mee::Stopwatch::getStr(totalTime);
//Tally the results //Tally the results
std::stringstream results; std::stringstream results;
results << "\n\n" << problem->getResults(); results << "\n\n" << problem->getResult();
results << "\nIt took an average of " << timeResults << " to run this problem over " << timesRun << " iterations\n\n" << std::endl; results << "\nIt took an average of " << timeResults << " to run this problem over " << timesRun << " iterations\n\n" << std::endl;
return results.str(); return results.str();
} }

View File

@@ -4,7 +4,10 @@ LIBFLAGS = -shared -std=c++17 -O3 -fPIC -Wall
EXEFLAGS = -Wall -std=c++11 -O3 -Wl,-rpath,'$$ORIGIN/lib' EXEFLAGS = -Wall -std=c++11 -O3 -Wl,-rpath,'$$ORIGIN/lib'
LINKEDLIBS = -lgmp -lgmpxx LINKEDLIBS = -lgmp -lgmpxx
PROBLEM_NUMBERS = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 67 PROBLEM_NUMBERS = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 67
PROBLEM_FILES = $(patsubst %,Source/libProblem%.cpp,$(PROBLEM_NUMBERS)) SOURCE_DIR = src
PROBLEM_DIR = $(SOURCE_DIR)/Problems
INCLUDE_DIR = headers
PROBLEM_FILES = $(patsubst %,$(PROBLEM_DIR)/libProblem%.cpp,$(PROBLEM_NUMBERS))
LIBDIR = ./lib LIBDIR = ./lib
LIBS = $(patsubst %, -lProblem%,$(PROBLEM_NUMBERS)) LIBS = $(patsubst %, -lProblem%,$(PROBLEM_NUMBERS))
@@ -24,24 +27,24 @@ moveBin:
mv ProjectEuler.exe lib/ProjectEuler.exe mv ProjectEuler.exe lib/ProjectEuler.exe
#Building the Libraries #Building the Libraries
$(LIBDIR)/libProblem%.so: Source/Problem%.cpp $(LIBDIR)/libProblem%.so: $(PROBLEM_DIR)/Problem%.cpp
$(CXX) $(LIBFLAGS) -o $@ $< $(LINKEDLIBS) $(CXX) $(LIBFLAGS) -o $@ $< -I $(INCLUDE_DIR) $(LINKEDLIBS)
$(LIBDIR)/libProblem67.so: Source/Problem67.cpp $(LIBDIR)/libProblem67.so: $(PROBLEM_DIR)/Problem67.cpp
$(CXX) $(LIBFLAGS) -o $@ $< $(LINKEDLIBS) -L $(LIBDIR) -lProblem18 $(CXX) $(LIBFLAGS) -o $@ $< -I $(INCLUDE_DIR) $(LINKEDLIBS) -L $(LIBDIR) -lProblem18
libsMulti: libsMulti:
$(MAKE) libs -j $(NUMCORES) $(MAKE) libs -j $(NUMCORES)
#Building the Libraries for Windows #Building the Libraries for Windows
$(LIBDIR)/libProblem%.dll: Source/Problem%.cpp $(LIBDIR)/libProblem%.dll: $(PROBLEM_DIR)/Problem%.cpp
$(CXX) $(LIBFLAGS) -o $@ $< $(LINKEDLIBS) $(CXX) $(LIBFLAGS) -o $@ $< -I $(INCLUDE_DIR) $(LINKEDLIBS)
$(LIBDIR)/libProblem67.dll: Source/Problem67.cpp $(LIBDIR)/libProblem67.dll: $(PROBLEM_DIR)/Problem67.cpp
$(CXX) $(LIBFLAGS) -o $@ $< $(LINKEDLIBS) -L $(LIBDIR) -lProblem18 $(CXX) $(LIBFLAGS) -o $@ $< -I $(INCLUDE_DIR) $(LINKEDLIBS) -L $(LIBDIR) -lProblem18
libsWindowsMulti: libsWindowsMulti:
$(MAKE) libsWindows -j $(NUMCORESWIN) $(MAKE) libsWindows -j $(NUMCORESWIN)
#Building the executable #Building the executable
ProjectEuler: main.cpp ProjectEuler: $(SOURCE_DIR)/main.cpp
$(CXX) $(EXEFLAGS) -o $@.exe $< -L $(LIBDIR) $(LIBS) $(LINKEDLIBS) $(CXX) $(EXEFLAGS) -o $@.exe $< -I $(INCLUDE_DIR) -L $(LIBDIR) $(LIBS) $(LINKEDLIBS)
#Clean up/Remove all files and folders created #Clean up/Remove all files and folders created

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem1.cpp //ProjectEuler/ProjectEulerCPP/Source/Problem1.cpp
//Matthew Ellison //Matthew Ellison
// Created: 07-10-19 // Created: 07-10-19
//Modified: 07-09-20 //Modified: 08-28-20
//What is the sum of all the multiples of 3 or 5 that are less than 1000 //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 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
/* /*
@@ -27,8 +27,7 @@
#include <cinttypes> #include <cinttypes>
#include <vector> #include <vector>
#include "Stopwatch.hpp" #include "Stopwatch.hpp"
#include "../Headers/Problem.hpp" #include "Problems/Problem1.hpp"
#include "../Headers/Problem1.hpp"
//The highest number to be tested //The highest number to be tested
@@ -62,13 +61,9 @@ void Problem1::solve(){
//Stop the timer //Stop the timer
timer.stop(); 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 //Throw a flag to show the problem is solved
solved = true; solved = true;
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
void Problem1::reset(){ void Problem1::reset(){
Problem::reset(); Problem::reset();
@@ -76,6 +71,15 @@ void Problem1::reset(){
} }
//Gets //Gets
//Return a string with the solution to the problem
std::string Problem1::getResult(){
if(!solved){
throw Unsolved();
}
std::stringstream result;
result << "The sum of all the numbers < " << MAX_NUMBER + 1 << " that are divisible by 3 or 5 is " << fullSum;
return result.str();
}
//Returns the requested sum //Returns the requested sum
uint64_t Problem1::getSum() const{ uint64_t Problem1::getSum() const{
//If the prblem hasn't been solved throw an exception //If the prblem hasn't been solved throw an exception

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem10.cpp //ProjectEuler/ProjectEulerCPP/Source/Problem10.cpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // Created: 09-28-18
//Modified: 07-09-20 //Modified: 08-28-20
//Find the sum of all the primes below two million //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 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
/* /*
@@ -27,8 +27,7 @@
#include <sstream> #include <sstream>
#include "Stopwatch.hpp" #include "Stopwatch.hpp"
#include "Algorithms.hpp" #include "Algorithms.hpp"
#include "../Headers/Problem.hpp" #include "Problems/Problem10.hpp"
#include "../Headers/Problem10.hpp"
//The largest number to check for primes //The largest number to check for primes
@@ -53,19 +52,25 @@ void Problem10::solve(){
//Stop the timer //Stop the timer
timer.stop(); 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 //Throw a flag to show the problem is solved
solved = true; solved = true;
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
void Problem10::reset(){ void Problem10::reset(){
Problem::reset(); Problem::reset();
sum = 0; sum = 0;
} }
//Gets
//Return a string with the solution to the problem
std::string Problem10::getResult(){
if(!solved){
throw Unsolved();
}
std::stringstream result;
result << "The sum of all the primes less than " << GOAL_NUMBER + 1 << " is " << sum;
return result.str();
}
//Returns the sum that was requested //Returns the sum that was requested
uint64_t Problem10::getSum() const{ uint64_t Problem10::getSum() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem11.cpp //ProjectEuler/ProjectEulerCPP/Source/Problem11.cpp
//Matthew Ellison //Matthew Ellison
// Created: 09-29-18 // 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? //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 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
@@ -49,8 +49,7 @@
#include <sstream> #include <sstream>
#include "Stopwatch.hpp" #include "Stopwatch.hpp"
#include "Algorithms.hpp" #include "Algorithms.hpp"
#include "../Headers/Problem.hpp" #include "Problems/Problem11.hpp"
#include "../Headers/Problem11.hpp"
//This is the grid of number that we will be working with //This is the grid of number that we will be working with
@@ -174,20 +173,26 @@ void Problem11::solve(){
//Stop the timer //Stop the timer
timer.stop(); 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 //Throw a flag to show the problem is solved
solved = true; solved = true;
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
void Problem11::reset(){ void Problem11::reset(){
Problem::reset(); Problem::reset();
greatestProduct.clear(); greatestProduct.clear();
} }
//Gets
//Return a string with the solution to the problem
std::string Problem11::getResult(){
if(!solved){
throw Unsolved();
}
std::stringstream result;
result << "The greatest product of 4 number in a line is " << mee::getProduct(greatestProduct)
<< "\nThe numbers are " << greatestProduct.at(0) << ' ' << greatestProduct.at(1) << ' ' << greatestProduct.at(2) << ' ' << greatestProduct.at(3);
return result.str();
}
//Returns the numbers that were being searched //Returns the numbers that were being searched
std::vector<int> Problem11::getNumbers() const{ std::vector<int> Problem11::getNumbers() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -196,7 +201,6 @@ std::vector<int> Problem11::getNumbers() const{
} }
return greatestProduct; return greatestProduct;
} }
//Returns the product that was requested //Returns the product that was requested
int Problem11::getProduct() const{ int Problem11::getProduct() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem12.cpp //ProjectEuler/ProjectEulerCPP/Source/Problem12.cpp
//Matthew Ellison //Matthew Ellison
// Created: 09-27-18 // 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? //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 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
/* Copyright (C) 2020 Matthew Ellison /* Copyright (C) 2020 Matthew Ellison
@@ -26,8 +26,7 @@
#include <sstream> #include <sstream>
#include "Stopwatch.hpp" #include "Stopwatch.hpp"
#include "Algorithms.hpp" #include "Algorithms.hpp"
#include "../Headers/Problem.hpp" #include "Problems/Problem12.hpp"
#include "../Headers/Problem12.hpp"
//The number of divisors that you want //The number of divisors that you want
@@ -66,13 +65,9 @@ void Problem12::solve(){
//Stop the timer //Stop the timer
timer.stop(); 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 //Throw a flag to show the problem is solved
solved = true; solved = true;
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
void Problem12::reset(){ void Problem12::reset(){
Problem::reset(); Problem::reset();
@@ -81,6 +76,16 @@ void Problem12::reset(){
counter = 2; counter = 2;
} }
//Gets
//Return a string with the solution to the problem
std::string Problem12::getResult(){
if(!solved){
throw Unsolved();
}
std::stringstream result;
result << "The triangular number " << sum << " is a sum of all numbers >= " << counter - 1 << " and has " << divisors.size() << " divisors";
return result.str();
}
//Returns the triangular number //Returns the triangular number
int64_t Problem12::getTriangularNumber() const{ int64_t Problem12::getTriangularNumber() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -89,7 +94,6 @@ int64_t Problem12::getTriangularNumber() const{
} }
return sum; return sum;
} }
//Get the final number that was added to the triangular number //Get the final number that was added to the triangular number
int64_t Problem12::getLastNumberAdded() const{ int64_t Problem12::getLastNumberAdded() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -98,7 +102,6 @@ int64_t Problem12::getLastNumberAdded() const{
} }
return counter - 1; return counter - 1;
} }
//Returns the list of divisors of the requested number //Returns the list of divisors of the requested number
std::vector<int64_t> Problem12::getDivisorsOfTriangularNumber() const{ std::vector<int64_t> Problem12::getDivisorsOfTriangularNumber() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -107,7 +110,6 @@ std::vector<int64_t> Problem12::getDivisorsOfTriangularNumber() const{
} }
return divisors; return divisors;
} }
//Returns the number of divisors of the requested number //Returns the number of divisors of the requested number
size_t Problem12::getNumberOfDivisors() const{ size_t Problem12::getNumberOfDivisors() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem13.cpp //ProjectEuler/ProjectEulerCPP/Source/Problem13.cpp
//Matthew Ellison //Matthew Ellison
// Created: 09-29-18 // 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 //Work out the first ten digits of the sum of the following one-hundred 50-digit numbers
/* /*
37107287533902102798797998220837590246510135740250 37107287533902102798797998220837590246510135740250
@@ -134,8 +134,7 @@
#include "gmpxx.h" //This is part of the gmp library #include "gmpxx.h" //This is part of the gmp library
#include "Stopwatch.hpp" #include "Stopwatch.hpp"
#include "Algorithms.hpp" #include "Algorithms.hpp"
#include "../Headers/Problem.hpp" #include "Problems/Problem13.hpp"
#include "../Headers/Problem13.hpp"
//A function to set the nums vector //A function to set the nums vector
@@ -273,14 +272,9 @@ void Problem13::solve(){
//Stop the timer //Stop the timer
timer.stop(); 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 //Throw a flag to show the problem is solved
solved = true; solved = true;
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
void Problem13::reset(){ void Problem13::reset(){
Problem::reset(); Problem::reset();
@@ -289,6 +283,17 @@ void Problem13::reset(){
reserveVectors(); reserveVectors();
} }
//Gets
//Return a string with the solution to the problem
std::string Problem13::getResult(){
if(!solved){
throw Unsolved();
}
std::stringstream result;
result << "The sum of all " << nums.size() << " numbers is " << sum
<< "\nThe first 10 digits of the sum of the numbers is " << sum.get_str().substr(0, 10);
return result.str();
}
//Returns the list 50-digit numbers //Returns the list 50-digit numbers
std::vector<mpz_class> Problem13::getNumbers() const{ std::vector<mpz_class> Problem13::getNumbers() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -297,7 +302,6 @@ std::vector<mpz_class> Problem13::getNumbers() const{
} }
return nums; return nums;
} }
//Returns the sum of the 50-digit numbers //Returns the sum of the 50-digit numbers
mpz_class Problem13::getSum() const{ mpz_class Problem13::getSum() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem14.cpp //ProjectEuler/ProjectEulerCPP/Source/Problem14.cpp
//Matthew Ellison //Matthew Ellison
// Created: 09-29-18 // Created: 09-29-18
//Modified: 07-09-20 //Modified: 08-28-20
/* /*
The following iterative sequence is defined for the set of positive integers: The following iterative sequence is defined for the set of positive integers:
n n/2 (n is even) n n/2 (n is even)
@@ -31,8 +31,7 @@ Which starting number, under one million, produces the longest chain?
#include <string> #include <string>
#include <sstream> #include <sstream>
#include "Stopwatch.hpp" #include "Stopwatch.hpp"
#include "../Headers/Problem.hpp" #include "Problems/Problem14.hpp"
#include "../Headers/Problem14.hpp"
//This is the top number that you will be checking against the series //This is the top number that you will be checking against the series
@@ -82,13 +81,9 @@ void Problem14::solve(){
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
//Save the results
result << "The number " << maxNum << " produced a chain of " << maxLength << " steps";
//Throw a flag to show the problem is solved //Throw a flag to show the problem is solved
solved = true; solved = true;
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
void Problem14::reset(){ void Problem14::reset(){
Problem::reset(); Problem::reset();
@@ -96,6 +91,16 @@ void Problem14::reset(){
maxNum = 0; maxNum = 0;
} }
//Gets
//Return a string with the solution to the problem
std::string Problem14::getResult(){
if(!solved){
throw Unsolved();
}
std::stringstream result;
result << "The number " << maxNum << " produced a chain of " << maxLength << " steps";
return result.str();
}
//Returns the length of the requested chain //Returns the length of the requested chain
uint64_t Problem14::getLength() const{ uint64_t Problem14::getLength() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -104,7 +109,6 @@ uint64_t Problem14::getLength() const{
} }
return maxLength; return maxLength;
} }
//Returns the starting number of the requested chain //Returns the starting number of the requested chain
uint64_t Problem14::getStartingNumber() const{ uint64_t Problem14::getStartingNumber() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem15.hpp //ProjectEuler/ProjectEulerCPP/Source/Problem15.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-29-18 // 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? //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 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
/* /*
@@ -26,8 +26,7 @@
#include <string> #include <string>
#include <sstream> #include <sstream>
#include "Stopwatch.hpp" #include "Stopwatch.hpp"
#include "../Headers/Problem.hpp" #include "Problems/Problem15.hpp"
#include "../Headers/Problem15.hpp"
int Problem15::WIDTH = 20; //The width of the grid int Problem15::WIDTH = 20; //The width of the grid
@@ -78,19 +77,25 @@ void Problem15::solve(){
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
//Save the results
result << "The number of routes is " << numOfRoutes;
//Throw a flag to show the problem is solved //Throw a flag to show the problem is solved
solved = true; solved = true;
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
void Problem15::reset(){ void Problem15::reset(){
Problem::reset(); Problem::reset();
numOfRoutes = 0; numOfRoutes = 0;
} }
//Gets
//Return a string with the solution to the problem
std::string Problem15::getResult(){
if(!solved){
throw Unsolved();
}
std::stringstream result;
result << "The number of routes is " << numOfRoutes;
return result.str();
}
//Returns the number of routes found //Returns the number of routes found
uint64_t Problem15::getNumberOfRoutes() const{ uint64_t Problem15::getNumberOfRoutes() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem16.cpp //ProjectEuler/ProjectEulerCPP/Source/Problem16.cpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // Created: 09-28-18
//Modified: 07-09-20 //Modified: 08-28-20
//What is the sum of the digits of the number 2^1000? //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 //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. //This file contains a header from the gmp library. The library is used for large integers.
@@ -28,8 +28,7 @@
#include <sstream> #include <sstream>
#include <gmpxx.h> #include <gmpxx.h>
#include "Stopwatch.hpp" #include "Stopwatch.hpp"
#include "../Headers/Problem.hpp" #include "Problems/Problem16.hpp"
#include "../Headers/Problem16.hpp"
int Problem16::NUM_TO_POWER = 2; //The number that is going to be raised to a power int Problem16::NUM_TO_POWER = 2; //The number that is going to be raised to a power
@@ -65,14 +64,9 @@ void Problem16::solve(){
//Stop the timer //Stop the timer
timer.stop(); 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 //Throw a flag to show the problem is solved
solved = true; solved = true;
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
void Problem16::reset(){ void Problem16::reset(){
Problem::reset(); Problem::reset();
@@ -80,6 +74,17 @@ void Problem16::reset(){
sumOfElements = 0; sumOfElements = 0;
} }
//Gets
//Return a string with the solution to the problem
std::string Problem16::getResult(){
if(!solved){
throw Unsolved();
}
std::stringstream result;
result << NUM_TO_POWER << '^' << POWER << " = " << num
<< "\nThe sum of the elements is " << sumOfElements;
return result.str();
}
//Returns the number that was calculated //Returns the number that was calculated
mpz_class Problem16::getNumber() const{ mpz_class Problem16::getNumber() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -88,7 +93,6 @@ mpz_class Problem16::getNumber() const{
} }
return num; return num;
} }
//Return the sum of the digits of the number //Return the sum of the digits of the number
int Problem16::getSum() const{ int Problem16::getSum() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem17.cpp //ProjectEuler/ProjectEulerCPP/Source/Problem17.cpp
//Matthew Ellison //Matthew Ellison
// Created: 10-05-18 // 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? //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 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
/* /*
@@ -26,8 +26,7 @@
#include <string> #include <string>
#include <sstream> #include <sstream>
#include "Stopwatch.hpp" #include "Stopwatch.hpp"
#include "../Headers/Problem.hpp" #include "Problems/Problem17.hpp"
#include "../Headers/Problem17.hpp"
//This is the largest number to get the words of //This is the largest number to get the words of
@@ -155,11 +154,11 @@ std::string Problem17::wordHelper(int num){
case 3: tempString += "three"; break; case 3: tempString += "three"; break;
case 2: tempString += "two"; break; case 2: tempString += "two"; break;
case 1: tempString += "one"; break; case 1: tempString += "one"; break;
//TODO: Throw an exception
default: tempString += "ERROR"; break; default: tempString += "ERROR"; break;
} }
return tempString; return tempString;
} }
//This counts the number of letters in the string that is passed in (ignoring numbers and punctuation) //This counts the number of letters in the string that is passed in (ignoring numbers and punctuation)
uint64_t Problem17::countLetters(std::string str){ uint64_t Problem17::countLetters(std::string str){
uint64_t letterCount = 0; uint64_t letterCount = 0;
@@ -195,19 +194,25 @@ void Problem17::solve(){
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
//Save the results
result << "The number of letters is " << letterCount;
//Throw a flag to show the problem is solved //Throw a flag to show the problem is solved
solved = true; solved = true;
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
void Problem17::reset(){ void Problem17::reset(){
Problem::reset(); Problem::reset();
letterCount = 0; letterCount = 0;
} }
//Gets
//Return a string with the solution to the problem
std::string Problem17::getResult(){
if(!solved){
throw Unsolved();
}
std::stringstream result;
result << "The number of letters is " << letterCount;
return result.str();
}
//Returns the number of letters asked for //Returns the number of letters asked for
uint64_t Problem17::getLetterCount() const{ uint64_t Problem17::getLetterCount() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem18.hpp //ProjectEuler/ProjectEulerCPP/Source/Problem18.hpp
//Matthew Ellison //Matthew Ellison
// Created: 11-01-18 // Created: 11-01-18
//Modified: 07-09-20 //Modified: 08-28-20
//Find the maximum total from top to bottom //Find the maximum total from top to bottom
/* /*
75 75
@@ -45,7 +45,7 @@
#include <string> #include <string>
#include <sstream> #include <sstream>
#include "Stopwatch.hpp" #include "Stopwatch.hpp"
#include "../Headers/Problem18.hpp" #include "Problems/Problem18.hpp"
//Setup the list you are trying to find a path through //Setup the list you are trying to find a path through
@@ -139,13 +139,9 @@ void Problem18::solve(){
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
//Save the results
result << "The value of the longest path is " << actualTotal;
//Throw a flag to show the problem is solved //Throw a flag to show the problem is solved
solved = true; solved = true;
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
void Problem18::reset(){ void Problem18::reset(){
Problem::reset(); Problem::reset();
@@ -154,6 +150,16 @@ void Problem18::reset(){
actualTotal = 0; actualTotal = 0;
} }
//Gets
//Return a string with the solution to the problem
std::string Problem18::getResult(){
if(!solved){
throw Unsolved();
}
std::stringstream result;
result << "The value of the longest path is " << actualTotal;
return result.str();
}
//Returns the pyramid that was traversed as a string //Returns the pyramid that was traversed as a string
std::string Problem18::getPyramid(){ std::string Problem18::getPyramid(){
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -170,7 +176,6 @@ std::string Problem18::getPyramid(){
} }
return results.str(); return results.str();
} }
//Returns the trail the algorithm took as a string //Returns the trail the algorithm took as a string
std::string Problem18::getTrail(){ std::string Problem18::getTrail(){
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -230,7 +235,6 @@ std::string Problem18::getTrail(){
} }
return results.str(); return results.str();
} }
//Returns the total that was asked for //Returns the total that was asked for
int Problem18::getTotal() const{ int Problem18::getTotal() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem19.cpp //ProjectEuler/ProjectEulerCPP/Source/Problem19.cpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // 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)? //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. You are given the following information, but you may prefer to do some research for yourself.
@@ -37,7 +37,7 @@ A leap year occurs on any year evenly divisible by 4, but not on a century unles
#include <string> #include <string>
#include <sstream> #include <sstream>
#include "Stopwatch.hpp" #include "Stopwatch.hpp"
#include "../Headers/Problem19.hpp" #include "Problems/Problem19.hpp"
unsigned int Problem19::START_YEAR = 1901; //The start year unsigned int Problem19::START_YEAR = 1901; //The start year
@@ -119,7 +119,6 @@ Problem19::DAYS Problem19::getDay(unsigned int month, unsigned int day, unsigned
default: return ERROR; default: return ERROR;
} }
} }
//Returns true if the year passed to it is a leap year //Returns true if the year passed to it is a leap year
bool Problem19::isLeapYear(unsigned int year){ bool Problem19::isLeapYear(unsigned int year){
if(year < 1){ if(year < 1){
@@ -171,19 +170,25 @@ void Problem19::solve(){
//Stop the timer //Stop the timer
timer.stop(); 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 //Throw a flag to show the problem is solved
solved = true; solved = true;
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
void Problem19::reset(){ void Problem19::reset(){
Problem::reset(); Problem::reset();
totalSundays = 0; totalSundays = 0;
} }
//Gets
//Return a string with the solution to the problem
std::string Problem19::getResult(){
if(!solved){
throw Unsolved();
}
std::stringstream result;
result << "There are " << totalSundays << " Sundays that landed on the first of the months from " << START_YEAR << " to " << END_YEAR;
return result.str();
}
//Returns the total sundays that were asked for //Returns the total sundays that were asked for
uint64_t Problem19::getTotalSundays() const{ uint64_t Problem19::getTotalSundays() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem2.cpp //ProjectEuler/ProjectEulerCPP/Source/Problem2.cpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // Created: 09-28-18
//Modified: 07-09-20 //Modified: 08-28-20
//The sum of the even Fibonacci numbers less than 4,000,000 //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 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
/* /*
@@ -27,8 +27,7 @@
#include <sstream> #include <sstream>
#include "Stopwatch.hpp" #include "Stopwatch.hpp"
#include "Algorithms.hpp" #include "Algorithms.hpp"
#include "../Headers/Problem.hpp" #include "Problems/Problem2.hpp"
#include "../Headers/Problem2.hpp"
//Holds the largest number that we are looking for //Holds the largest number that we are looking for
@@ -61,19 +60,25 @@ void Problem2::solve(){
//Stop the timer //Stop the timer
timer.stop(); 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 //Throw a flag to show the problem is solved
solved = true; solved = true;
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
void Problem2::reset(){ void Problem2::reset(){
Problem::reset(); Problem::reset();
fullSum = 0; fullSum = 0;
} }
//Gets
//Return a string with the solution to the problem
std::string Problem2::getResult(){
if(!solved){
throw Unsolved();
}
std::stringstream result;
result << "The sum of the even Fibonacci numbers less than 4,000,000 is " << fullSum;
return result.str();
}
//Returns the requested sum //Returns the requested sum
uint64_t Problem2::getSum() const{ uint64_t Problem2::getSum() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem20.cpp //ProjectEuler/ProjectEulerCPP/Source/Problem20.cpp
//Matthew Ellison //Matthew Ellison
// Created: 11-07-18 // Created: 11-07-18
//Modified: 07-09-20 //Modified: 08-28-20
//What is the sum of the digits of 100!? //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 //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. //This file contains a header from the gmp library. The library is used for large integers.
@@ -30,7 +30,7 @@
#include <sstream> #include <sstream>
#include "gmpxx.h" #include "gmpxx.h"
#include "Stopwatch.hpp" #include "Stopwatch.hpp"
#include "../Headers/Problem20.hpp" #include "Problems/Problem20.hpp"
//Constructor //Constructor
@@ -64,14 +64,9 @@ void Problem20::solve(){
//Stop the timer //Stop the timer
timer.stop(); 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 //Throw a flag to show the problem is solved
solved = true; solved = true;
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
void Problem20::reset(){ void Problem20::reset(){
Problem::reset(); Problem::reset();
@@ -79,6 +74,17 @@ void Problem20::reset(){
num = 1; num = 1;
} }
//Gets
//Return a string with the solution to the problem
std::string Problem20::getResult(){
if(!solved){
throw Unsolved();
}
std::stringstream result;
result << "100! = " << num.get_str()
<< "\nThe sum of the digits is: " << sum;
return result.str();
}
//Returns the number 100! //Returns the number 100!
mpz_class Problem20::getNumber() const{ mpz_class Problem20::getNumber() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -87,7 +93,6 @@ mpz_class Problem20::getNumber() const{
} }
return num; return num;
} }
//Returns the number 100! in a string //Returns the number 100! in a string
std::string Problem20::getNumberString() const{ std::string Problem20::getNumberString() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -96,7 +101,6 @@ std::string Problem20::getNumberString() const{
} }
return num.get_str(); return num.get_str();
} }
//Returns the sum of the digits of 100! //Returns the sum of the digits of 100!
uint64_t Problem20::getSum() const{ uint64_t Problem20::getSum() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem21.cpp //ProjectEuler/ProjectEulerCPP/Source/Problem21.cpp
//Matthew Ellison //Matthew Ellison
// Created: 11-08-18 // Created: 11-08-18
//Modified: 07-09-20 //Modified: 08-28-20
//Evaluate the sum of all the amicable numbers under 10000 //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 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
/* /*
@@ -28,7 +28,7 @@
#include <sstream> #include <sstream>
#include "Algorithms.hpp" #include "Algorithms.hpp"
#include "Stopwatch.hpp" #include "Stopwatch.hpp"
#include "../Headers/Problem21.hpp" #include "Problems/Problem21.hpp"
//The top number that will be evaluated //The top number that will be evaluated
@@ -87,17 +87,9 @@ void Problem21::solve(){
//Stop the timer //Stop the timer
timer.stop(); 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 //Throw a flag to show the problem is solved
solved = true; solved = true;
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
void Problem21::reset(){ void Problem21::reset(){
Problem::reset(); Problem::reset();
@@ -106,6 +98,20 @@ void Problem21::reset(){
reserveVectors(); reserveVectors();
} }
//Gets
//Return a string with the solution to the problem
std::string Problem21::getResult(){
if(!solved){
throw Unsolved();
}
std::stringstream result;
result << "All amicable numbers less than 10000 are\n";
for(unsigned int cnt = 0;cnt < amicable.size();++cnt){
result << amicable.at(cnt) << '\n';
}
result << "The sum of all of these amicable numbers is " << mee::getSum(amicable);
return result.str();
}
//Returns a vector with all of the amicable numbers calculated //Returns a vector with all of the amicable numbers calculated
std::vector<uint64_t> Problem21::getAmicable() const{ std::vector<uint64_t> Problem21::getAmicable() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -114,7 +120,6 @@ std::vector<uint64_t> Problem21::getAmicable() const{
} }
return amicable; return amicable;
} }
//Returns the sum of all of the amicable numbers //Returns the sum of all of the amicable numbers
uint64_t Problem21::getSum() const{ uint64_t Problem21::getSum() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem22.cpp //ProjectEuler/ProjectEulerCPP/Source/Problem22.cpp
//Matthew Ellison //Matthew Ellison
// Created: 11-09-18 // Created: 11-09-18
//Modified: 07-09-20 //Modified: 08-28-20
//What is the total of all the name scores in the file? //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 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
/* /*
@@ -29,7 +29,7 @@
#include <sstream> #include <sstream>
#include "Stopwatch.hpp" #include "Stopwatch.hpp"
#include "Algorithms.hpp" #include "Algorithms.hpp"
#include "../Headers/Problem22.hpp" #include "Problems/Problem22.hpp"
//Holds the names that will be scored //Holds the names that will be scored
@@ -445,13 +445,9 @@ void Problem22::solve(){
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
//Save the results
result << "The answer to the question is " << mee::getSum(prod);
//Throw a flag to show the problem is solved //Throw a flag to show the problem is solved
solved = true; solved = true;
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
void Problem22::reset(){ void Problem22::reset(){
Problem::reset(); Problem::reset();
@@ -460,6 +456,17 @@ void Problem22::reset(){
reserveVectors(); reserveVectors();
} }
//Gets
//Return a string with the solution to the problem
std::string Problem22::getResult(){
if(!solved){
throw Unsolved();
}
std::stringstream result;
result << "The answer to the question is " << mee::getSum(prod);
return result.str();
}
//Returns the vector of the names being scored //Returns the vector of the names being scored
std::vector<std::string> Problem22::getNames() const{ std::vector<std::string> Problem22::getNames() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -468,7 +475,6 @@ std::vector<std::string> Problem22::getNames() const{
} }
return names; return names;
} }
//Returns the sum of the names' scores //Returns the sum of the names' scores
uint64_t Problem22::getNameScoreSum() const{ uint64_t Problem22::getNameScoreSum() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem23.cpp //ProjectEuler/ProjectEulerCPP/Source/Problem23.cpp
//Matthew Ellison //Matthew Ellison
// Created: 11-09-18 // 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 //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 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
/* /*
@@ -29,7 +29,7 @@
#include <sstream> #include <sstream>
#include "Stopwatch.hpp" #include "Stopwatch.hpp"
#include "Algorithms.hpp" #include "Algorithms.hpp"
#include "../Headers/Problem23.hpp" #include "Problems/Problem23.hpp"
//The largest possible number that can not be written as the sum of two abundant numbers //The largest possible number that can not be written as the sum of two abundant numbers
@@ -54,7 +54,6 @@ bool Problem23::isSum(const std::vector<int>& abund, int num){
//If you have run through the entire list and did not find a sum then it is false //If you have run through the entire list and did not find a sum then it is false
return false; return false;
} }
//Reserve the size of the vector to speed up insertion //Reserve the size of the vector to speed up insertion
void Problem23::reserveVectors(){ void Problem23::reserveVectors(){
//This makes sure the vector is the correct size //This makes sure the vector is the correct size
@@ -104,13 +103,9 @@ void Problem23::solve(){
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
//Save the results
result << "The answer is " << sum;
//Throw a flag to show the problem is solved //Throw a flag to show the problem is solved
solved = true; solved = true;
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
void Problem23::reset(){ void Problem23::reset(){
Problem::reset(); Problem::reset();
@@ -119,6 +114,16 @@ void Problem23::reset(){
reserveVectors(); reserveVectors();
} }
//Gets
//Return a string with the solution to the problem
std::string Problem23::getResult(){
if(!solved){
throw Unsolved();
}
std::stringstream result;
result << "The answer is " << sum;
return result.str();
}
//Returns the sum of the numbers asked for //Returns the sum of the numbers asked for
uint64_t Problem23::getSum() const{ uint64_t Problem23::getSum() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem24.cpp //ProjectEuler/ProjectEulerCPP/Source/Problem24.cpp
//Matthew Ellison //Matthew Ellison
// Created: 11-11-18 // 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? //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 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
/* /*
@@ -27,7 +27,7 @@
#include <sstream> #include <sstream>
#include "Algorithms.hpp" #include "Algorithms.hpp"
#include "Stopwatch.hpp" #include "Stopwatch.hpp"
#include "../Headers/Problem24.hpp" #include "Problems/Problem24.hpp"
//The number of the permutation that you need //The number of the permutation that you need
@@ -55,19 +55,25 @@ void Problem24::solve(){
//Stop the timer //Stop the timer
timer.stop(); 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 //Throw a flag to show the problem is solved
solved = true; solved = true;
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
void Problem24::reset(){ void Problem24::reset(){
Problem::reset(); Problem::reset();
permutations.clear(); permutations.clear();
} }
//Gets
//Return a string with the solution to the problem
std::string Problem24::getResult(){
if(!solved){
throw Unsolved();
}
std::stringstream result;
result << "The 1 millionth permutation is " << permutations.at(NEEDED_PERM - 1);
return result.str();
}
//Returns a vector with all of the permutations //Returns a vector with all of the permutations
std::vector<std::string> Problem24::getPermutationsList() const{ std::vector<std::string> Problem24::getPermutationsList() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -76,7 +82,6 @@ std::vector<std::string> Problem24::getPermutationsList() const{
} }
return permutations; return permutations;
} }
//Returns the specific permutations you are looking for //Returns the specific permutations you are looking for
std::string Problem24::getPermutation() const{ std::string Problem24::getPermutation() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem25.cpp //ProjectEuler/ProjectEulerCPP/Source/Problem25.cpp
//Matthew Ellison //Matthew Ellison
// Created: 11-13-18 // 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? //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 //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. //This file contains a header from the gmp library. The library is used for large integers.
@@ -30,7 +30,7 @@
#include "gmpxx.h" #include "gmpxx.h"
#include "Algorithms.hpp" #include "Algorithms.hpp"
#include "Stopwatch.hpp" #include "Stopwatch.hpp"
#include "../Headers/Problem25.hpp" #include "Problems/Problem25.hpp"
//The number of digits to calculate up to //The number of digits to calculate up to
@@ -61,14 +61,9 @@ void Problem25::solve(){
//Stop the timer //Stop the timer
timer.stop(); 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 //Throw a flag to show the problem is solved
solved = true; solved = true;
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
void Problem25::reset(){ void Problem25::reset(){
Problem::reset(); Problem::reset();
@@ -76,6 +71,17 @@ void Problem25::reset(){
index = 2; index = 2;
} }
//Gets
//Return a string with the solution to the problem
std::string Problem25::getResult(){
if(!solved){
throw Unsolved();
}
std::stringstream result;
result << "The first Fibonacci number with " << NUM_DIGITS << " digits is " << number
<< "\nIts index is " << index;
return result.str();
}
//Returns the Fibonacci number asked for //Returns the Fibonacci number asked for
mpz_class Problem25::getNumber() const{ mpz_class Problem25::getNumber() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -84,7 +90,6 @@ mpz_class Problem25::getNumber() const{
} }
return number; return number;
} }
//Returns the Fibonacci number asked for as a string //Returns the Fibonacci number asked for as a string
std::string Problem25::getNumberString() const{ std::string Problem25::getNumberString() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -93,7 +98,6 @@ std::string Problem25::getNumberString() const{
} }
return number.get_str(); return number.get_str();
} }
//Returns the index of the requested Fibonacci number //Returns the index of the requested Fibonacci number
mpz_class Problem25::getIndex() const{ mpz_class Problem25::getIndex() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -102,7 +106,6 @@ mpz_class Problem25::getIndex() const{
} }
return index; return index;
} }
//Returns the index of the requested Fibonacci number as a string //Returns the index of the requested Fibonacci number as a string
std::string Problem25::getIndexString() const{ std::string Problem25::getIndexString() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -111,7 +114,6 @@ std::string Problem25::getIndexString() const{
} }
return index.get_str(); return index.get_str();
} }
//Returns the index of the requested Fibonacci number as a uint64_t //Returns the index of the requested Fibonacci number as a uint64_t
uint64_t Problem25::getIndexInt() const{ uint64_t Problem25::getIndexInt() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem26.cpp //ProjectEuler/ProjectEulerCPP/Source/Problem26.cpp
//Matthew Ellison //Matthew Ellison
// Created: 07-28-19 // 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. //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 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
/* /*
@@ -26,7 +26,7 @@
#include <sstream> #include <sstream>
#include <vector> #include <vector>
#include "Algorithms.hpp" #include "Algorithms.hpp"
#include "../Headers/Problem26.hpp" #include "Problems/Problem26.hpp"
//Holds the highest denominator we will check //Holds the highest denominator we will check
@@ -89,14 +89,9 @@ void Problem26::solve(){
//Stop the timer //Stop the timer
timer.stop(); 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 //Throw a flag to show the problem is solved
solved = true; solved = true;
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
void Problem26::reset(){ void Problem26::reset(){
Problem::reset(); Problem::reset();
@@ -104,6 +99,17 @@ void Problem26::reset(){
longestNumber = 1; longestNumber = 1;
} }
//Gets
//Return a string with the solution to the problem
std::string Problem26::getResult(){
if(!solved){
throw Unsolved();
}
std::stringstream result;
result << "The longest cycle is " << longestCycle << " digits long"
<< "\nIt is started with the number " << longestNumber;
return result.str();
}
//Returns the length of the longest cycle //Returns the length of the longest cycle
unsigned int Problem26::getLongestCycle() const{ unsigned int Problem26::getLongestCycle() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -112,7 +118,6 @@ unsigned int Problem26::getLongestCycle() const{
} }
return longestCycle; return longestCycle;
} }
//Returns the denominator that starts the longest cycle //Returns the denominator that starts the longest cycle
unsigned int Problem26::getLongestNumber() const{ unsigned int Problem26::getLongestNumber() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem27.cpp //ProjectEuler/ProjectEulerCPP/Source/Problem27.cpp
//Matthew Ellison //Matthew Ellison
// Created: 09-14-19 // 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. //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 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
/* /*
@@ -25,7 +25,7 @@
#include <sstream> #include <sstream>
#include <cinttypes> #include <cinttypes>
#include "Algorithms.hpp" #include "Algorithms.hpp"
#include "../Headers/Problem27.hpp" #include "Problems/Problem27.hpp"
//Constructor //Constructor
@@ -70,15 +70,9 @@ void Problem27::solve(){
//Stop the timer //Stop the timer
timer.stop(); 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 //Throw a flag to show the problem is solved
solved = true; solved = true;
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
void Problem27::reset(){ void Problem27::reset(){
Problem::reset(); Problem::reset();
@@ -86,6 +80,18 @@ void Problem27::reset(){
primes.clear(); primes.clear();
} }
//Gets
//Return a string with the solution to the problem
std::string Problem27::getResult(){
if(!solved){
throw Unsolved();
}
std::stringstream result;
result << "The greatest number of primes found is " << topN
<< "\nIt was found with A = " << topA << ", B = " << topB
<< "\nThe product of A and B is " << topA * topB;
return result.str();
}
//Returns the top A that was generated //Returns the top A that was generated
int64_t Problem27::getTopA() const{ int64_t Problem27::getTopA() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -94,7 +100,6 @@ int64_t Problem27::getTopA() const{
} }
return topA; return topA;
} }
//Returns the top B that was generated //Returns the top B that was generated
int64_t Problem27::getTopB() const{ int64_t Problem27::getTopB() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -103,7 +108,6 @@ int64_t Problem27::getTopB() const{
} }
return topB; return topB;
} }
//Returns the top N that was generated //Returns the top N that was generated
int64_t Problem27::getTopN() const{ int64_t Problem27::getTopN() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem28.cpp //ProjectEuler/ProjectEulerCPP/Source/Problem28.cpp
//Matthew Ellison //Matthew Ellison
// Created: 09-21-19 // 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 //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 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
/* /*
@@ -26,7 +26,7 @@
#include <cinttypes> #include <cinttypes>
#include <vector> #include <vector>
#include <sstream> #include <sstream>
#include "../Headers/Problem28.hpp" #include "Problems/Problem28.hpp"
//This sets up the grid to hold the correct number of variables //This sets up the grid to hold the correct number of variables
@@ -39,7 +39,6 @@ void Problem28::setupGrid(){
} }
} }
} }
//Puts all of the numbers in the grid up the grid //Puts all of the numbers in the grid up the grid
void Problem28::createGrid(){ void Problem28::createGrid(){
bool finalLocation = false; //A flag to indicate if the final location to be filled has been reached bool finalLocation = false; //A flag to indicate if the final location to be filled has been reached
@@ -84,7 +83,6 @@ void Problem28::createGrid(){
} }
} }
} }
//Finds the sum of the diagonals in the grid //Finds the sum of the diagonals in the grid
void Problem28::findSum(){ void Problem28::findSum(){
//Start at the top corners and work your way down moving toward the opposite side //Start at the top corners and work your way down moving toward the opposite side
@@ -132,13 +130,9 @@ void Problem28::solve(){
//Stop the timer //Stop the timer
timer.stop(); 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 //Throw a flag to show the problem is solved
solved = true; solved = true;
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
void Problem28::reset(){ void Problem28::reset(){
Problem::reset(); Problem::reset();
@@ -147,6 +141,16 @@ void Problem28::reset(){
setupGrid(); setupGrid();
} }
//Gets
//Return a string with the solution to the problem
std::string Problem28::getResult(){
if(!solved){
throw Unsolved();
}
std::stringstream result;
result << "The sum of the diagonals in the given grid is " << sumOfDiagonals;
return result.str();
}
//Returns the grid //Returns the grid
std::vector<std::vector<int>> Problem28::getGrid() const{ std::vector<std::vector<int>> Problem28::getGrid() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -155,7 +159,6 @@ std::vector<std::vector<int>> Problem28::getGrid() const{
} }
return grid; return grid;
} }
//Returns the sum of the diagonals //Returns the sum of the diagonals
uint64_t Problem28::getSum() const{ uint64_t Problem28::getSum() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem29.cpp //ProjectEuler/ProjectEulerCPP/Source/Problem29.cpp
//Matthew Ellison //Matthew Ellison
// Created: 10-06-19 // 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? //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 //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. //This file contains a header from the gmp library. The library is used for large integers.
@@ -30,7 +30,7 @@
#include <vector> #include <vector>
#include <cmath> #include <cmath>
#include <gmpxx.h> #include <gmpxx.h>
#include "../Headers/Problem29.hpp" #include "Problems/Problem29.hpp"
#include "Algorithms.hpp" #include "Algorithms.hpp"
@@ -73,19 +73,25 @@ void Problem29::solve(){
//Stop the timer //Stop the timer
timer.stop(); 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 //Throw a flag to show the problem is solved
solved = true; solved = true;
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
void Problem29::reset(){ void Problem29::reset(){
Problem::reset(); Problem::reset();
unique.clear(); unique.clear();
} }
//Gets
//Return a string with the solution to the problem
std::string Problem29::getResult(){
if(!solved){
throw Unsolved();
}
std::stringstream result;
result << "The number of unique values generated by a^b for " << BOTTOM_A << " <= a <= " << TOP_A << " and " << BOTTOM_B << " <= b <= " << TOP_B << " is " << unique.size();
return result.str();
}
//Returns the lowest possible value for a //Returns the lowest possible value for a
unsigned int Problem29::getBottomA() const{ unsigned int Problem29::getBottomA() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -94,7 +100,6 @@ unsigned int Problem29::getBottomA() const{
} }
return BOTTOM_A; return BOTTOM_A;
} }
//Returns the highest possible value for a //Returns the highest possible value for a
unsigned int Problem29::getTopA() const{ unsigned int Problem29::getTopA() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -103,7 +108,6 @@ unsigned int Problem29::getTopA() const{
} }
return TOP_A; return TOP_A;
} }
//Returns the lowest possible value for b //Returns the lowest possible value for b
unsigned int Problem29::getBottomB() const{ unsigned int Problem29::getBottomB() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -112,7 +116,6 @@ unsigned int Problem29::getBottomB() const{
} }
return BOTTOM_B; return BOTTOM_B;
} }
//Returns the highest possible value for b //Returns the highest possible value for b
unsigned int Problem29::getTopB() const{ unsigned int Problem29::getTopB() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -121,7 +124,6 @@ unsigned int Problem29::getTopB() const{
} }
return TOP_B; return TOP_B;
} }
//Returns a vector of all the unique values for a^b //Returns a vector of all the unique values for a^b
std::vector<mpz_class> Problem29::getUnique() const{ std::vector<mpz_class> Problem29::getUnique() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem3.cpp //ProjectEuler/ProjectEulerCPP/Source/Problem3.cpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // Created: 09-28-18
//Modified: 07-09-20 //Modified: 08-28-20
//The largest prime factor of 600851475143 //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 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
/* /*
@@ -28,8 +28,7 @@
#include <sstream> #include <sstream>
#include "Stopwatch.hpp" #include "Stopwatch.hpp"
#include "Algorithms.hpp" #include "Algorithms.hpp"
#include "../Headers/Problem.hpp" #include "Problems/Problem3.hpp"
#include "../Headers/Problem3.hpp"
//The number of which you are trying to find the factors //The number of which you are trying to find the factors
@@ -54,19 +53,25 @@ void Problem3::solve(){
//Stop the timer //Stop the timer
timer.stop(); 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 //Throw a flag to show the problem is solved
solved = true; solved = true;
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
void Problem3::reset(){ void Problem3::reset(){
Problem::reset(); Problem::reset();
factors.clear(); factors.clear();
} }
//Gets
//Return a string with the solution to the problem
std::string Problem3::getResult(){
if(!solved){
throw Unsolved();
}
std::stringstream result;
result << "The largest factor of the number " << GOAL_NUMBER << " is " << factors[factors.size() - 1];
return result.str();
}
//Returns the list of factors of the number //Returns the list of factors of the number
std::vector<uint64_t> Problem3::getFactors() const{ std::vector<uint64_t> Problem3::getFactors() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -75,7 +80,6 @@ std::vector<uint64_t> Problem3::getFactors() const{
} }
return factors; return factors;
} }
//Returns the largest factor of the number //Returns the largest factor of the number
uint64_t Problem3::getLargestFactor() const{ uint64_t Problem3::getLargestFactor() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -85,7 +89,6 @@ uint64_t Problem3::getLargestFactor() const{
return *factors.end(); return *factors.end();
} }
//Returns the number //Returns the number
uint64_t Problem3::getGoalNumber() const{ uint64_t Problem3::getGoalNumber() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem30.cpp //ProjectEuler/ProjectEulerCPP/Source/Problem30.cpp
//Matthew Ellison //Matthew Ellison
// Created: 10-27-19 // 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. //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 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
/* /*
@@ -27,7 +27,7 @@
#include <cinttypes> #include <cinttypes>
#include <vector> #include <vector>
#include <cmath> #include <cmath>
#include "../Headers/Problem30.hpp" #include "Problems/Problem30.hpp"
//This is the largest number that will be checked //This is the largest number that will be checked
@@ -85,17 +85,23 @@ void Problem30::solve(){
//Throw a flag to show the problem is solved //Throw a flag to show the problem is solved
solved = true; 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 //Reset the problem so it can be run again
void Problem30::reset(){ void Problem30::reset(){
Problem::reset(); Problem::reset();
sumOfFifthNumbers.clear(); sumOfFifthNumbers.clear();
} }
//Gets
//Return a string with the solution to the problem
std::string Problem30::getResult(){
if(!solved){
throw Unsolved();
}
std::stringstream result;
result << "The sum of all the numbers that can be written as the sum of the fifth powers of their digits is " << getSumOfList();
return result.str();
}
//This returns the top number to be checked //This returns the top number to be checked
uint64_t Problem30::getTopNum() const{ uint64_t Problem30::getTopNum() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -105,7 +111,6 @@ uint64_t Problem30::getTopNum() const{
return TOP_NUM; return TOP_NUM;
} }
//This returns a copy of the vector holding all the numbers that are the sum of the fifth power of their digits //This returns a copy of the vector holding all the numbers that are the sum of the fifth power of their digits
std::vector<uint64_t> Problem30::getListOfSumOfFifths() const{ std::vector<uint64_t> Problem30::getListOfSumOfFifths() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -115,7 +120,6 @@ std::vector<uint64_t> Problem30::getListOfSumOfFifths() const{
return sumOfFifthNumbers; return sumOfFifthNumbers;
} }
//This returns the sum of all entries in sumOfFifthNumbers //This returns the sum of all entries in sumOfFifthNumbers
uint64_t Problem30::getSumOfList() const{ uint64_t Problem30::getSumOfList() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem31.cpp //ProjectEuler/ProjectEulerCPP/Source/Problem31.cpp
//Matthew Ellison //Matthew Ellison
// Created: 06-19-20 // Created: 06-19-20
//Modified: 07-11-20 //Modified: 08-28-20
//How many different ways can £2 be made using any number of coins? //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 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
/* /*
@@ -25,7 +25,7 @@
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <vector> #include <vector>
#include "../Headers/Problem31.hpp" #include "Problems/Problem31.hpp"
//The value of coins we want //The value of coins we want
@@ -67,19 +67,25 @@ void Problem31::solve(){
//Stop the timer //Stop the timer
timer.stop(); 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 //Throw a flag to show the problem is solved
solved = true; solved = true;
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
void Problem31::reset(){ void Problem31::reset(){
Problem::reset(); Problem::reset();
permutations = 0; permutations = 0;
} }
//Gets
//Return a string with the solution to the problem
std::string Problem31::getResult(){
if(!solved){
throw Unsolved();
}
std::stringstream result;
result << "There are " << permutations << " ways to make 2 pounds with the given denominations of coins";
return result.str();
}
//Returns the number of correct permutations of the coins //Returns the number of correct permutations of the coins
int Problem31::getPermutations() const{ int Problem31::getPermutations() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem32.cpp //ProjectEuler/ProjectEulerCPP/Source/Problem32.cpp
//Matthew Ellison //Matthew Ellison
// Created: 07-27-20 // 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. //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 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
/* /*
@@ -27,7 +27,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "Algorithms.hpp" #include "Algorithms.hpp"
#include "../Headers/Problem32.hpp" #include "Problems/Problem32.hpp"
#include <iostream> #include <iostream>
int Problem32::TOP_MULTIPLICAND = 99; //The largest multiplicand to check int Problem32::TOP_MULTIPLICAND = 99; //The largest multiplicand to check
@@ -94,9 +94,6 @@ void Problem32::solve(){
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
//Save the results
result << "There are " << listOfProducts.size() << " unique 1-9 pandigitals\nThe sum of the products of these pandigitals is " << sumOfPandigitals;
//Throw a flag to show the problem is solved //Throw a flag to show the problem is solved
solved = true; solved = true;
} }
@@ -106,7 +103,17 @@ void Problem32::reset(){
listOfProducts.clear(); listOfProducts.clear();
sumOfPandigitals = 0; sumOfPandigitals = 0;
} }
//Gets //Gets
//Return a string with the solution to the problem
std::string Problem32::getResult(){
if(!solved){
throw Unsolved();
}
std::stringstream result;
result << "There are " << listOfProducts.size() << " unique 1-9 pandigitals\nThe sum of the products of these pandigitals is " << sumOfPandigitals;
return result.str();
}
//Returns the sum of the pandigitals //Returns the sum of the pandigitals
int64_t Problem32::getSumOfPandigitals(){ int64_t Problem32::getSumOfPandigitals(){
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem4.cpp //ProjectEuler/ProjectEulerCPP/Source/Problem4.cpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // 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 //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 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
/* /*
@@ -27,8 +27,7 @@
#include <sstream> #include <sstream>
#include <algorithm> #include <algorithm>
#include "Stopwatch.hpp" #include "Stopwatch.hpp"
#include "../Headers/Problem.hpp" #include "Problems/Problem4.hpp"
#include "../Headers/Problem4.hpp"
//The first number to check //The first number to check
@@ -75,19 +74,25 @@ void Problem4::solve(){
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
//Save the results
result << "The largest palindrome is " << *(palindromes.end() - 1);
//Throw a flag to show the problem is solved //Throw a flag to show the problem is solved
solved = true; solved = true;
} }
//Resets the problem so it can be run again //Resets the problem so it can be run again
void Problem4::reset(){ void Problem4::reset(){
Problem::reset(); Problem::reset();
palindromes.clear(); palindromes.clear();
} }
//Gets
//Return a string with the solution to the problem
std::string Problem4::getResult(){
if(!solved){
throw Unsolved();
}
std::stringstream result;
result << "The largest palindrome is " << *(palindromes.end() - 1);
return result.str();
}
//Returns the list of all palindromes //Returns the list of all palindromes
std::vector<uint64_t> Problem4::getPalindromes() const{ std::vector<uint64_t> Problem4::getPalindromes() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -96,7 +101,6 @@ std::vector<uint64_t> Problem4::getPalindromes() const{
} }
return palindromes; return palindromes;
} }
//Returns the largest palindrome //Returns the largest palindrome
uint64_t Problem4::getLargestPalindrome() const{ uint64_t Problem4::getLargestPalindrome() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem5.cpp //ProjectEuler/ProjectEulerCPP/Source/Problem5.cpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // 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? //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 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
/* /*
@@ -27,8 +27,7 @@
#include <string> #include <string>
#include <sstream> #include <sstream>
#include "Stopwatch.hpp" #include "Stopwatch.hpp"
#include "../Headers/Problem.hpp" #include "Problems/Problem5.hpp"
#include "../Headers/Problem5.hpp"
//Constructor //Constructor
@@ -72,19 +71,25 @@ void Problem5::solve(){
//Stop the timer //Stop the timer
timer.stop(); 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 //Throw a flag to show the problem is solved
solved = true; solved = true;
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
void Problem5::reset(){ void Problem5::reset(){
Problem::reset(); Problem::reset();
smallestNum = 0; smallestNum = 0;
} }
//Gets
//Return a string with the solution to the problem
std::string Problem5::getResult(){
if(!solved){
throw Unsolved();
}
std::stringstream result;
result << "The smallest positive number evenly divisible by all numbers 1-20 is " << smallestNum;
return result.str();
}
//Returns the requested number //Returns the requested number
int Problem5::getNumber() const{ int Problem5::getNumber() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem6.cpp //ProjectEuler/ProjectEulerCPP/Source/Problem6.cpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // 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. //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 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
/* /*
@@ -27,8 +27,7 @@
#include <string> #include <string>
#include <sstream> #include <sstream>
#include "Stopwatch.hpp" #include "Stopwatch.hpp"
#include "../Headers/Problem.hpp" #include "Problems/Problem6.hpp"
#include "../Headers/Problem6.hpp"
int Problem6::START_NUM = 1; //The first number to check int Problem6::START_NUM = 1; //The first number to check
@@ -58,19 +57,25 @@ void Problem6::solve(){
//Stop the timer //Stop the timer
timer.stop(); 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 //Throw a flag to show the problem is solved
solved = true; solved = true;
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
void Problem6::reset(){ void Problem6::reset(){
Problem::reset(); Problem::reset();
sumOfSquares = squareOfSum = 0; sumOfSquares = squareOfSum = 0;
} }
//Gets
//Return a string with the solution to the problem
std::string Problem6::getResult(){
if(!solved){
throw Unsolved();
}
std::stringstream result;
result << "The difference between the sum of the squares and the square of the sum of all numbers from 1-100 is " << abs(sumOfSquares - squareOfSum);
return result.str();
}
//Returns the sum of all the squares //Returns the sum of all the squares
uint64_t Problem6::getSumOfSquares() const{ uint64_t Problem6::getSumOfSquares() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -79,7 +84,6 @@ uint64_t Problem6::getSumOfSquares() const{
} }
return sumOfSquares; return sumOfSquares;
} }
//Returns the square of all of the sums //Returns the square of all of the sums
uint64_t Problem6::getSquareOfSum() const{ uint64_t Problem6::getSquareOfSum() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -88,7 +92,6 @@ uint64_t Problem6::getSquareOfSum() const{
} }
return squareOfSum; return squareOfSum;
} }
//Returns the requested difference //Returns the requested difference
uint64_t Problem6::getDifference() const{ uint64_t Problem6::getDifference() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem67.cpp //ProjectEuler/ProjectEulerCPP/Source/Problem67.cpp
//Matthew Ellison //Matthew Ellison
// Created: 11-02-18 // Created: 11-02-18
//Modified: 07-09-20 //Modified: 08-28-20
//The way to do this is using a breadth first search //The way to do this is using a breadth first search
/* /*
Find the maximum total from top to bottom Find the maximum total from top to bottom
@@ -126,7 +126,7 @@ Find the maximum total from top to bottom
#include <vector> #include <vector>
#include "../Headers/Problem67.hpp" #include "Problems/Problem67.hpp"
//This is the list you are trying to find a path through //This is the list you are trying to find a path through

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem7.cpp //ProjectEuler/ProjectEulerCPP/Source/Problem7.cpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // Created: 09-28-18
//Modified: 07-09-20 //Modified: 08-28-20
//What is the 10001th prime number? //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 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
/* /*
@@ -28,8 +28,7 @@
#include <sstream> #include <sstream>
#include "Stopwatch.hpp" #include "Stopwatch.hpp"
#include "Algorithms.hpp" #include "Algorithms.hpp"
#include "../Headers/Problem.hpp" #include "Problems/Problem7.hpp"
#include "../Headers/Problem7.hpp"
//The index of the prime number to find //The index of the prime number to find
@@ -54,18 +53,24 @@ void Problem7::solve(){
//Stop the timer //Stop the timer
timer.stop(); 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 //Throw a flag to show the problem is solved
solved = true; solved = true;
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
void Problem7::reset(){ void Problem7::reset(){
Problem::reset(); Problem::reset();
} }
//Gets
//Return a string with the solution to the problem
std::string Problem7::getResult(){
if(!solved){
throw Unsolved();
}
std::stringstream result;
result << "The " << NUMBER_OF_PRIMES << "th prime number is " << primes.at(primes.size() - 1);
return result.str();
}
//Returns the requested prime number //Returns the requested prime number
uint64_t Problem7::getPrime() const{ uint64_t Problem7::getPrime() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem8.cpp //ProjectEuler/ProjectEulerCPP/Source/Problem8.cpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // 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? //Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
/* /*
73167176531330624919225119674426574742355349194934 73167176531330624919225119674426574742355349194934
@@ -49,8 +49,7 @@
#include <vector> #include <vector>
#include <sstream> #include <sstream>
#include "Stopwatch.hpp" #include "Stopwatch.hpp"
#include "../Headers/Problem.hpp" #include "Problems/Problem8.hpp"
#include "../Headers/Problem8.hpp"
//The number that we are working with //The number that we are working with
@@ -84,14 +83,9 @@ void Problem8::solve(){
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
//Save the results
result << "The greatest product is " << maxProduct
<< "\nThe numbers are " << maxNums;
//Throw a flag to show the problem is solved //Throw a flag to show the problem is solved
solved = true; solved = true;
} }
//Reset the problem so it can be run //Reset the problem so it can be run
void Problem8::reset(){ void Problem8::reset(){
Problem::reset(); Problem::reset();
@@ -99,6 +93,17 @@ void Problem8::reset(){
maxProduct = 0; maxProduct = 0;
} }
//Gets
//Return a string with the solution to the problem
std::string Problem8::getResult(){
if(!solved){
throw Unsolved();
}
std::stringstream result;
result << "The greatest product is " << maxProduct
<< "\nThe numbers are " << maxNums;
return result.str();
}
//Returns the string of numbers that produces the largest product //Returns the string of numbers that produces the largest product
std::string Problem8::getLargestNums() const{ std::string Problem8::getLargestNums() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -107,7 +112,6 @@ std::string Problem8::getLargestNums() const{
} }
return maxNums; return maxNums;
} }
//Returns the requested product //Returns the requested product
uint64_t Problem8::getLargestProduct() const{ uint64_t Problem8::getLargestProduct() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem9.cpp //ProjectEuler/ProjectEulerCPP/Source/Problem9.cpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // 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. //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 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
/* /*
@@ -26,8 +26,7 @@
#include <string> #include <string>
#include <sstream> #include <sstream>
#include "Stopwatch.hpp" #include "Stopwatch.hpp"
#include "../Headers/Problem.hpp" #include "Problems/Problem9.hpp"
#include "../Headers/Problem9.hpp"
//Constructor //Constructor
@@ -68,17 +67,15 @@ void Problem9::solve(){
//Save the results //Save the results
if(found){ if(found){
result << "The Pythagorean triplet is " << a << ' ' << b << ' ' << (int)c solved = true;
<< "\nThe numbers' product is " << a * b * (int)c;
} }
else{ else{
result << "The number was not found!"; //TODO: Throw an exception
} }
//Throw a flag to show the problem is solved //Throw a flag to show the problem is solved
solved = true;
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
void Problem9::reset(){ void Problem9::reset(){
Problem::reset(); Problem::reset();
@@ -88,6 +85,17 @@ void Problem9::reset(){
found = false; found = false;
} }
//Gets
//Return a string with the solution to the problem
std::string Problem9::getResult(){
if(!solved){
throw Unsolved();
}
std::stringstream result;
result << "The Pythagorean triplet is " << a << ' ' << b << ' ' << (int)c
<< "\nThe numbers' product is " << a * b * (int)c;
return result.str();
}
//Returns the length of the first side //Returns the length of the first side
int Problem9::getSideA() const{ int Problem9::getSideA() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -96,7 +104,6 @@ int Problem9::getSideA() const{
} }
return a; return a;
} }
//Returns the length of the second side //Returns the length of the second side
int Problem9::getSideB() const{ int Problem9::getSideB() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -105,7 +112,6 @@ int Problem9::getSideB() const{
} }
return b; return b;
} }
//Returns the length of the hyp //Returns the length of the hyp
int Problem9::getSideC() const{ int Problem9::getSideC() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
@@ -114,7 +120,6 @@ int Problem9::getSideC() const{
} }
return (int)c; return (int)c;
} }
//Returns the product of the 3 sides //Returns the product of the 3 sides
int Problem9::getProduct() const{ int Problem9::getProduct() const{
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception

View File

@@ -26,7 +26,7 @@
#include <vector> #include <vector>
#include "benchmark.hpp" #include "benchmark.hpp"
#include "Algorithms.hpp" #include "Algorithms.hpp"
#include "Headers/Problem.hpp" #include "Problem.hpp"
#include "ProblemSelection.hpp" #include "ProblemSelection.hpp"