Updated to work with the new libraries

This commit is contained in:
2021-07-03 02:38:00 -04:00
parent fd54eb90d8
commit 9660fe9287
82 changed files with 794 additions and 1008 deletions

View File

@@ -1,10 +1,10 @@
//ProjectEuler/ProjectEulerCPP/headers/Problem.hpp //ProjectEuler/ProjectEulerCPP/headers/Problem.hpp
//Matthew Ellison //Matthew Ellison
// Created: 07-04-19 // Created: 07-04-19
//Modified: 08-28-20 //Modified: 07-02-21
//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) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -19,14 +19,13 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM_HPP #ifndef PROBLEM_HPP
#define PROBLEM_HPP #define PROBLEM_HPP
#include <sstream> #include <sstream>
#include <string> #include <string>
#include "Stopwatch.hpp" #include "mee/Stopwatch.hpp"
#include "Unsolved.hpp" #include "Unsolved.hpp"
@@ -69,11 +68,16 @@ public:
timer.reset(); timer.reset();
solved = false; solved = false;
} }
void solvedCheck(std::string str) const{
if(!solved){
throw new Unsolved("You must solve the problem before you can see the " + str);
}
}
//Pure virtual functions //Pure virtual functions
//Solve the problem //Solve the problem
virtual void solve() = 0; virtual void solve() = 0;
//Return a string with the solution to the problem //Return a string with the solution to the problem
virtual std::string getResult() = 0; virtual std::string getResult() const = 0;
}; };
#endif //PROBLEM_HPP #endif //PROBLEM_HPP

View File

@@ -1,10 +1,10 @@
//ProjectEuler/ProjectEulerCPP/headers/ProblemSelection.hpp //ProjectEuler/ProjectEulerCPP/headers/ProblemSelection.hpp
//Matthew Ellison //Matthew Ellison
// Created: 07-08-20 // Created: 07-08-20
//Modified: 08-28-20 //Modified: 07-02-21
//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) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -19,13 +19,12 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEMSELECTION_HPP #ifndef PROBLEMSELECTION_HPP
#define PROBLEMSELECTION_HPP #define PROBLEMSELECTION_HPP
#include <iostream> #include <iostream>
#include "Algorithms.hpp" #include "mee/vectorAlgorithms.hpp"
#include "Problems/Problem1.hpp" #include "Problems/Problem1.hpp"
#include "Problems/Problem2.hpp" #include "Problems/Problem2.hpp"
#include "Problems/Problem3.hpp" #include "Problems/Problem3.hpp"
@@ -156,4 +155,5 @@ void listProblems(){
std::cout << std::endl; std::cout << std::endl;
} }
#endif //PROBLEMSELECTION_HPP #endif //PROBLEMSELECTION_HPP

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem1.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem1.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // Created: 09-28-18
//Modified: 10-26-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -20,12 +20,10 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM1_HPP #ifndef PROBLEM1_HPP
#define PROBLEM1_HPP #define PROBLEM1_HPP
#include <vector>
#include <cinttypes> #include <cinttypes>
#include <string> #include <string>
#include "Problem.hpp" #include "Problem.hpp"
@@ -46,13 +44,15 @@ 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 virtual std::string getResult() const; //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
}; };
/* Results: /* Results:
The sum of all the numbers < 1000 that are divisible by 3 or 5 is 233168 The sum of all the numbers < 1000 that are divisible by 3 or 5 is 233168
It took an average of 50.000 nanoseconds to run this problem over 100 iterations It took an average of 50.000 nanoseconds to run this problem over 100 iterations
*/ */
#endif //PROBLEM1_HPP
#endif //PROBLEM1_HPP

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem10.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem10.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // Created: 09-28-18
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -20,7 +20,6 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM10_HPP #ifndef PROBLEM10_HPP
#define PROBLEM10_HPP #define PROBLEM10_HPP
@@ -44,13 +43,15 @@ 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 virtual std::string getResult() const; //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
}; };
/* Results: /* Results:
The sum of all the primes less than 2000000 is 142913828922 The sum of all the primes less than 2000000 is 142913828922
It took an average of 171.141 milliseconds to run this problem over 100 iterations It took an average of 171.141 milliseconds to run this problem over 100 iterations
*/ */
#endif //PROBLEM10_HPP
#endif //PROBLEM10_HPP

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem11.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem11.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-29-18 // Created: 09-29-18
//Modified: 08-28-20 //Modified: 07-02-21
//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
@@ -27,7 +27,7 @@
*/ */
//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) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -42,7 +42,6 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM11_HPP #ifndef PROBLEM11_HPP
#define PROBLEM11_HPP #define PROBLEM11_HPP
@@ -66,15 +65,17 @@ 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 virtual std::string getResult() const; //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
}; };
/* Results: /* Results:
The greatest product of 4 number in a line is 70600674 The greatest product of 4 number in a line is 70600674
The numbers are 89 94 97 87 The numbers are 89 94 97 87
It took an average of 11.201 microseconds to run this problem over 100 iterations It took an average of 11.201 microseconds to run this problem over 100 iterations
*/ */
#endif //PROBLEM11_HPP
#endif //PROBLEM11_HPP

View File

@@ -1,10 +1,11 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem12.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem12.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-27-18 // Created: 09-27-18
//Modified: 08-28-20 //Modified: 07-02-21
//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) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -19,14 +20,13 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM12_HPP #ifndef PROBLEM12_HPP
#define PROBLEM12_HPP #define PROBLEM12_HPP
#include <vector>
#include <cinttypes> #include <cinttypes>
#include <string> #include <string>
#include <vector>
#include "Problem.hpp" #include "Problem.hpp"
@@ -46,7 +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 virtual std::string getResult() const; //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
@@ -59,4 +59,5 @@ The triangular number 76576500 is a sum of all numbers >= 12375 and has 576 divi
It took an average of 280.536 milliseconds to run this problem over 100 iterations It took an average of 280.536 milliseconds to run this problem over 100 iterations
*/ */
#endif //PROBLEM12_HPP
#endif //PROBLEM12_HPP

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem13.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem13.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-29-18 // Created: 09-29-18
//Modified: 08-28-20 //Modified: 07-02-21
//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
@@ -110,7 +110,7 @@
//You can find more information about them at https://gmplib.org/ //You can find more information about them at https://gmplib.org/
//When compiling this file you need to have the gmp library installed as well as linking the libraries to your executable using the -lgmpxx and -lgmp flags //When compiling this file you need to have the gmp library installed as well as linking the libraries to your executable using the -lgmpxx and -lgmp flags
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -125,15 +125,13 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM13_HPP #ifndef PROBLEM13_HPP
#define PROBLEM13_HPP #define PROBLEM13_HPP
#include <cinttypes>
#include <vector> #include <vector>
#include <string> #include <string>
#include "gmpxx.h" //This is part of the gmp library #include <gmpxx.h>
#include "Problem.hpp" #include "Problem.hpp"
@@ -154,7 +152,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 virtual std::string getResult() const; //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
}; };
@@ -166,4 +164,5 @@ The first 10 digits of the sum of the numbers is 5537376230
It took an average of 13.270 microseconds to run this problem over 100 iterations It took an average of 13.270 microseconds to run this problem over 100 iterations
*/ */
#endif //PROBLEM13_HPP
#endif //PROBLEM13_HPP

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem14.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem14.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-29-18 // Created: 09-29-18
//Modified: 08-28-20 //Modified: 07-02-21
/* /*
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)
@@ -10,7 +10,7 @@ Which starting number, under one million, produces the longest chain?
*/ */
//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) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -25,7 +25,6 @@ Which starting number, under one million, produces the longest chain?
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM14_HPP #ifndef PROBLEM14_HPP
#define PROBLEM14_HPP #define PROBLEM14_HPP
@@ -53,14 +52,16 @@ 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 virtual std::string getResult() const; //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
}; };
/* Results: /* Results:
The number 837799 produced a chain of 525 steps The number 837799 produced a chain of 525 steps
It took an average of 197.008 milliseconds to run this problem over 100 iterations It took an average of 197.008 milliseconds to run this problem over 100 iterations
*/ */
#endif //PROBLEM14_HPP
#endif //PROBLEM14_HPP

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem15.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem15.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-29-18 // Created: 09-29-18
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -50,12 +50,15 @@ 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 virtual std::string getResult() const; //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:
The number of routes is 137846528820 The number of routes is 137846528820
It took an average of 18.010 minutes to run this problem through 10 iterations It took an average of 18.010 minutes to run this problem through 10 iterations
*/ */
#endif //PROBLEM15_HPP
#endif //PROBLEM15_HPP

View File

@@ -1,14 +1,14 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem16.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem16.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // Created: 09-28-18
//Modified: 08-28-20 //Modified: 07-02-21
//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.
//You can find more information about them at https://gmplib.org/ //You can find more information about them at https://gmplib.org/
//When compiling this file you need to have the gmp library installed as well as linking the libraries to your executable using the -lgmpxx and -lgmp flags //When compiling this file you need to have the gmp library installed as well as linking the libraries to your executable using the -lgmpxx and -lgmp flags
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -23,7 +23,6 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM16_HPP #ifndef PROBLEM16_HPP
#define PROBLEM16_HPP #define PROBLEM16_HPP
@@ -49,15 +48,17 @@ 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 virtual std::string getResult() const; //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
}; };
/* Results: /* Results:
2^1000 = 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376 2^1000 = 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376
The sum of the elements is 1366 The sum of the elements is 1366
It took an average of 4.806 microseconds to run this problem over 100 iterations It took an average of 4.806 microseconds to run this problem over 100 iterations
*/ */
#endif //PROBLEM16_HPP
#endif //PROBLEM16_HPP

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem17.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem17.hpp
//Matthew Ellison //Matthew Ellison
// Created: 10-05-18 // Created: 10-05-18
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -50,12 +50,15 @@ 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 virtual std::string getResult() const; //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:
The number of letters is 21124 The number of letters is 21124
It took an average of 220.126 microseconds to run this problem over 100 iterations It took an average of 220.126 microseconds to run this problem over 100 iterations
*/ */
#endif //Problem17_HPP
#endif //Problem17_HPP

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem18.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem18.hpp
//Matthew Ellison //Matthew Ellison
// Created: 11-01-18 // Created: 11-01-18
//Modified: 08-28-20 //Modified: 07-02-21
//Find the maximum total from top to bottom //Find the maximum total from top to bottom
/* /*
75 75
@@ -23,7 +23,7 @@
//This is done using a breadth first search //This is done using a breadth first search
//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) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -38,14 +38,13 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM18_HPP #ifndef PROBLEM18_HPP
#define PROBLEM18_HPP #define PROBLEM18_HPP
#include <vector>
#include <list> #include <list>
#include <string> #include <string>
#include <vector>
#include "Problem.hpp" #include "Problem.hpp"
@@ -69,7 +68,7 @@ private:
//Functions //Functions
void invert(); //This list turns every number in the vector into 100 - num void invert(); //This list turns every number in the vector into 100 - num
void setupList(); void setupList(); //Setup the list of numbers
protected: protected:
//Variables //Variables
//Static variables //Static variables
@@ -81,10 +80,10 @@ 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 virtual std::string getResult() const; //Return a string with the solution to the problem
std::string getPyramid(); //Returns the pyramid that was traversed as a string std::string getPyramid() const; //Returns the pyramid that was traversed as a string
std::string getTrail(); //Returns the trail the algorithm took as a string std::string getTrail() const; //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
}; };
/* Results: /* Results:
@@ -92,4 +91,4 @@ The value of the longest path is 1074
It took an average of 9.925 microseconds to run this problem over 100 iterations It took an average of 9.925 microseconds to run this problem over 100 iterations
*/ */
#endif //PROBLEM18_HPP #endif //PROBLEM18_HPP

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem19.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem19.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // Created: 09-28-18
//Modified: 08-28-20 //Modified: 07-02-21
//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.
@@ -16,7 +16,7 @@ A leap year occurs on any year evenly divisible by 4, but not on a century unles
*/ */
//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) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -31,7 +31,6 @@ A leap year occurs on any year evenly divisible by 4, but not on a century unles
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM19_HPP #ifndef PROBLEM19_HPP
#define PROBLEM19_HPP #define PROBLEM19_HPP
@@ -62,12 +61,15 @@ 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 virtual std::string getResult() const; //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
There are 171 Sundays that landed on the first of the months from 1901 to 2000 There are 171 Sundays that landed on the first of the months from 1901 to 2000
It took an average of 1.400 milliseconds to run this problem over 100 iterations It took an average of 1.400 milliseconds to run this problem over 100 iterations
*/ */
#endif //PROBLEM19_HPP
#endif //PROBLEM19_HPP

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem2.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem2.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // Created: 09-28-18
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -20,11 +20,10 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM2_HPP #ifndef PROBLEM2_HPP
#define PROBLEM2_HPP #define PROBLEM2_HPP
#include <cinttypes> #include <cinttypes>
#include <string> #include <string>
#include "Problem.hpp" #include "Problem.hpp"
@@ -44,13 +43,15 @@ 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 virtual std::string getResult() const; //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
}; };
/* Results: /* Results:
The sum of the even Fibonacci numbers less than 4,000,000 is 4613732 The sum of the even Fibonacci numbers less than 4,000,000 is 4613732
It took an average of 324.000 nanoseconds to run this problem over 100 iterations It took an average of 324.000 nanoseconds to run this problem over 100 iterations
*/ */
#endif //PROBLEM2_HPP
#endif //PROBLEM2_HPP

View File

@@ -1,14 +1,14 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem20.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem20.hpp
//Matthew Ellison //Matthew Ellison
// Created: 11-07-18 // Created: 11-07-18
//Modified: 08-28-20 //Modified: 07-02-21
//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.
//You can find more information about them at https://gmplib.org/ //You can find more information about them at https://gmplib.org/
//When compiling this file you need to have the gmp library installed as well as linking the libraries to your executable using the -lgmpxx and -lgmp flags //When compiling this file you need to have the gmp library installed as well as linking the libraries to your executable using the -lgmpxx and -lgmp flags
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -23,7 +23,6 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM20_HPP #ifndef PROBLEM20_HPP
#define PROBLEM20_HPP #define PROBLEM20_HPP
@@ -47,16 +46,18 @@ 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 virtual std::string getResult() const; //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!
}; };
/* Results: /* Results:
100! = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 100! = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
The sum of the digits is: 648 The sum of the digits is: 648
It took an average of 4.094 microseconds to run this problem over 100 iterations It took an average of 4.094 microseconds to run this problem over 100 iterations
*/ */
#endif //PROBLEM20_HPP
#endif //PROBLEM20_HPP

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem21.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem21.hpp
//Matthew Ellison //Matthew Ellison
// Created: 11-08-18 // Created: 11-08-18
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -20,14 +20,13 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM21_HPP #ifndef PROBLEM21_HPP
#define PROBLEM21_HPP #define PROBLEM21_HPP
#include <cinttypes> #include <cinttypes>
#include <vector>
#include <string> #include <string>
#include <vector>
#include "Problem.hpp" #include "Problem.hpp"
@@ -49,7 +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 virtual std::string getResult() const; //Return a string with the solution to the problem
std::vector<uint64_t> getAmicable() const; //Returns a vector with all of the amicable numbers calculated 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
}; };
@@ -71,4 +70,5 @@ The sum of all of these amicable numbers is 31626
It took an average of 4.310 milliseconds to run this problem over 100 iterations It took an average of 4.310 milliseconds to run this problem over 100 iterations
*/ */
#endif //PROBLEM21_HPP
#endif //PROBLEM21_HPP

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem22.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem22.hpp
//Matthew Ellison //Matthew Ellison
// Created: 11-09-18 // Created: 11-09-18
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -20,7 +20,6 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM22_HPP #ifndef PROBLEM22_HPP
#define PROBLEM22_HPP #define PROBLEM22_HPP
@@ -50,14 +49,16 @@ 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 virtual std::string getResult() const; //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
}; };
/* Results: /* Results:
The answer to the question is 871198282 The answer to the question is 871198282
It took an average of 436.559 microseconds to run this problem over 100 iterations It took an average of 436.559 microseconds to run this problem over 100 iterations
*/ */
#endif //Problem22
#endif //Problem22

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem23.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem23.hpp
//Matthew Ellison //Matthew Ellison
// Created: 11-09-18 // Created: 11-09-18
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -20,14 +20,13 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM23_HPP #ifndef PROBLEM23_HPP
#define PROBLEM23_HPP #define PROBLEM23_HPP
#include <vector>
#include <cinttypes> #include <cinttypes>
#include <string> #include <string>
#include <vector>
#include "Problem.hpp" #include "Problem.hpp"
@@ -50,13 +49,15 @@ 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 virtual std::string getResult() const; //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
}; };
/* Results: /* Results:
The answer is 4179871 The answer is 4179871
It took an average of 5.902 seconds to run this problem over 100 iterations It took an average of 5.902 seconds to run this problem over 100 iterations
*/ */
#endif //PROBLEM23_HPP
#endif //PROBLEM23_HPP

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem24.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem24.hpp
//Matthew Ellison //Matthew Ellison
// Created: 11-11-18 // Created: 11-11-18
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -20,7 +20,6 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM24_HPP #ifndef PROBLEM24_HPP
#define PROBLEM24_HPP #define PROBLEM24_HPP
@@ -44,14 +43,16 @@ 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 virtual std::string getResult() const; //Return a string with the solution to the problem
std::vector<std::string> getPermutationsList() const; //Returns a vector with all of the permutations std::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
}; };
/* Results /* Results
The 1 millionth permutation is 2783915460 The 1 millionth permutation is 2783915460
It took an average of 1.157 seconds to run this problem over 100 iterations It took an average of 1.157 seconds to run this problem over 100 iterations
*/ */
#endif //PROBLEM24_HPP
#endif //PROBLEM24_HPP

View File

@@ -1,14 +1,14 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem25.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem25.hpp
//Matthew Ellison //Matthew Ellison
// Created: 11-13-18 // Created: 11-13-18
//Modified: 08-28-20 //Modified: 07-02-21
//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.
//You can find more information about them at https://gmplib.org/ //You can find more information about them at https://gmplib.org/
//When compiling this file you need to have the gmp library installed as well as linking the libraries to your executable using the -lgmpxx and -lgmp flags //When compiling this file you need to have the gmp library installed as well as linking the libraries to your executable using the -lgmpxx and -lgmp flags
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -23,13 +23,12 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM25_HPP #ifndef PROBLEM25_HPP
#define PROBLEM25_HPP #define PROBLEM25_HPP
#include <string> #include <string>
#include "gmpxx.h" #include <gmpxx.h>
#include "Problem.hpp" #include "Problem.hpp"
@@ -48,7 +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 virtual std::string getResult() const; //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
@@ -62,4 +61,4 @@ Its index is 4782
It took an average of 241.017 milliseconds to run this problem over 100 iterations It took an average of 241.017 milliseconds to run this problem over 100 iterations
*/ */
#endif //PROBLEM25_HPP #endif //PROBLEM25_HPP

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem26.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem26.hpp
//Matthew Ellison //Matthew Ellison
// Created: 07-28-19 // Created: 07-28-19
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -20,8 +20,6 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM26_HPP #ifndef PROBLEM26_HPP
#define PROBLEM26_HPP #define PROBLEM26_HPP
@@ -45,7 +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 virtual std::string getResult() const; //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
}; };
@@ -57,4 +55,5 @@ It is started with the number 983
It took an average of 9.989 milliseconds to run this problem over 100 iterations It took an average of 9.989 milliseconds to run this problem over 100 iterations
*/ */
#endif //PROBLEM26_HPP
#endif //PROBLEM26_HPP

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem27.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem27.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-14-19 // Created: 09-14-19
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -20,13 +20,12 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM27_HPP #ifndef PROBLEM27_HPP
#define PROBLEM27_HPP #define PROBLEM27_HPP
#include <string>
#include <cinttypes> #include <cinttypes>
#include <string>
#include <vector> #include <vector>
#include "Problem.hpp" #include "Problem.hpp"
@@ -46,12 +45,13 @@ 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 virtual std::string getResult() const; //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
}; };
/* Results: /* Results:
The greatest number of primes found is 70 The greatest number of primes found is 70
It was found with A = -61, B = 971 It was found with A = -61, B = 971
@@ -59,4 +59,5 @@ The product of A and B is -59231
It took an average of 14.261 milliseconds to run this problem over 100 iterations It took an average of 14.261 milliseconds to run this problem over 100 iterations
*/ */
#endif //PROBLEM27_HPP
#endif //PROBLEM27_HPP

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem28.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem28.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-21-19 // Created: 09-21-19
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -20,14 +20,12 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM28_HPP #ifndef PROBLEM28_HPP
#define PROBLEM28_HPP #define PROBLEM28_HPP
#include <string>
#include <cinttypes> #include <cinttypes>
#include <string>
#include <vector> #include <vector>
#include "Problem.hpp" #include "Problem.hpp"
@@ -50,14 +48,16 @@ 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 virtual std::string getResult() const; //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
}; };
/* Results: /* Results:
The sum of the diagonals in the given grid is 669171001 The sum of the diagonals in the given grid is 669171001
It took an average of 1.254 milliseconds to run this problem over 100 iterations It took an average of 1.254 milliseconds to run this problem over 100 iterations
*/ */
#endif //PROBLEM28_HPP
#endif //PROBLEM28_HPP

View File

@@ -1,14 +1,14 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem29.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem29.hpp
//Matthew Ellison //Matthew Ellison
// Created: 10-06-19 // Created: 10-06-19
//Modified: 08-28-20 //Modified: 07-02-21
//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.
//You can find more information about them at https://gmplib.org/ //You can find more information about them at https://gmplib.org/
//When compiling this file you need to have the gmp library installed as well as linking the libraries to your executable using the -lgmpxx and -lgmp flags //When compiling this file you need to have the gmp library installed as well as linking the libraries to your executable using the -lgmpxx and -lgmp flags
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -23,14 +23,12 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM29_HPP #ifndef PROBLEM29_HPP
#define PROBLEM29_HPP #define PROBLEM29_HPP
#include <string>
#include <cinttypes> #include <cinttypes>
#include <string>
#include <vector> #include <vector>
#include <gmpxx.h> #include <gmpxx.h>
#include "Problem.hpp" #include "Problem.hpp"
@@ -41,9 +39,9 @@ private:
//Variables //Variables
//Static variables //Static variables
static unsigned int BOTTOM_A; //The lowest possible value for a static unsigned int BOTTOM_A; //The lowest possible value for a
static unsigned int TOP_A; //The highest possible value for a static unsigned int TOP_A; //The highest possible value for a
static unsigned int BOTTOM_B; //The lowest possible value for b static unsigned int BOTTOM_B; //The lowest possible value for b
static unsigned int TOP_B; //The highest possible value for b static unsigned int TOP_B; //The highest possible value for b
//Instance variables //Instance variables
std::vector<mpz_class> unique; //Holds all values in powers, except repeats std::vector<mpz_class> unique; //Holds all values in powers, except repeats
public: public:
@@ -53,17 +51,20 @@ 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 virtual std::string getResult()const; //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
unsigned int getTopB() const; //Returns the highest possible value for b unsigned int getTopB() const; //Returns the highest possible value for b
std::vector<mpz_class> getUnique() const; //Returns a vector of all the unique values for a^b std::vector<mpz_class> getUnique() const; //Returns a vector of all the unique values for a^b
size_t getNumUnique() const; //Returns the number of unique values for a^b
}; };
/* Results: /* Results:
The number of unique values generated by a^b for 2 <= a <= 100 and 2 <= b <= 100 is 9183 The number of unique values generated by a^b for 2 <= a <= 100 and 2 <= b <= 100 is 9183
It took an average of 1.651 seconds to run this problem over 100 iterations It took an average of 1.651 seconds to run this problem over 100 iterations
*/ */
#endif //PROBLEM29_HPP
#endif //PROBLEM29_HPP

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem3.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem3.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // Created: 09-28-18
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -20,14 +20,13 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM3_HPP #ifndef PROBLEM3_HPP
#define PROBLEM3_HPP #define PROBLEM3_HPP
#include <vector>
#include <cinttypes> #include <cinttypes>
#include <string> #include <string>
#include <vector>
#include "Problem.hpp" #include "Problem.hpp"
@@ -45,15 +44,16 @@ 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 virtual std::string getResult() const; //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
}; };
/* Results: /* Results:
The largest factor of the number 600851475143 is 6857 The largest factor of the number 600851475143 is 6857
It took an average of 50.300 milliseconds to run this problem over 100 iterations It took an average of 50.300 milliseconds to run this problem over 100 iterations
*/ */
#endif //PROBLEM3_HPP
#endif //PROBLEM3_HPP

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem30.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem30.hpp
//Matthew Ellison //Matthew Ellison
// Created: 10-27-19 // Created: 10-27-19
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -20,13 +20,12 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM30_HPP #ifndef PROBLEM30_HPP
#define PROBLEM30_HPP #define PROBLEM30_HPP
#include <string>
#include <cinttypes> #include <cinttypes>
#include <string>
#include <vector> #include <vector>
#include "Problem.hpp" #include "Problem.hpp"
@@ -51,7 +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 virtual std::string getResult() const; //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,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem31.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem31.hpp
//Matthew Ellison //Matthew Ellison
// Created: 06-19-20 // Created: 06-19-20
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -20,13 +20,11 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM31_HPP #ifndef PROBLEM31_HPP
#define PROBLEM31_HPP #define PROBLEM31_HPP
#include <string> #include <string>
#include <vector>
#include "Problem.hpp" #include "Problem.hpp"
@@ -44,7 +42,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 virtual std::string getResult() const; //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/Problems/Problem32.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem32.hpp
//Matthew Ellison //Matthew Ellison
// Created: 07-27-20 // Created: 07-27-20
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
@@ -20,7 +20,6 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM32_HPP #ifndef PROBLEM32_HPP
#define PROBLEM32_HPP #define PROBLEM32_HPP
@@ -69,8 +68,8 @@ public:
void solve(); //Solve the problem void solve(); //Solve the problem
void reset(); //Reset the problem so it can be run again 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 virtual std::string getResult() const; //Return a string with the solution to the problem
int64_t getSumOfPandigitals(); //Returns the sum of the pandigitals int64_t getSumOfPandigitals() const; //Returns the sum of the pandigitals
}; };
/* Results: /* Results:

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem33.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem33.hpp
//Matthew Ellison //Matthew Ellison
// Created: 02-05-2021 // Created: 02-05-21
//Modified: 02-05-2021 //Modified: 07-03-21
/* /*
The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s
We shall consider fractions like, 30/50 = 3/5, to be trivial examples We shall consider fractions like, 30/50 = 3/5, to be trivial examples
@@ -25,12 +25,10 @@ If the product of these four fractions is given in its lowest common terms, find
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM33_HPP #ifndef PROBLEM33_HPP
#define PROBLEM33_HPP #define PROBLEM33_HPP
#include <cinttypes>
#include <string> #include <string>
#include <vector> #include <vector>
#include "Problem.hpp" #include "Problem.hpp"
@@ -40,14 +38,14 @@ class Problem33: public Problem{
private: private:
//Variables //Variables
//Static variables //Static variables
static int MIN_NUMERATOR; //The lowest the numerator can be static int MIN_NUMERATOR; //The lowest the numerator can be
static int MAX_NUMERATOR; //The highest the numerator can be static int MAX_NUMERATOR; //The highest the numerator can be
static int MIN_DENOMINATOR; //The lowest the denominator can be static int MIN_DENOMINATOR; //The lowest the denominator can be
static int MAX_DENOMINATOR; //The highest the denominator can be static int MAX_DENOMINATOR; //The highest the denominator can be
//Instance variables //Instance variables
std::vector<int> numerators; //Holds the numerators that were found std::vector<int> numerators; //Holds the numerators that were found
std::vector<int> denominators; //Holds the denominators that were found std::vector<int> denominators; //Holds the denominators that were found
int prodDenominator; //Holds the answer to the question int prodDenominator; //Holds the answer to the question
public: public:
//Constructor //Constructor
Problem33(); Problem33();
@@ -55,15 +53,17 @@ 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 virtual std::string getResult() const; //Return a string with the solution to the problem
std::vector<int> getNumerators(); //Returns the list of numerators std::vector<int> getNumerators() const; //Returns the list of numerators
std::vector<int> getDenominators(); //Returns the list of denominators std::vector<int> getDenominators() const; //Returns the list of denominators
int getProdDenominator(); //Returns the answer to the question int getProdDenominator() const; //Returns the answer to the question
}; };
/* Results: /* Results:
The denominator of the product is 100 The denominator of the product is 100
It took an average of 69.741 microseconds to run this problem over 100 iterations It took an average of 69.741 microseconds to run this problem over 100 iterations
*/ */
#endif //PROBLEM33_HPP
#endif //PROBLEM33_HPP

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem34.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem34.hpp
//Matthew Ellison //Matthew Ellison
// Created: 06-01-21 // Created: 06-01-21
//Modified: 06-01-21 //Modified: 07-02-21
//Find the sum of all numbers which are equal to the sum of the factorial of their digits //Find the sum of all numbers which are equal to the sum of the factorial 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
/* /*
@@ -20,7 +20,6 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM34_HPP #ifndef PROBLEM34_HPP
#define PROBLEM34_HPP #define PROBLEM34_HPP
@@ -45,14 +44,16 @@ 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 virtual std::string getResult() const; //Return a string with the solution to the problem
std::vector<int> getFactorials(); //Returns the list of factorials from 0-9 std::vector<int> getFactorials() const; //Returns the list of factorials from 0-9
int getSum(); //Returns the sum of all numbers equal to the sum of their digit's factorials int getSum() const; //Returns the sum of all numbers equal to the sum of their digit's factorials
}; };
/* Results: /* Results:
The sum of all numbers that are the sum of their digit's factorials is 40730 The sum of all numbers that are the sum of their digit's factorials is 40730
It took an average of 15.181 milliseconds to run this problem over 100 iterations It took an average of 15.181 milliseconds to run this problem over 100 iterations
*/ */
#endif //PROBLEM34_HPP
#endif //PROBLEM34_HPP

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem35.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem35.hpp
//Matthew Ellison //Matthew Ellison
// Created: 06-02-21 // Created: 06-02-21
//Modified: 06-02-21 //Modified: 07-02-21
//How many circular primes are there below one million? //How many circular primes are there below one 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
/* /*
@@ -20,7 +20,6 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM35_HPP #ifndef PROBLEM35_HPP
#define PROBLEM35_HPP #define PROBLEM35_HPP
@@ -47,15 +46,17 @@ 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(); //Returns a string with the solution to the problem virtual std::string getResult() const; //Returns a string with the solution to the problem
std::vector<int> getPrimes(); //Returns the vector of primes < MAX_NUM std::vector<int> getPrimes() const; //Returns the vector of primes < MAX_NUM
std::vector<int> getCircularPrimes(); //Returns the vector of circular primes < MAX_NUM std::vector<int> getCircularPrimes() const; //Returns the vector of circular primes < MAX_NUM
int getNumCircularPrimes(); //Returns the number of circular primes int getNumCircularPrimes() const; //Returns the number of circular primes
}; };
/* Results: /* Results:
The number of all circular prime numbers under 999999 is 55 The number of all circular prime numbers under 999999 is 55
It took an average of 2.618 seconds to run this problem over 100 iterations It took an average of 2.618 seconds to run this problem over 100 iterations
*/ */
#endif //PROBLEM35_HPP
#endif //PROBLEM35_HPP

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem36.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem36.hpp
//Matthew Ellison //Matthew Ellison
// Created: 06-29-21 // Created: 06-29-21
//Modified: 06-29-21 //Modified: 07-02-21
//Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2. //Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.
//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
/* /*
@@ -20,7 +20,6 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM36_HPP #ifndef PROBLEM36_HPP
#define PROBLEM36_HPP #define PROBLEM36_HPP
@@ -28,7 +27,6 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "Problem.hpp" #include "Problem.hpp"
#include "Algorithms.hpp"
class Problem36 : public Problem{ class Problem36 : public Problem{
@@ -47,11 +45,12 @@ 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(); //Returns a string with the solution to the problem virtual std::string getResult() const; //Returns a string with the solution to the problem
std::vector<int> getPalindromes(); //Return the vector of palindromes < MAX_NUM std::vector<int> getPalindromes() const; //Return the vector of palindromes < MAX_NUM
int getSumOfPalindromes(); //Return the sum of all elements in the vector of palindromes int getSumOfPalindromes() const; //Return the sum of all elements in the vector of palindromes
}; };
/* Results: /* Results:
The sum of all base 10 and base 2 palindromic numbers < 999999 is 872187 The sum of all base 10 and base 2 palindromic numbers < 999999 is 872187
It took an average of 22.578 milliseconds to run this problem over 100 iterations It took an average of 22.578 milliseconds to run this problem over 100 iterations

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem37.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem37.hpp
//Matthew Ellison //Matthew Ellison
// Created: 06-30-21 // Created: 06-30-21
//Modified: 06-30-21 //Modified: 07-02-21
//Find the sum of the only eleven primes that are both truncatable from left to right and right to left (2, 3, 5, and 7 are not counted). //Find the sum of the only eleven primes that are both truncatable from left to right and right to left (2, 3, 5, and 7 are not counted).
//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
/* /*
@@ -20,11 +20,11 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM37_HPP #ifndef PROBLEM37_HPP
#define PROBLEM37_HPP #define PROBLEM37_HPP
#include <cinttypes>
#include <string> #include <string>
#include <vector> #include <vector>
#include "Problem.hpp" #include "Problem.hpp"
@@ -46,11 +46,12 @@ 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(); //Returns a string with the solution to the problem virtual std::string getResult() const; //Returns a string with the solution to the problem
std::vector<uint64_t> getTruncatablePrimes(); //Returns the list of primes that can be truncated std::vector<uint64_t> getTruncatablePrimes() const; //Returns the list of primes that can be truncated
uint64_t getSumOfPrimes(); //Get the sum of all primes in truncPrimes uint64_t getSumOfPrimes() const; //Get the sum of all primes in truncPrimes
}; };
/* Results: /* Results:
The sum of all left and right truncatable primes is 748317 The sum of all left and right truncatable primes is 748317
It took an average of 63.831 milliseconds to run this problem over 100 iterations It took an average of 63.831 milliseconds to run this problem over 100 iterations

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem4.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem4.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // Created: 09-28-18
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -20,13 +20,13 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM4_HPP #ifndef PROBLEM4_HPP
#define PROBLEM4_HPP #define PROBLEM4_HPP
#include <vector> #include <cinttypes>
#include <string> #include <string>
#include <vector>
#include "Problem.hpp" #include "Problem.hpp"
@@ -45,7 +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 virtual std::string getResult() const; //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
}; };
@@ -56,4 +56,5 @@ The largest palindrome is 906609
It took an average of 36.525 milliseconds to run this problem over 100 iterations It took an average of 36.525 milliseconds to run this problem over 100 iterations
*/ */
#endif //PROBLEM4_HPP
#endif //PROBLEM4_HPP

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem5.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem5.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // Created: 09-28-18
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -20,13 +20,10 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM5_HPP #ifndef PROBLEM5_HPP
#define PROBLEM5_HPP #define PROBLEM5_HPP
#include <vector>
#include <cinttypes>
#include <string> #include <string>
#include "Problem.hpp" #include "Problem.hpp"
@@ -43,13 +40,15 @@ 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 virtual std::string getResult() const; //Return a string with the solution to the problem
int getNumber() const; //Returns the requested number int getNumber() const; //Returns the requested number
}; };
/* Results: /* Results:
The smallest positive number evenly divisible by all numbers 1-20 is 232792560 The smallest positive number evenly divisible by all numbers 1-20 is 232792560
It took an average of 1.928 seconds to run this problem over 100 iterations It took an average of 1.802 seconds to run this problem over 100 iterations
*/ */
#endif //PROBLEM5_HPP
#endif //PROBLEM5_HPP

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem6.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem6.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // Created: 09-28-18
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -20,7 +20,6 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM6_HPP #ifndef PROBLEM6_HPP
#define PROBLEM6_HPP #define PROBLEM6_HPP
@@ -46,15 +45,17 @@ 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 virtual std::string getResult() const; //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
}; };
/* Result
/* Result:
The difference between the sum of the squares and the square of the sum of all numbers from 1-100 is 25164150 The difference between the sum of the squares and the square of the sum of all numbers from 1-100 is 25164150
It took an average of 76.000 nanoseconds to run this problem over 100 iterations It took an average of 76.000 nanoseconds to run this problem over 100 iterations
*/ */
#endif //PROBLEM6_HPP
#endif //PROBLEM6_HPP

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/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-02-21
//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
@@ -108,7 +108,7 @@ Find the maximum total from top to bottom
*/ */
//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) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -123,11 +123,11 @@ Find the maximum total from top to bottom
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM67_HPP #ifndef PROBLEM67_HPP
#define PROBLEM67_HPP #define PROBLEM67_HPP
#include "Problem67.hpp"
#include "Problem18.hpp" #include "Problem18.hpp"
@@ -136,15 +136,14 @@ class Problem67 : public Problem18{
private: private:
void setupList(); void setupList();
public: public:
Problem67() : Problem18(){ Problem67();
list.clear();
setupList();
}
}; };
/* Results: /* Results:
The value of the longest path is 7273 The value of the longest path is 7273
It took an average of 366.373 milliseconds to run this problem over 100 iterations It took an average of 366.373 milliseconds to run this problem over 100 iterations
*/ */
#endif //PROBLEM67_HPP
#endif //PROBLEM67_HPP

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem7.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem7.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // Created: 09-28-18
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -20,14 +20,13 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM7_HPP #ifndef PROBLEM7_HPP
#define PROBLEM7_HPP #define PROBLEM7_HPP
#include <vector>
#include <cinttypes> #include <cinttypes>
#include <string> #include <string>
#include <vector>
#include "Problem.hpp" #include "Problem.hpp"
@@ -45,13 +44,15 @@ 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 virtual std::string getResult() const; //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
}; };
/* Results
/* Results:
The 10001th prime number is 104743 The 10001th prime number is 104743
It took an average of 3.864 milliseconds to run this problem over 100 iterations It took an average of 3.864 milliseconds to run this problem over 100 iterations
*/ */
#endif //PROBLEM7_HPP
#endif //PROBLEM7_HPP

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem8.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem8.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // Created: 09-28-18
//Modified: 08-28-20 //Modified: 07-02-21
//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
@@ -27,7 +27,7 @@
*/ */
//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) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -42,14 +42,12 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM8_HPP #ifndef PROBLEM8_HPP
#define PROBLEM8_HPP #define PROBLEM8_HPP
#include <cinttypes> #include <cinttypes>
#include <string> #include <string>
#include <vector>
#include "Problem.hpp" #include "Problem.hpp"
@@ -68,16 +66,17 @@ 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 virtual std::string getResult() const; //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
}; };
/* Results /* Results:
The greatest product is 23514624000 The greatest product is 23514624000
The numbers are 5576689664895 The numbers are 5576689664895
It took an average of 129.024 microseconds to run this problem over 100 iterations It took an average of 129.024 microseconds to run this problem over 100 iterations
*/ */
#endif //PROBLEM8_HPP
#endif //PROBLEM8_HPP

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem9.hpp //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem9.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // Created: 09-28-18
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -20,12 +20,10 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef PROBLEM9_HPP #ifndef PROBLEM9_HPP
#define PROBLEM9_HPP #define PROBLEM9_HPP
#include <cmath>
#include <string> #include <string>
#include "Problem.hpp" #include "Problem.hpp"
@@ -47,17 +45,19 @@ 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 virtual std::string getResult() const; //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
int getProduct() const; //Returns the product of the 3 sides int getProduct() const; //Returns the product of the 3 sides
}; };
/* Results: /* Results:
The Pythagorean triplet is 200 375 425 The Pythagorean triplet is 200 375 425
The numbers' product is 31875000 The numbers' product is 31875000
It took an average of 154.595 microseconds to run this problem over 100 iterations It took an average of 154.595 microseconds to run this problem over 100 iterations
*/ */
#endif //PROBLEM9_HPP
#endif //PROBLEM9_HPP

View File

@@ -1,10 +1,24 @@
//ProjectEuler/ProjectEulerCPP/headers/Unsolved.hpp //ProjectEuler/ProjectEulerCPP/headers/Unsolved.hpp
//Matthew Ellison //Matthew Ellison
// Created: 08-28-20 // Created: 08-28-20
//Modified: 08-28-20 //Modified: 07-01-21
//An exception class thrown if you try to access something before it has been solved //An exception class thrown if you try to access something before it has been solved
/*
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef UNSOLVED_HPP #ifndef UNSOLVED_HPP
#define UNSOLVED_HPP #define UNSOLVED_HPP

View File

@@ -1,10 +1,10 @@
//ProjectEuler/ProjectEulerCPP/headers/benchmark.hpp //ProjectEuler/ProjectEulerCPP/headers/benchmark.hpp
//Matthew Ellison //Matthew Ellison
// Created: 07-08-20 // Created: 07-08-20
//Modified: 08-28-20 //Modified: 07-01-21
//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) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -19,11 +19,10 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef BENCHMARK_HPP #ifndef BENCHMARK_HPP
#define BENCHMARK_HPP #define BENCHMARK_HPP
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include "ProblemSelection.hpp" #include "ProblemSelection.hpp"

View File

@@ -1,7 +1,7 @@
NUMCORES = $(shell grep -c "^processor" /proc/cpuinfo) NUMCORES = $(shell grep -c "^processor" /proc/cpuinfo)
NUMCORESWIN = ${NUMBER_OF_PROCESSORS} NUMCORESWIN = ${NUMBER_OF_PROCESSORS}
LIBFLAGS = -shared -std=c++17 -O3 -fPIC -Wall LIBFLAGS = -shared -std=c++20 -O3 -fPIC -Wall -fcoroutines
EXEFLAGS = -Wall -std=c++17 -O3 -Wl,-rpath,'$$ORIGIN/lib' EXEFLAGS = -Wall -std=c++20 -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 33 34 35 36 37 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 33 34 35 36 37 67
SOURCE_DIR = src SOURCE_DIR = src

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem1.cpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem1.cpp
//Matthew Ellison //Matthew Ellison
// Created: 07-10-19 // Created: 07-10-19
//Modified: 10-26-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -22,11 +22,11 @@
*/ */
#include <string>
#include <sstream>
#include <cinttypes> #include <cinttypes>
#include <vector>
#include <cmath> #include <cmath>
#include <sstream>
#include <string>
#include <vector>
#include "Problems/Problem1.hpp" #include "Problems/Problem1.hpp"
@@ -51,12 +51,15 @@ void Problem1::solve(){
if(solved){ if(solved){
return; return;
} }
//Start the timer //Start the timer
timer.start(); timer.start();
//Get the sum of the progressions of 3 and 5 and remove the sum of progressions of the overlap //Get the sum of the progressions of 3 and 5 and remove the sum of progressions of the overlap
fullSum = sumOfProgression(3) + sumOfProgression(5) - sumOfProgression(3 * 5); fullSum = sumOfProgression(3) + sumOfProgression(5) - sumOfProgression(3 * 5);
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
@@ -71,19 +74,14 @@ void Problem1::reset(){
//Gets //Gets
//Return a string with the solution to the problem //Return a string with the solution to the problem
std::string Problem1::getResult(){ std::string Problem1::getResult() const{
if(!solved){ Problem::solvedCheck("result");
throw Unsolved();
}
std::stringstream result; std::stringstream result;
result << "The sum of all the numbers < " << MAX_NUMBER + 1 << " that are divisible by 3 or 5 is " << fullSum; result << "The sum of all the numbers < " << MAX_NUMBER + 1 << " that are divisible by 3 or 5 is " << fullSum;
return result.str(); 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 Problem::solvedCheck("sum");
if(!solved){
throw Unsolved();
}
return fullSum; return fullSum;
} }

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem10.cpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem10.cpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // Created: 09-28-18
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -23,9 +23,10 @@
#include <cinttypes> #include <cinttypes>
#include <string>
#include <sstream> #include <sstream>
#include "Algorithms.hpp" #include <string>
#include "mee/numberAlgorithms.hpp"
#include "mee/vectorAlgorithms.hpp"
#include "Problems/Problem10.hpp" #include "Problems/Problem10.hpp"
@@ -42,12 +43,15 @@ void Problem10::solve(){
if(solved){ if(solved){
return; return;
} }
//Start the timer //Start the timer
timer.start(); timer.start();
//Get the sum of all prime numbers <= GOAL_NUMBER //Get the sum of all prime numbers <= GOAL_NUMBER
sum = mee::getSum(mee::getPrimes(GOAL_NUMBER)); sum = mee::getSum(mee::getPrimes(GOAL_NUMBER));
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
@@ -62,19 +66,14 @@ void Problem10::reset(){
//Gets //Gets
//Return a string with the solution to the problem //Return a string with the solution to the problem
std::string Problem10::getResult(){ std::string Problem10::getResult() const{
if(!solved){ solvedCheck("result");
throw Unsolved();
}
std::stringstream result; std::stringstream result;
result << "The sum of all the primes less than " << GOAL_NUMBER + 1 << " is " << sum; result << "The sum of all the primes less than " << GOAL_NUMBER + 1 << " is " << sum;
return result.str(); 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 solvedCheck("sum");
if(!solved){
throw Unsolved();
}
return sum; return sum;
} }

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem11.cpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem11.cpp
//Matthew Ellison //Matthew Ellison
// Created: 09-29-18 // Created: 09-29-18
//Modified: 08-28-20 //Modified: 07-02-21
//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
@@ -27,7 +27,7 @@
*/ */
//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) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -44,10 +44,10 @@
*/ */
#include <vector>
#include <string>
#include <sstream> #include <sstream>
#include "Algorithms.hpp" #include <string>
#include <vector>
#include "mee/vectorAlgorithms.hpp"
#include "Problems/Problem11.hpp" #include "Problems/Problem11.hpp"
@@ -93,6 +93,7 @@ void Problem11::solve(){
//Start the timer //Start the timer
timer.start(); timer.start();
//Loop through every row and column //Loop through every row and column
for(unsigned int row = 0;row < 20;++row){ for(unsigned int row = 0;row < 20;++row){
for(unsigned int col = 0;col < grid[row].size();++col){ for(unsigned int col = 0;col < grid[row].size();++col){
@@ -117,10 +118,10 @@ void Problem11::solve(){
//Right //Right
if(right){ if(right){
//Fill the product //Fill the product
currentProduct.at(0) = grid[row].at(col); currentProduct[0] = grid[row][col];
currentProduct.at(1) = grid[row].at(col + 1); currentProduct[1] = grid[row][col + 1];
currentProduct.at(2) = grid[row].at(col + 2); currentProduct[2] = grid[row][col + 2];
currentProduct.at(3) = grid[row].at(col + 3); currentProduct[3] = grid[row][col + 3];
//If the current numbers' product is greater than the greatest product replace it //If the current numbers' product is greater than the greatest product replace it
if(mee::getProduct(currentProduct) > mee::getProduct(greatestProduct)){ if(mee::getProduct(currentProduct) > mee::getProduct(greatestProduct)){
@@ -130,10 +131,10 @@ void Problem11::solve(){
//Down //Down
if(down){ if(down){
//Fill the product //Fill the product
currentProduct.at(0) = grid[row].at(col); currentProduct[0] = grid[row][col];
currentProduct.at(1) = grid[row + 1].at(col); currentProduct[1] = grid[row + 1][col];
currentProduct.at(2) = grid[row + 2].at(col); currentProduct[2] = grid[row + 2][col];
currentProduct.at(3) = grid[row + 3].at(col); currentProduct[3] = grid[row + 3][col];
//If the current numbers' product is greater than the greatest product replace it //If the current numbers' product is greater than the greatest product replace it
if(mee::getProduct(currentProduct) > mee::getProduct(greatestProduct)){ if(mee::getProduct(currentProduct) > mee::getProduct(greatestProduct)){
@@ -143,10 +144,10 @@ void Problem11::solve(){
//LeftDown //LeftDown
if(left && down){ if(left && down){
//Fill the product //Fill the product
currentProduct.at(0) = grid[row].at(col); currentProduct[0] = grid[row][col];
currentProduct.at(1) = grid[row + 1].at(col - 1); currentProduct[1] = grid[row + 1][col - 1];
currentProduct.at(2) = grid[row + 2].at(col - 2); currentProduct[2] = grid[row + 2][col - 2];
currentProduct.at(3) = grid[row + 3].at(col - 3); currentProduct[3] = grid[row + 3][col - 3];
//If the current numbers' product is greater than the greatest product replace it //If the current numbers' product is greater than the greatest product replace it
if(mee::getProduct(currentProduct) > mee::getProduct(greatestProduct)){ if(mee::getProduct(currentProduct) > mee::getProduct(greatestProduct)){
@@ -156,10 +157,10 @@ void Problem11::solve(){
//RightDown //RightDown
if(right && down){ if(right && down){
//Fill the product //Fill the product
currentProduct.at(0) = grid[row].at(col); currentProduct[0] = grid[row][col];
currentProduct.at(1) = grid[row + 1].at(col + 1); currentProduct[1] = grid[row + 1][col + 1];
currentProduct.at(2) = grid[row + 2].at(col + 2); currentProduct[2] = grid[row + 2][col + 2];
currentProduct.at(3) = grid[row + 3].at(col + 3); currentProduct[3] = grid[row + 3][col + 3];
//If the current numbers' product is greater than the greatest product replace it //If the current numbers' product is greater than the greatest product replace it
if(mee::getProduct(currentProduct) > mee::getProduct(greatestProduct)){ if(mee::getProduct(currentProduct) > mee::getProduct(greatestProduct)){
@@ -169,6 +170,7 @@ void Problem11::solve(){
} }
} }
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
@@ -183,28 +185,20 @@ void Problem11::reset(){
//Gets //Gets
//Return a string with the solution to the problem //Return a string with the solution to the problem
std::string Problem11::getResult(){ std::string Problem11::getResult() const{
if(!solved){ solvedCheck("result");
throw Unsolved();
}
std::stringstream result; std::stringstream result;
result << "The greatest product of 4 number in a line is " << mee::getProduct(greatestProduct) 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); << "\nThe numbers are " << greatestProduct[0] << ' ' << greatestProduct[1] << ' ' << greatestProduct[2] << ' ' << greatestProduct[3];
return result.str(); 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 solvedCheck("numbers");
if(!solved){
throw Unsolved();
}
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 solvedCheck("product of the numbers");
if(!solved){
throw Unsolved();
}
return mee::getProduct(greatestProduct); return mee::getProduct(greatestProduct);
} }

View File

@@ -1,10 +1,10 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem12.cpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem12.cpp
//Matthew Ellison //Matthew Ellison
// Created: 09-27-18 // Created: 09-27-18
//Modified: 08-28-20 //Modified: 07-02-21
//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) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -20,11 +20,12 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include <vector>
#include <cinttypes> #include <cinttypes>
#include <string>
#include <sstream> #include <sstream>
#include "Algorithms.hpp" #include <string>
#include <vector>
#include "mee/numberAlgorithms.hpp"
#include "Problems/Problem12.hpp" #include "Problems/Problem12.hpp"
@@ -48,6 +49,7 @@ void Problem12::solve(){
//Start the timer //Start the timer
timer.start(); timer.start();
while(!foundNumber){ while(!foundNumber){
divisors = mee::getDivisors(sum); divisors = mee::getDivisors(sum);
//If the number of divisors is correct set the flag. It must be > 500 //If the number of divisors is correct set the flag. It must be > 500
@@ -61,6 +63,7 @@ void Problem12::solve(){
} }
} }
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
@@ -77,43 +80,32 @@ void Problem12::reset(){
//Gets //Gets
//Return a string with the solution to the problem //Return a string with the solution to the problem
std::string Problem12::getResult(){ std::string Problem12::getResult() const{
if(!solved){ solvedCheck("result");
throw Unsolved();
}
std::stringstream result; std::stringstream result;
result << "The triangular number " << sum << " is a sum of all numbers >= " << counter - 1 << " and has " << divisors.size() << " divisors"; result << "The triangular number " << sum << " is a sum of all numbers >= " << counter - 1 << " and has " << divisors.size() << " divisors";
return result.str(); 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 solvedCheck("triangular number");
if(!solved){
throw Unsolved();
}
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
if(!solved){ if(!solved){
throw Unsolved(); throw Unsolved("You must solve the problem before you can see the last number added to get the triangular number");
} }
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 solvedCheck("divisors of the triangular number");
if(!solved){
throw Unsolved();
}
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 solvedCheck("number of divisors of the triangular number");
if(!solved){
throw Unsolved();
}
return divisors.size(); return divisors.size();
} }

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem13.cpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem13.cpp
//Matthew Ellison //Matthew Ellison
// Created: 09-29-18 // Created: 09-29-18
//Modified: 08-28-20 //Modified: 07-02-21
//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
@@ -110,7 +110,7 @@
//You can find more information about them at https://gmplib.org/ //You can find more information about them at https://gmplib.org/
//When compiling this file you need to have the gmp library installed as well as linking the libraries to your executable using the -lgmpxx and -lgmp flags //When compiling this file you need to have the gmp library installed as well as linking the libraries to your executable using the -lgmpxx and -lgmp flags
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -131,8 +131,8 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include <sstream> #include <sstream>
#include "gmpxx.h" //This is part of the gmp library #include <gmpxx.h> //This is part of the gmp library
#include "Algorithms.hpp" #include "mee/vectorAlgorithms.hpp"
#include "Problems/Problem13.hpp" #include "Problems/Problem13.hpp"
@@ -284,10 +284,8 @@ void Problem13::reset(){
//Gets //Gets
//Return a string with the solution to the problem //Return a string with the solution to the problem
std::string Problem13::getResult(){ std::string Problem13::getResult() const{
if(!solved){ solvedCheck("result");
throw Unsolved();
}
std::stringstream result; std::stringstream result;
result << "The sum of all " << nums.size() << " numbers is " << sum 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); << "\nThe first 10 digits of the sum of the numbers is " << sum.get_str().substr(0, 10);
@@ -295,17 +293,11 @@ std::string Problem13::getResult(){
} }
//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 solvedCheck("numbers");
if(!solved){
throw Unsolved();
}
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 solvedCheck("sum");
if(!solved){
throw Unsolved();
}
return sum; return sum;
} }

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem14.cpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem14.cpp
//Matthew Ellison //Matthew Ellison
// Created: 09-29-18 // Created: 09-29-18
//Modified: 08-28-20 //Modified: 07-02-21
/* /*
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)
@@ -10,7 +10,7 @@ Which starting number, under one million, produces the longest chain?
*/ */
//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) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -28,8 +28,8 @@ Which starting number, under one million, produces the longest chain?
#include <cinttypes> #include <cinttypes>
#include <string>
#include <sstream> #include <sstream>
#include <string>
#include "Problems/Problem14.hpp" #include "Problems/Problem14.hpp"
@@ -67,6 +67,7 @@ void Problem14::solve(){
//Start the timer //Start the timer
timer.start(); timer.start();
//Loop through all numbers less than 1000000 and check them agains the series //Loop through all numbers less than 1000000 and check them agains the series
for(uint64_t currentNum = 1;currentNum <= MAX_NUM;++currentNum){ for(uint64_t currentNum = 1;currentNum <= MAX_NUM;++currentNum){
uint64_t currentLength = checkSeries(currentNum); uint64_t currentLength = checkSeries(currentNum);
@@ -77,6 +78,7 @@ void Problem14::solve(){
} }
} }
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
@@ -92,27 +94,19 @@ void Problem14::reset(){
//Gets //Gets
//Return a string with the solution to the problem //Return a string with the solution to the problem
std::string Problem14::getResult(){ std::string Problem14::getResult() const{
if(!solved){ solvedCheck("result");
throw Unsolved();
}
std::stringstream result; std::stringstream result;
result << "The number " << maxNum << " produced a chain of " << maxLength << " steps"; result << "The number " << maxNum << " produced a chain of " << maxLength << " steps";
return result.str(); 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 solvedCheck("length of the longest chain");
if(!solved){
throw Unsolved();
}
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 solvedCheck("starting number of the longest chain");
if(!solved){
throw Unsolved();
}
return maxNum; return maxNum;
} }

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem15.hpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem15.hpp
//Matthew Ellison //Matthew Ellison
// Created: 09-29-18 // Created: 09-29-18
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -23,8 +23,8 @@
#include <cinttypes> #include <cinttypes>
#include <string>
#include <sstream> #include <sstream>
#include <string>
#include "Problems/Problem15.hpp" #include "Problems/Problem15.hpp"
@@ -69,10 +69,12 @@ void Problem15::solve(){
//Start the timer //Start the timer
timer.start(); timer.start();
//We write this as a recursive function //We write this as a recursive function
//When in a location it always moves right first, then down //When in a location it always moves right first, then down
move(currentX, currentY); move(currentX, currentY);
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
@@ -87,19 +89,14 @@ void Problem15::reset(){
//Gets //Gets
//Return a string with the solution to the problem //Return a string with the solution to the problem
std::string Problem15::getResult(){ std::string Problem15::getResult() const{
if(!solved){ solvedCheck("result");
throw Unsolved();
}
std::stringstream result; std::stringstream result;
result << "The number of routes is " << numOfRoutes; result << "The number of routes is " << numOfRoutes;
return result.str(); 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 solvedCheck("number of routes");
if(!solved){
throw Unsolved();
}
return numOfRoutes; return numOfRoutes;
} }

View File

@@ -1,14 +1,14 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem16.cpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem16.cpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // Created: 09-28-18
//Modified: 08-28-20 //Modified: 07-02-21
//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.
//You can find more information about them at https://gmplib.org/ //You can find more information about them at https://gmplib.org/
//When compiling this file you need to have the gmp library installed as well as linking the libraries to your executable using the -lgmpxx and -lgmp flags //When compiling this file you need to have the gmp library installed as well as linking the libraries to your executable using the -lgmpxx and -lgmp flags
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -24,8 +24,9 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include <string>
#include <sstream> #include <sstream>
#include <string>
#include <gmpxx.h> #include <gmpxx.h>
#include "Problems/Problem16.hpp" #include "Problems/Problem16.hpp"
@@ -47,12 +48,11 @@ void Problem16::solve(){
//Start the timer //Start the timer
timer.start(); timer.start();
//Get the number //Get the number
mpz_ui_pow_ui(num.get_mpz_t(), NUM_TO_POWER, POWER); mpz_ui_pow_ui(num.get_mpz_t(), NUM_TO_POWER, POWER);
//Get a string of the number //Get a string of the number
std::string numString = num.get_str(); std::string numString = num.get_str();
//Add up the individual characters of the string //Add up the individual characters of the string
for(char number : numString){ for(char number : numString){
std::string tempString; std::string tempString;
@@ -60,6 +60,7 @@ void Problem16::solve(){
sumOfElements += std::stoi(tempString); sumOfElements += std::stoi(tempString);
} }
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
@@ -75,10 +76,8 @@ void Problem16::reset(){
//Gets //Gets
//Return a string with the solution to the problem //Return a string with the solution to the problem
std::string Problem16::getResult(){ std::string Problem16::getResult() const{
if(!solved){ solvedCheck("result");
throw Unsolved();
}
std::stringstream result; std::stringstream result;
result << NUM_TO_POWER << '^' << POWER << " = " << num result << NUM_TO_POWER << '^' << POWER << " = " << num
<< "\nThe sum of the elements is " << sumOfElements; << "\nThe sum of the elements is " << sumOfElements;
@@ -86,17 +85,11 @@ std::string Problem16::getResult(){
} }
//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 solvedCheck("number");
if(!solved){
throw Unsolved();
}
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 solvedCheck("sum");
if(!solved){
throw Unsolved();
}
return sumOfElements; return sumOfElements;
} }

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem17.cpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem17.cpp
//Matthew Ellison //Matthew Ellison
// Created: 10-05-18 // Created: 10-05-18
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -23,9 +23,8 @@
#include <cinttypes> #include <cinttypes>
#include <string>
#include <sstream> #include <sstream>
#include "Stopwatch.hpp" #include <string>
#include "Problems/Problem17.hpp" #include "Problems/Problem17.hpp"
@@ -164,7 +163,7 @@ uint64_t Problem17::countLetters(std::string str){
uint64_t letterCount = 0; uint64_t letterCount = 0;
//Step through every character in the string and count how many letters there are //Step through every character in the string and count how many letters there are
for(unsigned int cnt = 0;cnt < str.size();++cnt){ for(unsigned int cnt = 0;cnt < str.size();++cnt){
if(isalpha(str.at(cnt))){ if(isalpha(str[cnt])){
++letterCount; ++letterCount;
} }
} }
@@ -185,12 +184,14 @@ void Problem17::solve(){
//Start the timer //Start the timer
timer.start(); timer.start();
//Step through every element in nums and get the word representations of the numbers //Step through every element in nums and get the word representations of the numbers
for(int cnt = START_NUM;cnt <= STOP_NUM;++cnt){ for(int cnt = START_NUM;cnt <= STOP_NUM;++cnt){
std::string words = makeWordFromNum(cnt); //Get the words of each number in turn std::string words = makeWordFromNum(cnt); //Get the words of each number in turn
letterCount += countLetters(words); //Add the number of letters to the running tally letterCount += countLetters(words); //Add the number of letters to the running tally
} }
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
@@ -205,19 +206,14 @@ void Problem17::reset(){
//Gets //Gets
//Return a string with the solution to the problem //Return a string with the solution to the problem
std::string Problem17::getResult(){ std::string Problem17::getResult() const{
if(!solved){ solvedCheck("result");
throw Unsolved();
}
std::stringstream result; std::stringstream result;
result << "The number of letters is " << letterCount; result << "The number of letters is " << letterCount;
return result.str(); 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 solvedCheck("letter count");
if(!solved){
throw Unsolved();
}
return letterCount; return letterCount;
} }

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem18.hpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem18.hpp
//Matthew Ellison //Matthew Ellison
// Created: 11-01-18 // Created: 11-01-18
//Modified: 08-28-20 //Modified: 07-02-21
//Find the maximum total from top to bottom //Find the maximum total from top to bottom
/* /*
75 75
@@ -23,7 +23,7 @@
//This is done using a breadth first search //This is done using a breadth first search
//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) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -40,10 +40,10 @@
*/ */
#include <vector>
#include <list> #include <list>
#include <string>
#include <sstream> #include <sstream>
#include <string>
#include <vector>
#include "Problems/Problem18.hpp" #include "Problems/Problem18.hpp"
@@ -92,6 +92,7 @@ void Problem18::solve(){
//Start the timer //Start the timer
timer.start(); timer.start();
//The method that I am using looks for the smallest numbers, so I need to invert the numbers in this list //The method that I am using looks for the smallest numbers, so I need to invert the numbers in this list
invert(); invert();
//Now l[i][j] == 100 - l[i][j]; //Now l[i][j] == 100 - l[i][j];
@@ -135,6 +136,7 @@ void Problem18::solve(){
actualTotal = ((100 * list.size()) - foundPoints.back().total); //Change the minimum to the maximum actualTotal = ((100 * list.size()) - foundPoints.back().total); //Change the minimum to the maximum
invert(); //Invert the list again so that it is back to it's original form invert(); //Invert the list again so that it is back to it's original form
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
@@ -151,22 +153,16 @@ void Problem18::reset(){
//Gets //Gets
//Return a string with the solution to the problem //Return a string with the solution to the problem
std::string Problem18::getResult(){ std::string Problem18::getResult() const{
if(!solved){ solvedCheck("result");
throw Unsolved();
}
std::stringstream result; std::stringstream result;
result << "The value of the longest path is " << actualTotal; result << "The value of the longest path is " << actualTotal;
return result.str(); 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() const{
//If the problem hasn't been solved throw an exception solvedCheck("pyramid of numbers");
if(!solved){
throw Unsolved();
}
std::stringstream results; std::stringstream results;
//Print the triangle list of numbers //Print the triangle list of numbers
for(unsigned int rowCnt = 0;rowCnt < list.size();++rowCnt){ for(unsigned int rowCnt = 0;rowCnt < list.size();++rowCnt){
for(unsigned int colCnt = 0;colCnt < list[rowCnt].size();++colCnt){ for(unsigned int colCnt = 0;colCnt < list[rowCnt].size();++colCnt){
@@ -176,11 +172,8 @@ 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() const{
//If the problem hasn't been solved throw an exception solvedCheck("trail of the shortest path");
if(!solved){
throw Unsolved();
}
std::stringstream results; std::stringstream results;
//Print the trail the algorithm took //Print the trail the algorithm took
@@ -190,13 +183,13 @@ std::string Problem18::getTrail(){
while(!top){ while(!top){
bool found = false; bool found = false;
int loc = foundPoints.size() - 1; int loc = foundPoints.size() - 1;
std::list<location>::iterator toAdd = foundPoints.begin(); std::list<location>::const_iterator toAdd = foundPoints.begin();
while(!found){ while(!found){
if(loc < 0){ if(loc < 0){
results << "Error: Location < 0\n"; results << "Error: Location < 0\n";
exit(1); exit(1);
} }
std::list<location>::iterator it = foundPoints.begin(); std::list<location>::const_iterator it = foundPoints.begin();
std::advance(it, loc); std::advance(it, loc);
if(trail.front().fromRight){ if(trail.front().fromRight){
if((it->xLocation == trail.begin()->xLocation) && (it->yLocation == (trail.begin()->yLocation - 1))){ if((it->xLocation == trail.begin()->xLocation) && (it->yLocation == (trail.begin()->yLocation - 1))){
@@ -236,9 +229,6 @@ std::string Problem18::getTrail(){
} }
//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 solvedCheck("total");
if(!solved){
throw Unsolved();
}
return actualTotal; return actualTotal;
} }

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem19.cpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem19.cpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // Created: 09-28-18
//Modified: 08-28-20 //Modified: 07-02-21
//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.
@@ -16,7 +16,7 @@ A leap year occurs on any year evenly divisible by 4, but not on a century unles
*/ */
//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) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -34,8 +34,8 @@ A leap year occurs on any year evenly divisible by 4, but not on a century unles
#include <cinttypes> #include <cinttypes>
#include <string>
#include <sstream> #include <sstream>
#include <string>
#include "Problems/Problem19.hpp" #include "Problems/Problem19.hpp"
@@ -158,6 +158,7 @@ void Problem19::solve(){
//Start the timer //Start the timer
timer.start(); timer.start();
//Run for all years up to 2000 //Run for all years up to 2000
for(unsigned int year = START_YEAR;year <= END_YEAR;++year){ for(unsigned int year = START_YEAR;year <= END_YEAR;++year){
//Run for all months in the year //Run for all months in the year
@@ -172,6 +173,7 @@ void Problem19::solve(){
} }
} }
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
@@ -186,19 +188,14 @@ void Problem19::reset(){
//Gets //Gets
//Return a string with the solution to the problem //Return a string with the solution to the problem
std::string Problem19::getResult(){ std::string Problem19::getResult() const{
if(!solved){ solvedCheck("result");
throw Unsolved();
}
std::stringstream result; std::stringstream result;
result << "There are " << totalSundays << " Sundays that landed on the first of the months from " << START_YEAR << " to " << END_YEAR; result << "There are " << totalSundays << " Sundays that landed on the first of the months from " << START_YEAR << " to " << END_YEAR;
return result.str(); 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 solvedCheck("total number of sundays");
if(!solved){
throw Unsolved();
}
return totalSundays; return totalSundays;
} }

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem2.cpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem2.cpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // Created: 09-28-18
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -23,9 +23,9 @@
#include <cinttypes> #include <cinttypes>
#include <string>
#include <sstream> #include <sstream>
#include "Algorithms.hpp" #include <string>
#include "mee/numberAlgorithms.hpp"
#include "Problems/Problem2.hpp" #include "Problems/Problem2.hpp"
@@ -42,9 +42,11 @@ void Problem2::solve(){
if(solved){ if(solved){
return; return;
} }
//Start the timer //Start the timer
timer.start(); timer.start();
//Get all the Fibonacci numbers <= TOP_NUM //Get all the Fibonacci numbers <= TOP_NUM
std::vector<uint64_t> fibList = mee::getAllFib(TOP_NUM); std::vector<uint64_t> fibList = mee::getAllFib(TOP_NUM);
//Step through the sum of all the elements to find the even numbers and add them to the list //Step through the sum of all the elements to find the even numbers and add them to the list
@@ -56,6 +58,7 @@ void Problem2::solve(){
//Otherwise ignore it //Otherwise ignore it
} }
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
@@ -70,19 +73,14 @@ void Problem2::reset(){
//Gets //Gets
//Return a string with the solution to the problem //Return a string with the solution to the problem
std::string Problem2::getResult(){ std::string Problem2::getResult() const{
if(!solved){ solvedCheck("result");
throw Unsolved();
}
std::stringstream result; std::stringstream result;
result << "The sum of the even Fibonacci numbers less than 4,000,000 is " << fullSum; result << "The sum of the even Fibonacci numbers less than 4,000,000 is " << fullSum;
return result.str(); 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 solvedCheck("sum");
if(!solved){
throw Unsolved();
}
return fullSum; return fullSum;
} }

View File

@@ -1,14 +1,14 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem20.cpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem20.cpp
//Matthew Ellison //Matthew Ellison
// Created: 11-07-18 // Created: 11-07-18
//Modified: 08-28-20 //Modified: 07-02-21
//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.
//You can find more information about them at https://gmplib.org/ //You can find more information about them at https://gmplib.org/
//When compiling this file you need to have the gmp library installed as well as linking the libraries to your executable using the -lgmpxx and -lgmp flags //When compiling this file you need to have the gmp library installed as well as linking the libraries to your executable using the -lgmpxx and -lgmp flags
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -26,8 +26,8 @@
#include <cinttypes> #include <cinttypes>
#include <string>
#include <sstream> #include <sstream>
#include <string>
#include "gmpxx.h" #include "gmpxx.h"
#include "Problems/Problem20.hpp" #include "Problems/Problem20.hpp"
@@ -48,6 +48,7 @@ void Problem20::solve(){
//Start the timer //Start the timer
timer.start(); timer.start();
//Run through every number from 1 to 100 and multiply it by the current num //Run through every number from 1 to 100 and multiply it by the current num
for(int cnt = 1;cnt <= 100;++cnt){ for(int cnt = 1;cnt <= 100;++cnt){
num *= cnt; num *= cnt;
@@ -60,6 +61,7 @@ void Problem20::solve(){
sum += std::stoi(numString.substr(cnt, 1)); sum += std::stoi(numString.substr(cnt, 1));
} }
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
@@ -75,10 +77,8 @@ void Problem20::reset(){
//Gets //Gets
//Return a string with the solution to the problem //Return a string with the solution to the problem
std::string Problem20::getResult(){ std::string Problem20::getResult() const{
if(!solved){ solvedCheck("result");
throw Unsolved();
}
std::stringstream result; std::stringstream result;
result << "100! = " << num.get_str() result << "100! = " << num.get_str()
<< "\nThe sum of the digits is: " << sum; << "\nThe sum of the digits is: " << sum;
@@ -86,25 +86,16 @@ std::string Problem20::getResult(){
} }
//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 solvedCheck("number");
if(!solved){
throw Unsolved();
}
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 solvedCheck("number as a string");
if(!solved){
throw Unsolved();
}
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 solvedCheck("sum of the digits");
if(!solved){
throw Unsolved();
}
return sum; return sum;
} }

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem21.cpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem21.cpp
//Matthew Ellison //Matthew Ellison
// Created: 11-08-18 // Created: 11-08-18
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -26,7 +26,8 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include <sstream> #include <sstream>
#include "Algorithms.hpp" #include "mee/numberAlgorithms.hpp"
#include "mee/vectorAlgorithms.hpp"
#include "Problems/Problem21.hpp" #include "Problems/Problem21.hpp"
@@ -54,23 +55,24 @@ void Problem21::solve(){
//Start the timer //Start the timer
timer.start(); timer.start();
//Generate the divisors of all the numbers < 10000, get their sum, and add it to the list //Generate the divisors of all the numbers < 10000, get their sum, and add it to the list
for(int cnt = 1;cnt < LIMIT;++cnt){ for(int cnt = 1;cnt < LIMIT;++cnt){
std::vector<int> divisors = mee::getDivisors(cnt); //Get all the divisors of a number std::vector<int> divisors = mee::getDivisors(cnt); //Get all the divisors of a number
if(divisors.size() > 1){ if(divisors.size() > 1){
divisors.pop_back(); //Remove the last entry because it will be the number itself divisors.pop_back(); //Remove the last entry because it will be the number itself
} }
divisorSum.at(cnt) = mee::getSum(divisors); //Add the sum of the divisors to the vector divisorSum[cnt] = mee::getSum(divisors); //Add the sum of the divisors to the vector
} }
//Check every sum of divisors in the list for a matching sum //Check every sum of divisors in the list for a matching sum
for(uint64_t cnt = 1;cnt < divisorSum.size();++cnt){ for(uint64_t cnt = 1;cnt < divisorSum.size();++cnt){
uint64_t sum = divisorSum.at(cnt); uint64_t sum = divisorSum[cnt];
//If the sum is greater than the number of divisors then it is impossible to be amicable. Skip the number and continue //If the sum is greater than the number of divisors then it is impossible to be amicable. Skip the number and continue
if(sum >= divisorSum.size()){ if(sum >= divisorSum.size()){
continue; continue;
} }
//We know that divisorSum.at(cnt) == sum, so if divisorSum.at(sum) == cnt we found an amicable number //We know that divisorSum[cnt] == sum, so if divisorSum[sum] == cnt we found an amicable number
if(divisorSum.at(sum) == cnt){ if(divisorSum[sum] == cnt){
//A number can't be amicable with itself //A number can't be amicable with itself
if(sum == cnt){ if(sum == cnt){
continue; continue;
@@ -83,6 +85,7 @@ void Problem21::solve(){
//Sort the vector for neatness //Sort the vector for neatness
std::sort(amicable.begin(), amicable.end()); std::sort(amicable.begin(), amicable.end());
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
@@ -99,31 +102,23 @@ void Problem21::reset(){
//Gets //Gets
//Return a string with the solution to the problem //Return a string with the solution to the problem
std::string Problem21::getResult(){ std::string Problem21::getResult() const{
if(!solved){ solvedCheck("result");
throw Unsolved();
}
std::stringstream result; std::stringstream result;
result << "All amicable numbers less than 10000 are\n"; result << "All amicable numbers less than 10000 are\n";
for(unsigned int cnt = 0;cnt < amicable.size();++cnt){ for(unsigned int cnt = 0;cnt < amicable.size();++cnt){
result << amicable.at(cnt) << '\n'; result << amicable[cnt] << '\n';
} }
result << "The sum of all of these amicable numbers is " << mee::getSum(amicable); result << "The sum of all of these amicable numbers is " << mee::getSum(amicable);
return result.str(); 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 solvedCheck("amicable numbers");
if(!solved){
throw Unsolved();
}
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 solvedCheck("sum of the amicable numbers");
if(!solved){
throw Unsolved();
}
return mee::getSum(amicable); return mee::getSum(amicable);
} }

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem22.cpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem22.cpp
//Matthew Ellison //Matthew Ellison
// Created: 11-09-18 // Created: 11-09-18
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -27,7 +27,7 @@
#include <cinttypes> #include <cinttypes>
#include <algorithm> #include <algorithm>
#include <sstream> #include <sstream>
#include "Algorithms.hpp" #include "mee/vectorAlgorithms.hpp"
#include "Problems/Problem22.hpp" #include "Problems/Problem22.hpp"
@@ -426,6 +426,7 @@ void Problem22::solve(){
//Start the timer //Start the timer
timer.start(); timer.start();
//Sort all the names //Sort all the names
std::sort(names.begin(), names.end()); std::sort(names.begin(), names.end());
//Step through every name //Step through every name
@@ -433,16 +434,17 @@ void Problem22::solve(){
//Step through every character in the current name //Step through every character in the current name
for(unsigned int charCnt = 0;charCnt < names[nameCnt].size();++charCnt){ for(unsigned int charCnt = 0;charCnt < names[nameCnt].size();++charCnt){
//A = 65 so subtracting 64 means A = 1. This will only work correctly if all letters are capitalized //A = 65 so subtracting 64 means A = 1. This will only work correctly if all letters are capitalized
sums.at(nameCnt) += (names[nameCnt][charCnt] - 64); sums[nameCnt] += (names[nameCnt][charCnt] - 64);
} }
} }
//Get the product for all numbers //Get the product for all numbers
for(unsigned int cnt = 0;cnt < sums.size();++cnt){ for(unsigned int cnt = 0;cnt < sums.size();++cnt){
prod.at(cnt) = sums.at(cnt) * (cnt + 1); prod[cnt] = sums[cnt] * (cnt + 1);
} }
//Get the sum of the product of all numbers
sum = mee::getSum(prod); sum = mee::getSum(prod);
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
@@ -460,27 +462,19 @@ void Problem22::reset(){
//Gets //Gets
//Return a string with the solution to the problem //Return a string with the solution to the problem
std::string Problem22::getResult(){ std::string Problem22::getResult() const{
if(!solved){ solvedCheck("result");
throw Unsolved();
}
std::stringstream result; std::stringstream result;
result << "The answer to the question is " << sum; result << "The answer to the question is " << sum;
return result.str(); 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 solvedCheck("names");
if(!solved){
throw Unsolved();
}
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 solvedCheck("score of the names");
if(!solved){
throw Unsolved();
}
return mee::getSum(prod); return mee::getSum(prod);
} }

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem23.cpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem23.cpp
//Matthew Ellison //Matthew Ellison
// Created: 11-09-18 // Created: 11-09-18
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -27,7 +27,8 @@
#include <algorithm> #include <algorithm>
#include <string> #include <string>
#include <sstream> #include <sstream>
#include "Algorithms.hpp" #include "mee/numberAlgorithms.hpp"
#include "mee/vectorAlgorithms.hpp"
#include "Problems/Problem23.hpp" #include "Problems/Problem23.hpp"
@@ -41,7 +42,7 @@ bool Problem23::isSum(const std::vector<int>& abund, int num){
for(unsigned int firstNum = 0;firstNum < abund.size();++firstNum){ for(unsigned int firstNum = 0;firstNum < abund.size();++firstNum){
//Pick a number for the second part of the sum //Pick a number for the second part of the sum
for(unsigned int secondNum = firstNum;secondNum < abund.size();++secondNum){ for(unsigned int secondNum = firstNum;secondNum < abund.size();++secondNum){
sum = abund.at(firstNum) + abund.at(secondNum); sum = abund[firstNum] + abund[secondNum];
if(sum == num){ if(sum == num){
return true; return true;
} }
@@ -75,19 +76,20 @@ void Problem23::solve(){
//Start the timer //Start the timer
timer.start(); timer.start();
//Get the sum of the divisors of all numbers < MAX_NUM //Get the sum of the divisors of all numbers < MAX_NUM
for(int cnt = 1;cnt <= MAX_NUM;++cnt){ for(int cnt = 1;cnt <= MAX_NUM;++cnt){
std::vector<int> div = mee::getDivisors(cnt); std::vector<int> div = mee::getDivisors(cnt);
if(div.size() > 1){ if(div.size() > 1){
div.pop_back(); //Remove the last element, which is the number itself. This gives us the propper divisors div.pop_back(); //Remove the last element, which is the number itself. This gives us the propper divisors
} }
divisorSums.at(cnt) = mee::getSum(div); divisorSums[cnt] = mee::getSum(div);
} }
//Get the abundant numbers //Get the abundant numbers
std::vector<int> abund; std::vector<int> abund;
for(unsigned int cnt = 0;cnt < divisorSums.size();++cnt){ for(unsigned int cnt = 0;cnt < divisorSums.size();++cnt){
if(divisorSums.at(cnt) > cnt){ if(divisorSums[cnt] > cnt){
abund.push_back(cnt); abund.push_back(cnt);
} }
} }
@@ -99,6 +101,7 @@ void Problem23::solve(){
} }
} }
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
@@ -115,19 +118,14 @@ void Problem23::reset(){
//Gets //Gets
//Return a string with the solution to the problem //Return a string with the solution to the problem
std::string Problem23::getResult(){ std::string Problem23::getResult() const{
if(!solved){ solvedCheck("result");
throw Unsolved();
}
std::stringstream result; std::stringstream result;
result << "The answer is " << sum; result << "The answer is " << sum;
return result.str(); 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 solvedCheck("sum");
if(!solved){
throw Unsolved();
}
return sum; return sum;
} }

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem24.cpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem24.cpp
//Matthew Ellison //Matthew Ellison
// Created: 11-11-18 // Created: 11-11-18
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -25,7 +25,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <sstream> #include <sstream>
#include "Algorithms.hpp" #include "mee/stringAlgorithms.hpp"
#include "Problems/Problem24.hpp" #include "Problems/Problem24.hpp"
@@ -48,9 +48,11 @@ void Problem24::solve(){
//Start the timer //Start the timer
timer.start(); timer.start();
//Get all permutations of the string //Get all permutations of the string
permutations = mee::getPermutations(nums); permutations = mee::getPermutations(nums);
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
@@ -65,27 +67,19 @@ void Problem24::reset(){
//Gets //Gets
//Return a string with the solution to the problem //Return a string with the solution to the problem
std::string Problem24::getResult(){ std::string Problem24::getResult() const{
if(!solved){ solvedCheck("result");
throw Unsolved();
}
std::stringstream result; std::stringstream result;
result << "The 1 millionth permutation is " << permutations.at(NEEDED_PERM - 1); result << "The 1 millionth permutation is " << permutations[NEEDED_PERM - 1];
return result.str(); 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 solvedCheck("permutations");
if(!solved){
throw Unsolved();
}
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 solvedCheck("1,000,000th permutation");
if(!solved){ return permutations[NEEDED_PERM - 1];
throw Unsolved();
}
return permutations.at(NEEDED_PERM - 1);
} }

View File

@@ -1,14 +1,14 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem25.cpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem25.cpp
//Matthew Ellison //Matthew Ellison
// Created: 11-13-18 // Created: 11-13-18
//Modified: 08-28-20 //Modified: 07-02-21
//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.
//You can find more information about them at https://gmplib.org/ //You can find more information about them at https://gmplib.org/
//When compiling this file you need to have the gmp library installed as well as linking the libraries to your executable using the -lgmpxx and -lgmp flags //When compiling this file you need to have the gmp library installed as well as linking the libraries to your executable using the -lgmpxx and -lgmp flags
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -25,10 +25,10 @@
*/ */
#include <string>
#include <sstream> #include <sstream>
#include "gmpxx.h" #include <string>
#include "Algorithms.hpp" #include <gmpxx.h>
#include "mee/numberAlgorithms.hpp"
#include "Problems/Problem25.hpp" #include "Problems/Problem25.hpp"
@@ -51,12 +51,14 @@ void Problem25::solve(){
//Start the timer //Start the timer
timer.start(); timer.start();
//Move through all Fibonacci numbers until you reach the one with at least NUM_DIGITS digits //Move through all Fibonacci numbers until you reach the one with at least NUM_DIGITS digits
while(number.get_str().size() < NUM_DIGITS){ while(number.get_str().size() < NUM_DIGITS){
++index; //Increase the index number. Doing this at the beginning keeps the index correct at the end of the loop ++index; //Increase the index number. Doing this at the beginning keeps the index correct at the end of the loop
number = mee::getFib(index); //Calculate the number number = mee::getFib(index); //Calculate the number
} }
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
@@ -72,10 +74,8 @@ void Problem25::reset(){
//Gets //Gets
//Return a string with the solution to the problem //Return a string with the solution to the problem
std::string Problem25::getResult(){ std::string Problem25::getResult() const{
if(!solved){ solvedCheck("result");
throw Unsolved();
}
std::stringstream result; std::stringstream result;
result << "The first Fibonacci number with " << NUM_DIGITS << " digits is " << number result << "The first Fibonacci number with " << NUM_DIGITS << " digits is " << number
<< "\nIts index is " << index; << "\nIts index is " << index;
@@ -83,41 +83,26 @@ std::string Problem25::getResult(){
} }
//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 solvedCheck("fibonacci number");
if(!solved){
throw Unsolved();
}
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 solvedCheck("fibonacci number as a string");
if(!solved){
throw Unsolved();
}
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 solvedCheck("index of the fibonacci number");
if(!solved){
throw Unsolved();
}
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 solvedCheck("index of the fibonacci number as a string");
if(!solved){
throw Unsolved();
}
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 solvedCheck("index of the fibonacci number as an int");
if(!solved){
throw Unsolved();
}
return index.get_ui(); return index.get_ui();
} }

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem26.cpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem26.cpp
//Matthew Ellison //Matthew Ellison
// Created: 07-28-19 // Created: 07-28-19
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -25,7 +25,7 @@
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <vector> #include <vector>
#include "Algorithms.hpp" #include "mee/vectorAlgorithms.hpp"
#include "Problems/Problem26.hpp" #include "Problems/Problem26.hpp"
@@ -48,6 +48,7 @@ void Problem26::solve(){
//Start the timer //Start the timer
timer.start(); timer.start();
//Start with 1/2 and find out how long the longest cycle is by checking the remainders //Start with 1/2 and find out how long the longest cycle is by checking the remainders
//Loop through every number from 2-999 and use it for the denominator //Loop through every number from 2-999 and use it for the denominator
for(unsigned int denominator = 2;denominator <= TOP_NUMBER;++denominator){ for(unsigned int denominator = 2;denominator <= TOP_NUMBER;++denominator){
@@ -86,6 +87,7 @@ void Problem26::solve(){
} }
} }
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
@@ -101,10 +103,8 @@ void Problem26::reset(){
//Gets //Gets
//Return a string with the solution to the problem //Return a string with the solution to the problem
std::string Problem26::getResult(){ std::string Problem26::getResult() const{
if(!solved){ solvedCheck("result");
throw Unsolved();
}
std::stringstream result; std::stringstream result;
result << "The longest cycle is " << longestCycle << " digits long" result << "The longest cycle is " << longestCycle << " digits long"
<< "\nIt is started with the number " << longestNumber; << "\nIt is started with the number " << longestNumber;
@@ -112,17 +112,11 @@ std::string Problem26::getResult(){
} }
//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 solvedCheck("length of the longest cycle");
if(!solved){
throw Unsolved();
}
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 solvedCheck("denominator that starts the longest cycle");
if(!solved){
throw Unsolved();
}
return longestNumber; return longestNumber;
} }

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem27.cpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem27.cpp
//Matthew Ellison //Matthew Ellison
// Created: 09-14-19 // Created: 09-14-19
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -21,10 +21,11 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <cinttypes> #include <cinttypes>
#include "Algorithms.hpp" #include "mee/numberAlgorithms.hpp"
#include "Problems/Problem27.hpp" #include "Problems/Problem27.hpp"
@@ -43,6 +44,7 @@ void Problem27::solve(){
//Start the timer //Start the timer
timer.start(); timer.start();
//Start with the lowest possible A and check all possibilities after that //Start with the lowest possible A and check all possibilities after that
for(int64_t a = -999;a <= 999;++a){ for(int64_t a = -999;a <= 999;++a){
//Start with the lowest possible B and check all possibilities after that //Start with the lowest possible B and check all possibilities after that
@@ -65,6 +67,7 @@ void Problem27::solve(){
} }
} }
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
@@ -80,10 +83,8 @@ void Problem27::reset(){
//Gets //Gets
//Return a string with the solution to the problem //Return a string with the solution to the problem
std::string Problem27::getResult(){ std::string Problem27::getResult() const{
if(!solved){ solvedCheck("result");
throw Unsolved();
}
std::stringstream result; std::stringstream result;
result << "The greatest number of primes found is " << topN result << "The greatest number of primes found is " << topN
<< "\nIt was found with A = " << topA << ", B = " << topB << "\nIt was found with A = " << topA << ", B = " << topB
@@ -92,25 +93,16 @@ std::string Problem27::getResult(){
} }
//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 solvedCheck("largest A");
if(!solved){
throw Unsolved();
}
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 solvedCheck("largest B");
if(!solved){
throw Unsolved();
}
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 solvedCheck("largest N");
if(!solved){
throw Unsolved();
}
return topN; return topN;
} }

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem28.cpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem28.cpp
//Matthew Ellison //Matthew Ellison
// Created: 09-21-19 // Created: 09-21-19
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -22,10 +22,10 @@
*/ */
#include <string>
#include <cinttypes> #include <cinttypes>
#include <vector>
#include <sstream> #include <sstream>
#include <string>
#include <vector>
#include "Problems/Problem28.hpp" #include "Problems/Problem28.hpp"
@@ -35,7 +35,7 @@ void Problem28::setupGrid(){
for(int cnt = 0;cnt < 1001;++cnt){ for(int cnt = 0;cnt < 1001;++cnt){
grid.emplace_back(); grid.emplace_back();
for(int location = 0;location < 1001;++location){ for(int location = 0;location < 1001;++location){
grid.at(cnt).push_back(0); grid[cnt].push_back(0);
} }
} }
} }
@@ -47,33 +47,33 @@ void Problem28::createGrid(){
//Start with the middle location and set it correctly and advance the tracker to the next number //Start with the middle location and set it correctly and advance the tracker to the next number
int xLocation = 500; int xLocation = 500;
int yLocation = 500; int yLocation = 500;
grid.at(yLocation).at(xLocation) = currentNum++; grid[yLocation][xLocation] = currentNum++;
//Move right the first time //Move right the first time
++xLocation; ++xLocation;
//Move in a circular pattern until you reach the final location //Move in a circular pattern until you reach the final location
while(!finalLocation){ while(!finalLocation){
//Move down until you reach a blank location on the left //Move down until you reach a blank location on the left
while(grid.at(yLocation).at(xLocation - 1) != 0){ while(grid[yLocation][xLocation - 1] != 0){
grid.at(yLocation).at(xLocation) = currentNum++; grid[yLocation][xLocation] = currentNum++;
++yLocation; ++yLocation;
} }
//Move left until you reach a blank location above //Move left until you reach a blank location above
while(grid.at(yLocation - 1).at(xLocation) != 0){ while(grid[yLocation - 1][xLocation] != 0){
grid.at(yLocation).at(xLocation) = currentNum++; grid[yLocation][xLocation] = currentNum++;
--xLocation; --xLocation;
} }
//Move up until you reach a blank location to the right //Move up until you reach a blank location to the right
while(grid.at(yLocation).at(xLocation + 1) != 0){ while(grid[yLocation][xLocation + 1] != 0){
grid.at(yLocation).at(xLocation) = currentNum++; grid[yLocation][xLocation] = currentNum++;
--yLocation; --yLocation;
} }
//Move right until you reach a blank location below //Move right until you reach a blank location below
while(grid.at(yLocation + 1).at(xLocation) != 0){ while(grid[yLocation + 1][xLocation] != 0){
grid.at(yLocation).at(xLocation) = currentNum++; grid[yLocation][xLocation] = currentNum++;
++xLocation; ++xLocation;
//Check if you are at the final location and break the loop if you are //Check if you are at the final location and break the loop if you are
if(xLocation == (int)grid.size()){ if(xLocation == (int)grid.size()){
@@ -92,11 +92,11 @@ void Problem28::findSum(){
while(row < grid.size()){ while(row < grid.size()){
//This ensures the middle location is only counted once //This ensures the middle location is only counted once
if(leftSide == rightSide){ if(leftSide == rightSide){
sumOfDiagonals += grid.at(row).at(leftSide); sumOfDiagonals += grid[row][leftSide];
} }
else{ else{
sumOfDiagonals += grid.at(row).at(leftSide); sumOfDiagonals += grid[row][leftSide];
sumOfDiagonals += grid.at(row).at(rightSide); sumOfDiagonals += grid[row][rightSide];
} }
++row; ++row;
++leftSide; ++leftSide;
@@ -143,27 +143,19 @@ void Problem28::reset(){
//Gets //Gets
//Return a string with the solution to the problem //Return a string with the solution to the problem
std::string Problem28::getResult(){ std::string Problem28::getResult() const{
if(!solved){ solvedCheck("result");
throw Unsolved();
}
std::stringstream result; std::stringstream result;
result << "The sum of the diagonals in the given grid is " << sumOfDiagonals; result << "The sum of the diagonals in the given grid is " << sumOfDiagonals;
return result.str(); 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 solvedCheck("grid");
if(!solved){
throw Unsolved();
}
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 solvedCheck("sum");
if(!solved){
throw Unsolved();
}
return sumOfDiagonals; return sumOfDiagonals;
} }

View File

@@ -1,14 +1,14 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem29.cpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem29.cpp
//Matthew Ellison //Matthew Ellison
// Created: 10-06-19 // Created: 10-06-19
//Modified: 08-28-20 //Modified: 07-02-21
//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.
//You can find more information about them at https://gmplib.org/ //You can find more information about them at https://gmplib.org/
//When compiling this file you need to have the gmp library installed as well as linking the libraries to your executable using the -lgmpxx and -lgmp flags //When compiling this file you need to have the gmp library installed as well as linking the libraries to your executable using the -lgmpxx and -lgmp flags
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -24,14 +24,15 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include <string>
#include <sstream>
#include <cinttypes> #include <cinttypes>
#include <vector>
#include <cmath> #include <cmath>
#include <sstream>
#include <string>
#include <vector>
#include <gmpxx.h> #include <gmpxx.h>
#include "mee/vectorAlgorithms.hpp"
#include "Problems/Problem29.hpp" #include "Problems/Problem29.hpp"
#include "Algorithms.hpp"
//The lowest possible value for a //The lowest possible value for a
@@ -57,6 +58,7 @@ void Problem29::solve(){
//Start the timer //Start the timer
timer.start(); timer.start();
//Start with the first A and move towards the top //Start with the first A and move towards the top
for(unsigned long currentA = BOTTOM_A;currentA <= TOP_A;++currentA){ for(unsigned long currentA = BOTTOM_A;currentA <= TOP_A;++currentA){
//Start with the first B and move towards the top //Start with the first B and move towards the top
@@ -70,6 +72,7 @@ void Problem29::solve(){
} }
} }
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
@@ -84,51 +87,39 @@ void Problem29::reset(){
//Gets //Gets
//Return a string with the solution to the problem //Return a string with the solution to the problem
std::string Problem29::getResult(){ std::string Problem29::getResult() const{
if(!solved){ solvedCheck("result");
throw Unsolved();
}
std::stringstream result; 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(); 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(); 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 solvedCheck("lowest a");
if(!solved){
throw Unsolved();
}
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 solvedCheck("highest a");
if(!solved){
throw Unsolved();
}
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 solvedCheck("lowest b");
if(!solved){
throw Unsolved();
}
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 solvedCheck("highest b");
if(!solved){
throw Unsolved();
}
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 solvedCheck("the unique values for a^b");
if(!solved){
throw Unsolved();
}
return unique; return unique;
} }
//Returns the number of unique value for a^b
size_t Problem29::getNumUnique() const{
solvedCheck("the number of unique values for a^b");
return unique.size();
}

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem3.cpp //ProjectEuler/ProjectEulerCPP/Source/Problem3.cpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // Created: 09-28-18
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -22,11 +22,11 @@
*/ */
#include <vector>
#include <cinttypes> #include <cinttypes>
#include <string>
#include <sstream> #include <sstream>
#include "Algorithms.hpp" #include <string>
#include <vector>
#include "mee/numberAlgorithms.hpp"
#include "Problems/Problem3.hpp" #include "Problems/Problem3.hpp"
@@ -43,12 +43,15 @@ void Problem3::solve(){
if(solved){ if(solved){
return; return;
} }
//Start the timer //Start the timer
timer.start(); timer.start();
//Find the factors of GOAL_NUMBER //Find the factors of GOAL_NUMBER
factors = mee::getFactors(GOAL_NUMBER); factors = mee::getFactors(GOAL_NUMBER);
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
@@ -63,36 +66,19 @@ void Problem3::reset(){
//Gets //Gets
//Return a string with the solution to the problem //Return a string with the solution to the problem
std::string Problem3::getResult(){ std::string Problem3::getResult() const{
if(!solved){ solvedCheck("result");
throw Unsolved();
}
std::stringstream result; std::stringstream result;
result << "The largest factor of the number " << GOAL_NUMBER << " is " << factors[factors.size() - 1]; result << "The largest factor of the number " << GOAL_NUMBER << " is " << factors[factors.size() - 1];
return result.str(); 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 solvedCheck("factors");
if(!solved){
throw Unsolved();
}
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 solvedCheck("largest factor");
if(!solved){
throw Unsolved();
}
return *factors.end(); return *factors.end();
} }
//Returns the number
uint64_t Problem3::getGoalNumber() const{
//If the problem hasn't been solved throw an exception
if(!solved){
throw Unsolved();
}
return GOAL_NUMBER;
}

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem30.cpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem30.cpp
//Matthew Ellison //Matthew Ellison
// Created: 10-27-19 // Created: 10-27-19
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -21,12 +21,12 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include <string>
#include <sstream>
#include <cinttypes> #include <cinttypes>
#include <vector>
#include <cmath> #include <cmath>
#include <sstream>
#include <string>
#include <vector>
#include "mee/vectorAlgorithms.hpp"
#include "Problems/Problem30.hpp" #include "Problems/Problem30.hpp"
@@ -80,7 +80,7 @@ void Problem30::solve(){
} }
} }
sum = getSumOfList(); sum = mee::getSum(sumOfFifthNumbers);
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
@@ -96,43 +96,24 @@ void Problem30::reset(){
//Gets //Gets
//Return a string with the solution to the problem //Return a string with the solution to the problem
std::string Problem30::getResult(){ std::string Problem30::getResult() const{
if(!solved){ solvedCheck("result");
throw Unsolved();
}
std::stringstream result; 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 " << sum; result << "The sum of all the numbers that can be written as the sum of the fifth powers of their digits is " << sum;
return result.str(); 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 solvedCheck("largest number checked");
if(!solved){
throw Unsolved();
}
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 solvedCheck("list of all numbers that are the sum of the 5th power of their digits");
if(!solved){
throw Unsolved();
}
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 solvedCheck("sum of all numbers that are the sum of the 5th power of their digits");
if(!solved){
throw Unsolved();
}
uint64_t sum = 0;
for(uint64_t num : sumOfFifthNumbers){
sum += num;
}
return sum; return sum;
} }

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem31.cpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem31.cpp
//Matthew Ellison //Matthew Ellison
// Created: 06-19-20 // Created: 06-19-20
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -22,8 +22,8 @@
*/ */
#include <string>
#include <sstream> #include <sstream>
#include <string>
#include <vector> #include <vector>
#include "Problems/Problem31.hpp" #include "Problems/Problem31.hpp"
@@ -46,6 +46,7 @@ void Problem31::solve(){
//Start the timer //Start the timer
timer.start(); timer.start();
//Start with 200p and remove the necessary coins with each loop //Start with 200p and remove the necessary coins with each loop
for(int pound2 = desiredValue;pound2 >= 0;pound2 -= 200){ for(int pound2 = desiredValue;pound2 >= 0;pound2 -= 200){
for(int pound1 = pound2;pound1 >= 0;pound1 -= 100){ for(int pound1 = pound2;pound1 >= 0;pound1 -= 100){
@@ -78,19 +79,14 @@ void Problem31::reset(){
//Gets //Gets
//Return a string with the solution to the problem //Return a string with the solution to the problem
std::string Problem31::getResult(){ std::string Problem31::getResult() const{
if(!solved){ solvedCheck("result");
throw Unsolved();
}
std::stringstream result; std::stringstream result;
result << "There are " << permutations << " ways to make 2 pounds with the given denominations of coins"; result << "There are " << permutations << " ways to make 2 pounds with the given denominations of coins";
return result.str(); 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 solvedCheck("number of correct permutations of the coins");
if(!solved){
throw Unsolved();
}
return permutations; return permutations;
} }

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem32.cpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem32.cpp
//Matthew Ellison //Matthew Ellison
// Created: 07-27-20 // Created: 07-27-20
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -26,14 +26,13 @@
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <vector> #include <vector>
#include "Algorithms.hpp" #include "mee/stringAlgorithms.hpp"
#include "Problems/Problem32.hpp" #include "Problems/Problem32.hpp"
#include <iostream>
int Problem32::TOP_MULTIPLICAND = 99; //The largest multiplicand to check int Problem32::TOP_MULTIPLICAND = 99; //The largest multiplicand to check
int Problem32::TOP_MULTIPLIER = 4999; //The largest multiplier to check int Problem32::TOP_MULTIPLIER = 4999; //The largest multiplier to check
//Returns true if the passed productset is 1-9 pandigital //Returns true if the passed productset is 1-9 pandigital
bool Problem32::isPandigital(ProductSet currentSet){ bool Problem32::isPandigital(ProductSet currentSet){
//Get the numbers out of the object and put them into a string //Get the numbers out of the object and put them into a string
@@ -68,6 +67,7 @@ void Problem32::solve(){
//Start the timer //Start the timer
timer.start(); timer.start();
//Create the multiplicand and start working your way up //Create the multiplicand and start working your way up
for(int multiplicand = 1;multiplicand <= TOP_MULTIPLICAND;++multiplicand){ for(int multiplicand = 1;multiplicand <= TOP_MULTIPLICAND;++multiplicand){
//Run through all possible multipliers //Run through all possible multipliers
@@ -91,6 +91,7 @@ void Problem32::solve(){
sumOfPandigitals += prod.getProduct(); sumOfPandigitals += prod.getProduct();
} }
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
@@ -106,19 +107,14 @@ void Problem32::reset(){
//Gets //Gets
//Return a string with the solution to the problem //Return a string with the solution to the problem
std::string Problem32::getResult(){ std::string Problem32::getResult() const{
if(!solved){ solvedCheck("result");
throw Unsolved();
}
std::stringstream result; std::stringstream result;
result << "There are " << listOfProducts.size() << " unique 1-9 pandigitals\nThe sum of the products of these pandigitals is " << sumOfPandigitals; result << "There are " << listOfProducts.size() << " unique 1-9 pandigitals\nThe sum of the products of these pandigitals is " << sumOfPandigitals;
return result.str(); return result.str();
} }
//Returns the sum of the pandigitals //Returns the sum of the pandigitals
int64_t Problem32::getSumOfPandigitals(){ int64_t Problem32::getSumOfPandigitals() const{
//If the problem hasn't been solved throw an exception solvedCheck("sum of the pandigitals");
if(!solved){
throw Unsolved();
}
return sumOfPandigitals; return sumOfPandigitals;
} }

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/src/Problems/Problem33.cpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem33.cpp
//Matthew Ellison //Matthew Ellison
// Created: 02-05-2021 // Created: 02-05-21
//Modified: 02-06-2021 //Modified: 07-02-21
/* /*
The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s
We shall consider fractions like, 30/50 = 3/5, to be trivial examples We shall consider fractions like, 30/50 = 3/5, to be trivial examples
@@ -29,11 +29,11 @@ If the product of these four fractions is given in its lowest common terms, find
#include <cinttypes> #include <cinttypes>
#include <numeric> #include <numeric>
#include <string>
#include <sstream> #include <sstream>
#include <string>
#include <vector> #include <vector>
#include "mee/vectorAlgorithms.hpp"
#include "Problems/Problem33.hpp" #include "Problems/Problem33.hpp"
#include "Algorithms.hpp"
//The lowest the numerator can be //The lowest the numerator can be
@@ -60,6 +60,7 @@ void Problem33::solve(){
//Start the timer //Start the timer
timer.start(); timer.start();
//Search every possible numerator/denominator pair //Search every possible numerator/denominator pair
for(int denominator = MIN_DENOMINATOR;denominator <= MAX_DENOMINATOR;++denominator){ for(int denominator = MIN_DENOMINATOR;denominator <= MAX_DENOMINATOR;++denominator){
for(int numerator = MIN_NUMERATOR;(numerator < denominator) && (numerator <= MAX_NUMERATOR);++numerator){ for(int numerator = MIN_NUMERATOR;(numerator < denominator) && (numerator <= MAX_NUMERATOR);++numerator){
@@ -106,6 +107,7 @@ void Problem33::solve(){
//Save the denominator //Save the denominator
prodDenominator = denomProd / gcd; prodDenominator = denomProd / gcd;
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
@@ -121,36 +123,24 @@ void Problem33::reset(){
//Gets //Gets
//Return a string with the solution to the problem //Return a string with the solution to the problem
std::string Problem33::getResult(){ std::string Problem33::getResult() const{
//If the problem hasn't been solved throw an exception solvedCheck("result");
if(!solved){
throw Unsolved();
}
std::stringstream result; std::stringstream result;
result << "The denominator of the product is " << prodDenominator; result << "The denominator of the product is " << prodDenominator;
return result.str(); return result.str();
} }
//Returns the list of numerators //Returns the list of numerators
std::vector<int> Problem33::getNumerators(){ std::vector<int> Problem33::getNumerators() const{
//If the problem hasn't been solved throw an exception solvedCheck("list of numerators");
if(!solved){
throw Unsolved();
}
return numerators; return numerators;
} }
//Returns the list of denominators //Returns the list of denominators
std::vector<int> Problem33::getDenominators(){ std::vector<int> Problem33::getDenominators() const{
//If the problem hasn't been solved throw an exception solvedCheck("list of denominators");
if(!solved){
throw Unsolved();
}
return denominators; return denominators;
} }
//Returns the answer to the question //Returns the answer to the question
int Problem33::getProdDenominator(){ int Problem33::getProdDenominator() const{
//If the problem hasn't been solved throw an exception solvedCheck("denominator");
if(!solved){
throw Unsolved();
}
return prodDenominator; return prodDenominator;
} }

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/src/Problems/Problem34.cpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem34.cpp
//Matthew Ellison //Matthew Ellison
// Created: 06-01-21 // Created: 06-01-21
//Modified: 06-01-21 //Modified: 07-02-21
//Find the sum of all numbers which are equal to the sum of the factorial of their digits //Find the sum of all numbers which are equal to the sum of the factorial 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
/* /*
@@ -25,8 +25,8 @@
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <vector> #include <vector>
#include "mee/numberAlgorithms.hpp"
#include "Problems/Problem34.hpp" #include "Problems/Problem34.hpp"
#include "Algorithms.hpp"
//The largest num that can be the sum of its own digits //The largest num that can be the sum of its own digits
@@ -50,6 +50,7 @@ void Problem34::solve(){
//Start the timer //Start the timer
timer.start(); timer.start();
//Pre-compute the possible factorials from 0! to 9! //Pre-compute the possible factorials from 0! to 9!
for(int cnt = 0;cnt <= 9;++cnt){ for(int cnt = 0;cnt <= 9;++cnt){
factorials[cnt] = mee::factorial(cnt); factorials[cnt] = mee::factorial(cnt);
@@ -69,6 +70,7 @@ void Problem34::solve(){
} }
} }
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
@@ -86,28 +88,19 @@ void Problem34::reset(){
//Gets //Gets
//Return a string with the solution to the problem //Return a string with the solution to the problem
std::string Problem34::getResult(){ std::string Problem34::getResult() const{
//If the problem hasn't been solved throw an exception solvedCheck("result");
if(!solved){
throw Unsolved();
}
std::stringstream result; std::stringstream result;
result << "The sum of all numbers that are the sum of their digit's factorials is " << sum; result << "The sum of all numbers that are the sum of their digit's factorials is " << sum;
return result.str(); return result.str();
} }
//Returns the list of factorials from 0-9 //Returns the list of factorials from 0-9
std::vector<int> Problem34::getFactorials(){ std::vector<int> Problem34::getFactorials() const{
//If the problem hasn't been solved throw an exception solvedCheck("list of factorials");
if(!solved){
throw Unsolved();
}
return factorials; return factorials;
} }
//Returns the sum of all numbers equal to the sum of their digit's factorials //Returns the sum of all numbers equal to the sum of their digit's factorials
int Problem34::getSum(){ int Problem34::getSum() const{
//If the problem hasn't been solved throw an exception solvedCheck("sum");
if(!solved){
throw Unsolved();
}
return sum; return sum;
} }

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/src/Problems/Problem35.cpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem35.cpp
//Matthew Ellison //Matthew Ellison
// Created: 06-02-21 // Created: 06-02-21
//Modified: 06-02-21 //Modified: 07-02-21
//How many circular primes are there below one million? //How many circular primes are there below one 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
/* /*
@@ -24,11 +24,12 @@
//TODO: This could also be improved by skipping any prime that includes 2, 4, 5, 6, 8, or 0 //TODO: This could also be improved by skipping any prime that includes 2, 4, 5, 6, 8, or 0
#include <string>
#include <sstream> #include <sstream>
#include <string>
#include <vector> #include <vector>
#include "mee/numberAlgorithms.hpp"
#include "mee/vectorAlgorithms.hpp"
#include "Problems/Problem35.hpp" #include "Problems/Problem35.hpp"
#include "Algorithms.hpp"
//The largest number that we are checking for primes //The largest number that we are checking for primes
@@ -61,6 +62,7 @@ void Problem35::solve(){
//Start the timer //Start the timer
timer.start(); timer.start();
//Get all primes under 1,000,000 //Get all primes under 1,000,000
primes = mee::getPrimes(MAX_NUM); primes = mee::getPrimes(MAX_NUM);
//Go through all primes, get all their rotations, and check if those numbers are also primes //Go through all primes, get all their rotations, and check if those numbers are also primes
@@ -81,6 +83,7 @@ void Problem35::solve(){
} }
} }
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
@@ -95,36 +98,24 @@ void Problem35::reset(){
} }
//Gets //Gets
//Returns a string with the solution to the problem //Returns a string with the solution to the problem
std::string Problem35::getResult(){ std::string Problem35::getResult() const{
//If the problem hasn't been solved throw an exception solvedCheck("result");
if(!solved){
throw Unsolved();
}
std::stringstream result; std::stringstream result;
result << "The number of all circular prime numbers under " << MAX_NUM << " is " << circularPrimes.size(); result << "The number of all circular prime numbers under " << MAX_NUM << " is " << circularPrimes.size();
return result.str(); return result.str();
} }
//Returns the vector of primes < MAX_NUM //Returns the vector of primes < MAX_NUM
std::vector<int> Problem35::getPrimes(){ std::vector<int> Problem35::getPrimes() const{
//If the problem hasn't been solved throw an exception solvedCheck("list of primes");
if(!solved){
throw Unsolved();
}
return primes; return primes;
} }
//Returns the vector of circular primes < MAX_NUM //Returns the vector of circular primes < MAX_NUM
std::vector<int> Problem35::getCircularPrimes(){ std::vector<int> Problem35::getCircularPrimes() const{
//If the problem hasn't been solved throw an exception solvedCheck("list of circular primes");
if(!solved){
throw Unsolved();
}
return circularPrimes; return circularPrimes;
} }
//Returns the number of circular primes //Returns the number of circular primes
int Problem35::getNumCircularPrimes(){ int Problem35::getNumCircularPrimes() const{
//If the problem hasn't been solved throw an exception solvedCheck("number of circular primes");
if(!solved){
throw Unsolved();
}
return circularPrimes.size(); return circularPrimes.size();
} }

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/src/Problems/Problem36.cpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem36.cpp
//Matthew Ellison //Matthew Ellison
// Created: 06-29-21 // Created: 06-29-21
//Modified: 06-29-21 //Modified: 07-02-21
//Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2. //Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.
//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
/* /*
@@ -22,11 +22,13 @@
*/ */
#include <string>
#include <sstream> #include <sstream>
#include <string>
#include <vector> #include <vector>
#include "mee/numberAlgorithms.hpp"
#include "mee/stringAlgorithms.hpp"
#include "mee/vectorAlgorithms.hpp"
#include "Problems/Problem36.hpp" #include "Problems/Problem36.hpp"
#include "Algorithms.hpp"
//The largest number that will be checked //The largest number that will be checked
@@ -47,6 +49,7 @@ void Problem36::solve(){
//Start the timer //Start the timer
timer.start(); timer.start();
//Start with 1, check if it is a palindrome in base 10 and 2, and continue to MAX_NUM //Start with 1, check if it is a palindrome in base 10 and 2, and continue to MAX_NUM
for(int num = 1;num < MAX_NUM;++num){ for(int num = 1;num < MAX_NUM;++num){
//Check if num is a palindrome //Check if num is a palindrome
@@ -62,6 +65,7 @@ void Problem36::solve(){
//Get the sum of all palindromes in the vector //Get the sum of all palindromes in the vector
sum = mee::getSum(palindromes); sum = mee::getSum(palindromes);
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
@@ -77,28 +81,19 @@ void Problem36::reset(){
//Gets //Gets
//Returns a string with the solution to the problem //Returns a string with the solution to the problem
std::string Problem36::getResult(){ std::string Problem36::getResult() const{
//If the problem hasn't been solved throw an exception solvedCheck("result");
if(!solved){
throw Unsolved();
}
std::stringstream result; std::stringstream result;
result << "The sum of all base 10 and base 2 palindromic numbers < " << MAX_NUM << " is " << sum; result << "The sum of all base 10 and base 2 palindromic numbers < " << MAX_NUM << " is " << sum;
return result.str(); return result.str();
} }
//Return the vector of palindromes < MAX_NUM //Return the vector of palindromes < MAX_NUM
std::vector<int> Problem36::getPalindromes(){ std::vector<int> Problem36::getPalindromes() const{
//If the problem hasn't been solved throw an exception solvedCheck("list of palindromes");
if(!solved){
throw Unsolved();
}
return palindromes; return palindromes;
} }
//Return the sum of all elements in the vector of palindromes //Return the sum of all elements in the vector of palindromes
int Problem36::getSumOfPalindromes(){ int Problem36::getSumOfPalindromes() const{
//If the problem hasn't been solved throw an exception solvedCheck("sum of all palindromes");
if(!solved){
throw Unsolved();
}
return sum; return sum;
} }

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/src/Problems/Problem37.cpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem37.cpp
//Matthew Ellison //Matthew Ellison
// Created: 06-30-21 // Created: 06-30-21
//Modified: 06-30-21 //Modified: 07-02-21
//Find the sum of the only eleven primes that are both truncatable from left to right and right to left (2, 3, 5, and 7 are not counted). //Find the sum of the only eleven primes that are both truncatable from left to right and right to left (2, 3, 5, and 7 are not counted).
//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
/* /*
@@ -23,11 +23,13 @@
#include <cinttypes> #include <cinttypes>
#include <string>
#include <sstream> #include <sstream>
#include <string>
#include <vector> #include <vector>
#include "mee/Generator.hpp"
#include "mee/numberAlgorithms.hpp"
#include "mee/vectorAlgorithms.hpp"
#include "Problems/Problem37.hpp" #include "Problems/Problem37.hpp"
#include "Algorithms.hpp"
//The last prime before 11 since single digit primes aren't checked //The last prime before 11 since single digit primes aren't checked
@@ -48,8 +50,9 @@ void Problem37::solve(){
//Start the timer //Start the timer
timer.start(); timer.start();
//Create the sieve and get the first prime number //Create the sieve and get the first prime number
mee::SieveOfEratosthenes<uint64_t> sieve; mee::Generator<uint64_t> sieve = mee::sieveOfEratosthenes<uint64_t>();
uint64_t currentPrime = sieve.next(); uint64_t currentPrime = sieve.next();
//Loop through the sieve until you get to LAST_PRIME_BEFORE_CHECK //Loop through the sieve until you get to LAST_PRIME_BEFORE_CHECK
while(currentPrime < LAST_PRIME_BEFORE_CHECK){ while(currentPrime < LAST_PRIME_BEFORE_CHECK){
@@ -110,6 +113,7 @@ void Problem37::solve(){
//Get the sum of all elements in the truncPrimes vector //Get the sum of all elements in the truncPrimes vector
sum = mee::getSum(truncPrimes); sum = mee::getSum(truncPrimes);
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
@@ -125,28 +129,19 @@ void Problem37::reset(){
//Gets //Gets
//Returns a string with the solution to the problem //Returns a string with the solution to the problem
std::string Problem37::getResult(){ std::string Problem37::getResult() const{
//If the problem hasn't been solved throw an exception solvedCheck("result");
if(!solved){
throw Unsolved();
}
std::stringstream result; std::stringstream result;
result << "The sum of all left and right truncatable primes is " << sum; result << "The sum of all left and right truncatable primes is " << sum;
return result.str(); return result.str();
} }
//Returns the list of primes that can be truncated //Returns the list of primes that can be truncated
std::vector<uint64_t> Problem37::getTruncatablePrimes(){ std::vector<uint64_t> Problem37::getTruncatablePrimes() const{
//If the problem hasn't been solved throw an exception solvedCheck("list of truncatable primes");
if(!solved){
throw Unsolved();
}
return truncPrimes; return truncPrimes;
} }
//Get the sum of all primes in truncPrimes //Get the sum of all primes in truncPrimes
uint64_t Problem37::getSumOfPrimes(){ uint64_t Problem37::getSumOfPrimes() const{
//If the problem hasn't been solved throw an exception solvedCheck("sum of truncatable primes");
if(!solved){
throw Unsolved();
}
return sum; return sum;
} }

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem4.cpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem4.cpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // Created: 09-28-18
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -22,10 +22,10 @@
*/ */
#include <vector>
#include <string>
#include <sstream>
#include <algorithm> #include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include "Problems/Problem4.hpp" #include "Problems/Problem4.hpp"
@@ -44,9 +44,11 @@ void Problem4::solve(){
if(solved){ if(solved){
return; return;
} }
//Start the timer //Start the timer
timer.start(); timer.start();
//Start at the first 3-digit number and check every one up to the last 3-digit number //Start at the first 3-digit number and check every one up to the last 3-digit number
for(int firstNum = START_NUM;firstNum <= END_NUM;++firstNum){ for(int firstNum = START_NUM;firstNum <= END_NUM;++firstNum){
//You can start at the location of the first number because everything before that has already been tested. (100*101 == 101*100) //You can start at the location of the first number because everything before that has already been tested. (100*101 == 101*100)
@@ -66,10 +68,10 @@ void Problem4::solve(){
//If it's not a palindrome ignore it and move to the next number //If it's not a palindrome ignore it and move to the next number
} }
} }
//Sort the palindromes so that the last one is the largest //Sort the palindromes so that the last one is the largest
std::sort(palindromes.begin(), palindromes.end()); std::sort(palindromes.begin(), palindromes.end());
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
@@ -84,27 +86,19 @@ void Problem4::reset(){
//Gets //Gets
//Return a string with the solution to the problem //Return a string with the solution to the problem
std::string Problem4::getResult(){ std::string Problem4::getResult() const{
if(!solved){ solvedCheck("result");
throw Unsolved();
}
std::stringstream result; std::stringstream result;
result << "The largest palindrome is " << *(palindromes.end() - 1); result << "The largest palindrome is " << *(palindromes.end() - 1);
return result.str(); 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 solvedCheck("palindromes");
if(!solved){
throw Unsolved();
}
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 solvedCheck("largest palindrome");
if(!solved){
throw Unsolved();
}
return *(palindromes.end() - 1); return *(palindromes.end() - 1);
} }

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem5.cpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem5.cpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // Created: 09-28-18
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -22,10 +22,12 @@
*/ */
#include <vector>
#include <cinttypes> #include <cinttypes>
#include <string>
#include <sstream> #include <sstream>
#include <string>
#include <vector>
#include "mee/numberAlgorithms.hpp"
#include "mee/vectorAlgorithms.hpp"
#include "Problems/Problem5.hpp" #include "Problems/Problem5.hpp"
@@ -39,15 +41,16 @@ void Problem5::solve(){
if(solved){ if(solved){
return; return;
} }
//Setup your variables
bool numFound = false; //Used to determine if the number has been found
//Start the timer //Start the timer
timer.start(); timer.start();
//Start at 20 because it must at least be divisible by 20. Increment by 2 because it must be an even number to be divisible by 2
//Loop through every even number starting at 20 int currentNum = 0; //The current number being checked
int currentNum = 20; //Get all primes < 20 and multiply them together. That will be the starting point as the smallest possible answer
std::vector<int> primes = mee::getPrimes(20);
currentNum = mee::getProduct(primes);
bool numFound = false; //Used to determine if the number has been found
while((currentNum > 0) && (!numFound)){ while((currentNum > 0) && (!numFound)){
//Start by assuming you found the number (because we throw a flag if we didn't find it) //Start by assuming you found the number (because we throw a flag if we didn't find it)
numFound = true; numFound = true;
@@ -59,14 +62,15 @@ void Problem5::solve(){
break; break;
} }
} }
//If you didn't find the correct number then increment by 2 //If you didn't find the correct number then increment by 2 because the number has to be even
if(!numFound){ if(!numFound){
currentNum += 2; currentNum += 2;
} }
} }
//Save the current number as the smallest
smallestNum = currentNum; smallestNum = currentNum;
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
@@ -81,19 +85,14 @@ void Problem5::reset(){
//Gets //Gets
//Return a string with the solution to the problem //Return a string with the solution to the problem
std::string Problem5::getResult(){ std::string Problem5::getResult() const{
if(!solved){ solvedCheck("result");
throw Unsolved();
}
std::stringstream result; std::stringstream result;
result << "The smallest positive number evenly divisible by all numbers 1-20 is " << smallestNum; result << "The smallest positive number evenly divisible by all numbers 1-20 is " << smallestNum;
return result.str(); 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 solvedCheck("number");
if(!solved){
throw Unsolved();
}
return smallestNum; return smallestNum;
} }

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem6.cpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem6.cpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // Created: 09-28-18
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -24,8 +24,8 @@
#include <cinttypes> #include <cinttypes>
#include <cmath> #include <cmath>
#include <string>
#include <sstream> #include <sstream>
#include <string>
#include "Problems/Problem6.hpp" #include "Problems/Problem6.hpp"
@@ -42,9 +42,11 @@ void Problem6::solve(){
if(solved){ if(solved){
return; return;
} }
//Start the timer //Start the timer
timer.start(); timer.start();
//Run through all numbers and add them to the appropriate sums //Run through all numbers and add them to the appropriate sums
for(int currentNum = START_NUM;currentNum <= END_NUM;++currentNum){ for(int currentNum = START_NUM;currentNum <= END_NUM;++currentNum){
sumOfSquares += (currentNum * currentNum); //Add the square to the correct variable sumOfSquares += (currentNum * currentNum); //Add the square to the correct variable
@@ -53,6 +55,7 @@ void Problem6::solve(){
//Square the sum that needs it //Square the sum that needs it
squareOfSum *= squareOfSum; squareOfSum *= squareOfSum;
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
@@ -67,35 +70,24 @@ void Problem6::reset(){
//Gets //Gets
//Return a string with the solution to the problem //Return a string with the solution to the problem
std::string Problem6::getResult(){ std::string Problem6::getResult() const{
if(!solved){ solvedCheck("result");
throw Unsolved();
}
std::stringstream result; 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); 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(); 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 solvedCheck("sum of the squares");
if(!solved){
throw Unsolved();
}
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 solvedCheck("square of the sums");
if(!solved){
throw Unsolved();
}
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 solvedCheck("difference between the two numbers");
if(!solved){ return std::abs((int64_t)(sumOfSquares - squareOfSum));
throw Unsolved();
}
return abs(sumOfSquares - squareOfSum);
} }

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem67.cpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem67.cpp
//Matthew Ellison //Matthew Ellison
// Created: 11-02-18 // Created: 11-02-18
//Modified: 08-28-20 //Modified: 07-02-21
//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
@@ -108,7 +108,7 @@ Find the maximum total from top to bottom
*/ */
//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) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -130,7 +130,6 @@ Find the maximum total from top to bottom
//This is the list you are trying to find a path through //This is the list you are trying to find a path through
void Problem67::setupList(){ void Problem67::setupList(){
list.push_back({59}); list.push_back({59});
list.push_back({73, 41}); list.push_back({73, 41});
@@ -233,3 +232,8 @@ void Problem67::setupList(){
list.push_back({30, 11, 85, 31, 34, 71, 13, 48, 05, 14, 44, 03, 19, 67, 23, 73, 19, 57, 06, 90, 94, 72, 57, 69, 81, 62, 59, 68, 88, 57, 55, 69, 49, 13, 07, 87, 97, 80, 89, 05, 71, 05, 05, 26, 38, 40, 16, 62, 45, 99, 18, 38, 98, 24, 21, 26, 62, 74, 69, 04, 85, 57, 77, 35, 58, 67, 91, 79, 79, 57, 86, 28, 66, 34, 72, 51, 76, 78, 36, 95, 63, 90, 8, 78, 47, 63, 45, 31, 22, 70, 52, 48, 79, 94, 15, 77, 61, 67, 68}); list.push_back({30, 11, 85, 31, 34, 71, 13, 48, 05, 14, 44, 03, 19, 67, 23, 73, 19, 57, 06, 90, 94, 72, 57, 69, 81, 62, 59, 68, 88, 57, 55, 69, 49, 13, 07, 87, 97, 80, 89, 05, 71, 05, 05, 26, 38, 40, 16, 62, 45, 99, 18, 38, 98, 24, 21, 26, 62, 74, 69, 04, 85, 57, 77, 35, 58, 67, 91, 79, 79, 57, 86, 28, 66, 34, 72, 51, 76, 78, 36, 95, 63, 90, 8, 78, 47, 63, 45, 31, 22, 70, 52, 48, 79, 94, 15, 77, 61, 67, 68});
list.push_back({23, 33, 44, 81, 80, 92, 93, 75, 94, 88, 23, 61, 39, 76, 22, 03, 28, 94, 32, 06, 49, 65, 41, 34, 18, 23, 8, 47, 62, 60, 03, 63, 33, 13, 80, 52, 31, 54, 73, 43, 70, 26, 16, 69, 57, 87, 83, 31, 03, 93, 70, 81, 47, 95, 77, 44, 29, 68, 39, 51, 56, 59, 63, 07, 25, 70, 07, 77, 43, 53, 64, 03, 94, 42, 95, 39, 18, 01, 66, 21, 16, 97, 20, 50, 90, 16, 70, 10, 95, 69, 29, 06, 25, 61, 41, 26, 15, 59, 63, 35}); list.push_back({23, 33, 44, 81, 80, 92, 93, 75, 94, 88, 23, 61, 39, 76, 22, 03, 28, 94, 32, 06, 49, 65, 41, 34, 18, 23, 8, 47, 62, 60, 03, 63, 33, 13, 80, 52, 31, 54, 73, 43, 70, 26, 16, 69, 57, 87, 83, 31, 03, 93, 70, 81, 47, 95, 77, 44, 29, 68, 39, 51, 56, 59, 63, 07, 25, 70, 07, 77, 43, 53, 64, 03, 94, 42, 95, 39, 18, 01, 66, 21, 16, 97, 20, 50, 90, 16, 70, 10, 95, 69, 29, 06, 25, 61, 41, 26, 15, 59, 63, 35});
} }
Problem67::Problem67() : Problem18(){
list.clear();
setupList();
}

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem7.cpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem7.cpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // Created: 09-28-18
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -22,11 +22,10 @@
*/ */
#include <vector>
#include <cinttypes> #include <cinttypes>
#include <string>
#include <sstream> #include <sstream>
#include "Algorithms.hpp" #include <string>
#include "mee/numberAlgorithms.hpp"
#include "Problems/Problem7.hpp" #include "Problems/Problem7.hpp"
@@ -43,12 +42,15 @@ void Problem7::solve(){
if(solved){ if(solved){
return; return;
} }
//Start the timer //Start the timer
timer.start(); timer.start();
//Get the correct number of prime numbers //Get the correct number of prime numbers
primes = mee::getNumPrimes(NUMBER_OF_PRIMES); primes = mee::getNumPrimes(NUMBER_OF_PRIMES);
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
@@ -62,19 +64,14 @@ void Problem7::reset(){
//Gets //Gets
//Return a string with the solution to the problem //Return a string with the solution to the problem
std::string Problem7::getResult(){ std::string Problem7::getResult() const{
if(!solved){ solvedCheck("result");
throw Unsolved();
}
std::stringstream result; std::stringstream result;
result << "The " << NUMBER_OF_PRIMES << "th prime number is " << primes.at(primes.size() - 1); result << "The " << NUMBER_OF_PRIMES << "th prime number is " << primes[primes.size() - 1];
return result.str(); 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 solvedCheck("prime");
if(!solved){
throw Unsolved();
}
return *primes.end(); return *primes.end();
} }

View File

@@ -1,7 +1,7 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem8.cpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem8.cpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // Created: 09-28-18
//Modified: 08-28-20 //Modified: 07-02-21
//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
@@ -27,7 +27,7 @@
*/ */
//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) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -45,9 +45,9 @@
#include <cinttypes> #include <cinttypes>
#include <sstream>
#include <string> #include <string>
#include <vector> #include <vector>
#include <sstream>
#include "Problems/Problem8.hpp" #include "Problems/Problem8.hpp"
@@ -64,9 +64,11 @@ void Problem8::solve(){
if(solved){ if(solved){
return; return;
} }
//Start the timer //Start the timer
timer.start(); timer.start();
//Cycle through the string of numbers looking for the maximum product //Cycle through the string of numbers looking for the maximum product
for(unsigned int cnt = 12;cnt < number.size();++cnt){ for(unsigned int cnt = 12;cnt < number.size();++cnt){
uint64_t currentProduct = std::stoull(number.substr(cnt, 1)) * std::stoull(number.substr(cnt - 1, 1)) * std::stoull(number.substr(cnt - 2, 1)) * std::stoull(number.substr(cnt - 3, 1)) * std::stoull(number.substr(cnt - 4, 1)) * std::stoull(number.substr(cnt - 5, 1)) * std::stoull(number.substr(cnt - 6, 1)) * std::stoull(number.substr(cnt - 7, 1)) * std::stoull(number.substr(cnt - 8, 1)) * std::stoull(number.substr(cnt - 9, 1)) * std::stoull(number.substr(cnt - 10, 1)) * std::stoull(number.substr(cnt - 11, 1)) * std::stoull(number.substr(cnt - 12, 1)); uint64_t currentProduct = std::stoull(number.substr(cnt, 1)) * std::stoull(number.substr(cnt - 1, 1)) * std::stoull(number.substr(cnt - 2, 1)) * std::stoull(number.substr(cnt - 3, 1)) * std::stoull(number.substr(cnt - 4, 1)) * std::stoull(number.substr(cnt - 5, 1)) * std::stoull(number.substr(cnt - 6, 1)) * std::stoull(number.substr(cnt - 7, 1)) * std::stoull(number.substr(cnt - 8, 1)) * std::stoull(number.substr(cnt - 9, 1)) * std::stoull(number.substr(cnt - 10, 1)) * std::stoull(number.substr(cnt - 11, 1)) * std::stoull(number.substr(cnt - 12, 1));
@@ -79,6 +81,7 @@ void Problem8::solve(){
} }
} }
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
@@ -94,10 +97,8 @@ void Problem8::reset(){
//Gets //Gets
//Return a string with the solution to the problem //Return a string with the solution to the problem
std::string Problem8::getResult(){ std::string Problem8::getResult() const{
if(!solved){ solvedCheck("result");
throw Unsolved();
}
std::stringstream result; std::stringstream result;
result << "The greatest product is " << maxProduct result << "The greatest product is " << maxProduct
<< "\nThe numbers are " << maxNums; << "\nThe numbers are " << maxNums;
@@ -105,17 +106,11 @@ std::string Problem8::getResult(){
} }
//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 solvedCheck("numbers that make the largest product");
if(!solved){
throw Unsolved();
}
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 solvedCheck("product of the numbers");
if(!solved){
throw Unsolved();
}
return maxProduct; return maxProduct;
} }

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/Source/Problem9.cpp //ProjectEuler/ProjectEulerCPP/src/Problems/Problem9.cpp
//Matthew Ellison //Matthew Ellison
// Created: 09-28-18 // Created: 09-28-18
//Modified: 08-28-20 //Modified: 07-02-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -23,8 +23,8 @@
#include <cmath> #include <cmath>
#include <string>
#include <sstream> #include <sstream>
#include <string>
#include "Problems/Problem9.hpp" #include "Problems/Problem9.hpp"
@@ -41,18 +41,20 @@ void Problem9::solve(){
if(solved){ if(solved){
return; return;
} }
//Start the timer //Start the timer
timer.start(); timer.start();
//Loop through all possible a's //Loop through all possible a's
while((a < GOAL_SUM) && !found){ while((a < GOAL_SUM) && !found){
b = a + 1; //b bust be greater than a b = a + 1; //b bust be greater than a
c = sqrt((a * a) + (b * b)); c = std::sqrt((a * a) + (b * b));
//Loop through all possible b's //Loop through all possible b's
while((a + b + c) < GOAL_SUM){ while((a + b + c) < GOAL_SUM){
++b; ++b;
c = sqrt((a * a) + (b * b)); c = std::sqrt((a * a) + (b * b));
} }
//If the sum == 1000 you found the number, otherwise go to the next possible a //If the sum == 1000 you found the number, otherwise go to the next possible a
@@ -64,19 +66,17 @@ void Problem9::solve(){
} }
} }
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
//Save the results //Throw a flag to show the problem is solved
if(found){ if(found){
solved = true; solved = true;
} }
else{ else{
throw Unsolved("The triplet was not found!"); throw Unsolved("The triplet was not found!");
} }
//Throw a flag to show the problem is solved
} }
//Reset the problem so it can be run again //Reset the problem so it can be run again
void Problem9::reset(){ void Problem9::reset(){
@@ -89,10 +89,8 @@ void Problem9::reset(){
//Gets //Gets
//Return a string with the solution to the problem //Return a string with the solution to the problem
std::string Problem9::getResult(){ std::string Problem9::getResult() const{
if(!solved){ solvedCheck("result");
throw Unsolved();
}
std::stringstream result; std::stringstream result;
result << "The Pythagorean triplet is " << a << ' ' << b << ' ' << (int)c result << "The Pythagorean triplet is " << a << ' ' << b << ' ' << (int)c
<< "\nThe numbers' product is " << a * b * (int)c; << "\nThe numbers' product is " << a * b * (int)c;
@@ -100,33 +98,21 @@ std::string Problem9::getResult(){
} }
//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 solvedCheck("first side");
if(!solved){
throw Unsolved();
}
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 solvedCheck("second side");
if(!solved){
throw Unsolved();
}
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 solvedCheck("third side");
if(!solved){
throw Unsolved();
}
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 solvedCheck("product of all three sides");
if(!solved){
throw Unsolved();
}
return a * b * (int)c; return a * b * (int)c;
} }

View File

@@ -1,11 +1,11 @@
//ProjectEuler/ProjectEulerCPP/main.cpp //ProjectEuler/ProjectEulerCPP/main.cpp
//Matthew Ellison //Matthew Ellison
// Created: 07-04-19 // Created: 07-04-19
//Modified: 07-09-20 //Modified: 07-01-21
//This is a driver function for all Project Euler problems //This is a driver function for all Project Euler problems
//It prompts the user for an action (state the problem or solve the problem), then executes the appropriate command on the appropriate problem //It prompts the user for an action (state the problem or solve the problem), then executes the appropriate command on the appropriate problem
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -25,7 +25,6 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include "benchmark.hpp" #include "benchmark.hpp"
#include "Algorithms.hpp"
#include "Problem.hpp" #include "Problem.hpp"
#include "ProblemSelection.hpp" #include "ProblemSelection.hpp"
@@ -102,9 +101,9 @@ void solveMenu(){
//Solve to every valid problem number, skipping over 0 //Solve to every valid problem number, skipping over 0
for(unsigned int problemLocation = PROBLEM_NUMBERS[1];problemLocation < PROBLEM_NUMBERS.size();++problemLocation){ for(unsigned int problemLocation = PROBLEM_NUMBERS[1];problemLocation < PROBLEM_NUMBERS.size();++problemLocation){
//Generate the current problem //Generate the current problem
problem = getProblem(PROBLEM_NUMBERS.at(problemLocation)); problem = getProblem(PROBLEM_NUMBERS[problemLocation]);
//Solve the problem //Solve the problem
std::cout << PROBLEM_NUMBERS.at(problemLocation) << ". "; std::cout << PROBLEM_NUMBERS[problemLocation] << ". ";
solveProblem(problem); solveProblem(problem);
//Release the memory //Release the memory
delete problem; delete problem;
@@ -137,9 +136,9 @@ void descriptionMenu(){
//Print description for every valid problem number, skipping over 0 //Print description for every valid problem number, skipping over 0
for(unsigned int problemLocation = PROBLEM_NUMBERS[1];problemLocation < PROBLEM_NUMBERS.size();++problemLocation){ for(unsigned int problemLocation = PROBLEM_NUMBERS[1];problemLocation < PROBLEM_NUMBERS.size();++problemLocation){
//Generate the problem //Generate the problem
problem = getProblem(PROBLEM_NUMBERS.at(problemLocation)); problem = getProblem(PROBLEM_NUMBERS[problemLocation]);
//Print the problem's description //Print the problem's description
std::cout << PROBLEM_NUMBERS.at(problemLocation) << ". "; std::cout << PROBLEM_NUMBERS[problemLocation] << ". ";
printDescription(problem); printDescription(problem);
std::cout << '\n'; std::cout << '\n';
//Release the memory //Release the memory