From 9660fe92875fb2ff42e60728f8d9f0ac4405c15e Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Sat, 3 Jul 2021 02:38:00 -0400 Subject: [PATCH] Updated to work with the new libraries --- headers/Problem.hpp | 16 +++++---- headers/ProblemSelection.hpp | 10 +++--- headers/Problems/Problem1.hpp | 12 +++---- headers/Problems/Problem10.hpp | 11 +++--- headers/Problems/Problem11.hpp | 11 +++--- headers/Problems/Problem12.hpp | 13 +++---- headers/Problems/Problem13.hpp | 13 ++++--- headers/Problems/Problem14.hpp | 13 +++---- headers/Problems/Problem15.hpp | 11 +++--- headers/Problems/Problem16.hpp | 11 +++--- headers/Problems/Problem17.hpp | 11 +++--- headers/Problems/Problem18.hpp | 19 +++++----- headers/Problems/Problem19.hpp | 14 ++++---- headers/Problems/Problem2.hpp | 13 +++---- headers/Problems/Problem20.hpp | 11 +++--- headers/Problems/Problem21.hpp | 12 +++---- headers/Problems/Problem22.hpp | 11 +++--- headers/Problems/Problem23.hpp | 13 +++---- headers/Problems/Problem24.hpp | 11 +++--- headers/Problems/Problem25.hpp | 11 +++--- headers/Problems/Problem26.hpp | 11 +++--- headers/Problems/Problem27.hpp | 13 +++---- headers/Problems/Problem28.hpp | 14 ++++---- headers/Problems/Problem29.hpp | 23 ++++++------ headers/Problems/Problem3.hpp | 14 ++++---- headers/Problems/Problem30.hpp | 9 +++-- headers/Problems/Problem31.hpp | 8 ++--- headers/Problems/Problem32.hpp | 7 ++-- headers/Problems/Problem33.hpp | 24 ++++++------- headers/Problems/Problem34.hpp | 13 +++---- headers/Problems/Problem35.hpp | 15 ++++---- headers/Problems/Problem36.hpp | 11 +++--- headers/Problems/Problem37.hpp | 11 +++--- headers/Problems/Problem4.hpp | 13 +++---- headers/Problems/Problem5.hpp | 15 ++++---- headers/Problems/Problem6.hpp | 13 +++---- headers/Problems/Problem67.hpp | 15 ++++---- headers/Problems/Problem7.hpp | 15 ++++---- headers/Problems/Problem8.hpp | 13 ++++--- headers/Problems/Problem9.hpp | 12 +++---- headers/Unsolved.hpp | 16 ++++++++- headers/benchmark.hpp | 7 ++-- makefile | 4 +-- src/Problems/Problem1.cpp | 26 +++++++------- src/Problems/Problem10.cpp | 25 +++++++------ src/Problems/Problem11.cpp | 64 +++++++++++++++------------------- src/Problems/Problem12.cpp | 38 ++++++++------------ src/Problems/Problem13.cpp | 26 +++++--------- src/Problems/Problem14.cpp | 26 ++++++-------- src/Problems/Problem15.cpp | 21 +++++------ src/Problems/Problem16.cpp | 29 ++++++--------- src/Problems/Problem17.cpp | 24 ++++++------- src/Problems/Problem18.cpp | 42 +++++++++------------- src/Problems/Problem19.cpp | 21 +++++------ src/Problems/Problem2.cpp | 24 ++++++------- src/Problems/Problem20.cpp | 31 ++++++---------- src/Problems/Problem21.cpp | 37 +++++++++----------- src/Problems/Problem22.cpp | 32 +++++++---------- src/Problems/Problem23.cpp | 28 +++++++-------- src/Problems/Problem24.cpp | 30 +++++++--------- src/Problems/Problem25.cpp | 45 ++++++++---------------- src/Problems/Problem26.cpp | 26 ++++++-------- src/Problems/Problem27.cpp | 32 +++++++---------- src/Problems/Problem28.cpp | 52 ++++++++++++--------------- src/Problems/Problem29.cpp | 53 ++++++++++++---------------- src/Problems/Problem3.cpp | 38 +++++++------------- src/Problems/Problem30.cpp | 45 +++++++----------------- src/Problems/Problem31.cpp | 20 +++++------ src/Problems/Problem32.cpp | 26 ++++++-------- src/Problems/Problem33.cpp | 38 ++++++++------------ src/Problems/Problem34.cpp | 27 ++++++-------- src/Problems/Problem35.cpp | 37 ++++++++------------ src/Problems/Problem36.cpp | 31 +++++++--------- src/Problems/Problem37.cpp | 33 ++++++++---------- src/Problems/Problem4.cpp | 34 ++++++++---------- src/Problems/Problem5.cpp | 39 ++++++++++----------- src/Problems/Problem6.cpp | 34 +++++++----------- src/Problems/Problem67.cpp | 12 ++++--- src/Problems/Problem7.cpp | 27 +++++++------- src/Problems/Problem8.cpp | 27 ++++++-------- src/Problems/Problem9.cpp | 46 +++++++++--------------- src/main.cpp | 13 ++++--- 82 files changed, 794 insertions(+), 1008 deletions(-) diff --git a/headers/Problem.hpp b/headers/Problem.hpp index 9a8454a..2fdc3a8 100644 --- a/headers/Problem.hpp +++ b/headers/Problem.hpp @@ -1,10 +1,10 @@ //ProjectEuler/ProjectEulerCPP/headers/Problem.hpp //Matthew Ellison // 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 /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -19,14 +19,13 @@ You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ - - #ifndef PROBLEM_HPP #define PROBLEM_HPP + #include #include -#include "Stopwatch.hpp" +#include "mee/Stopwatch.hpp" #include "Unsolved.hpp" @@ -69,11 +68,16 @@ public: timer.reset(); 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 //Solve the problem virtual void solve() = 0; //Return a string with the solution to the problem - virtual std::string getResult() = 0; + virtual std::string getResult() const = 0; }; #endif //PROBLEM_HPP diff --git a/headers/ProblemSelection.hpp b/headers/ProblemSelection.hpp index 947b40c..8e51ca1 100644 --- a/headers/ProblemSelection.hpp +++ b/headers/ProblemSelection.hpp @@ -1,10 +1,10 @@ //ProjectEuler/ProjectEulerCPP/headers/ProblemSelection.hpp //Matthew Ellison // 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 /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -19,13 +19,12 @@ You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ - - #ifndef PROBLEMSELECTION_HPP #define PROBLEMSELECTION_HPP + #include -#include "Algorithms.hpp" +#include "mee/vectorAlgorithms.hpp" #include "Problems/Problem1.hpp" #include "Problems/Problem2.hpp" #include "Problems/Problem3.hpp" @@ -156,4 +155,5 @@ void listProblems(){ std::cout << std::endl; } + #endif //PROBLEMSELECTION_HPP diff --git a/headers/Problems/Problem1.hpp b/headers/Problems/Problem1.hpp index 08b0fdc..f57e521 100644 --- a/headers/Problems/Problem1.hpp +++ b/headers/Problems/Problem1.hpp @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem1.hpp //Matthew Ellison // 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 //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 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 along with this program. If not, see . */ - #ifndef PROBLEM1_HPP #define PROBLEM1_HPP -#include #include #include #include "Problem.hpp" @@ -46,13 +44,15 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getResult(); //Return a string with the solution to the problem + virtual std::string getResult() const; //Return a string with the solution to the problem uint64_t getSum() const; //Returns the requested sum }; + /* Results: 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 */ -#endif //PROBLEM1_HPP + +#endif //PROBLEM1_HPP diff --git a/headers/Problems/Problem10.hpp b/headers/Problems/Problem10.hpp index e4b9952..cde611e 100644 --- a/headers/Problems/Problem10.hpp +++ b/headers/Problems/Problem10.hpp @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem10.hpp //Matthew Ellison // Created: 09-28-18 -//Modified: 08-28-20 +//Modified: 07-02-21 //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 /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -20,7 +20,6 @@ You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ - #ifndef PROBLEM10_HPP #define PROBLEM10_HPP @@ -44,13 +43,15 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getResult(); //Return a string with the solution to the problem + virtual std::string getResult() const; //Return a string with the solution to the problem uint64_t getSum() const; //Returns the sum that was requested }; + /* Results: 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 */ -#endif //PROBLEM10_HPP + +#endif //PROBLEM10_HPP diff --git a/headers/Problems/Problem11.hpp b/headers/Problems/Problem11.hpp index 456355d..ec10994 100644 --- a/headers/Problems/Problem11.hpp +++ b/headers/Problems/Problem11.hpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem11.hpp //Matthew Ellison // 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? /* 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 /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -42,7 +42,6 @@ You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ - #ifndef PROBLEM11_HPP #define PROBLEM11_HPP @@ -66,15 +65,17 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getResult(); //Return a string with the solution to the problem + virtual std::string getResult() const; //Return a string with the solution to the problem std::vector getNumbers() const; //Returns the numbers that were being searched int getProduct() const; //Returns the product that was requested }; + /* Results: The greatest product of 4 number in a line is 70600674 The numbers are 89 94 97 87 It took an average of 11.201 microseconds to run this problem over 100 iterations */ -#endif //PROBLEM11_HPP + +#endif //PROBLEM11_HPP diff --git a/headers/Problems/Problem12.hpp b/headers/Problems/Problem12.hpp index 7ccac44..d52e49a 100644 --- a/headers/Problems/Problem12.hpp +++ b/headers/Problems/Problem12.hpp @@ -1,10 +1,11 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem12.hpp //Matthew Ellison // 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? //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 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 along with this program. If not, see . */ - #ifndef PROBLEM12_HPP #define PROBLEM12_HPP -#include #include #include +#include #include "Problem.hpp" @@ -46,7 +46,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getResult(); //Return a string with the solution to the problem + virtual std::string getResult() const; //Return a string with the solution to the problem int64_t getTriangularNumber() const; //Returns the triangular number int64_t getLastNumberAdded() const; //Get the final number that was added to the triangular number std::vector 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 */ -#endif //PROBLEM12_HPP + +#endif //PROBLEM12_HPP diff --git a/headers/Problems/Problem13.hpp b/headers/Problems/Problem13.hpp index 8918bca..68ff7df 100644 --- a/headers/Problems/Problem13.hpp +++ b/headers/Problems/Problem13.hpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem13.hpp //Matthew Ellison // 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 /* 37107287533902102798797998220837590246510135740250 @@ -110,7 +110,7 @@ //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 /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -125,15 +125,13 @@ You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ - #ifndef PROBLEM13_HPP #define PROBLEM13_HPP -#include #include #include -#include "gmpxx.h" //This is part of the gmp library +#include #include "Problem.hpp" @@ -154,7 +152,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getResult(); //Return a string with the solution to the problem + virtual std::string getResult() const; //Return a string with the solution to the problem std::vector getNumbers() const; //Returns the list 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 */ -#endif //PROBLEM13_HPP + +#endif //PROBLEM13_HPP diff --git a/headers/Problems/Problem14.hpp b/headers/Problems/Problem14.hpp index 7932489..1e98f3b 100644 --- a/headers/Problems/Problem14.hpp +++ b/headers/Problems/Problem14.hpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem14.hpp //Matthew Ellison // Created: 09-29-18 -//Modified: 08-28-20 +//Modified: 07-02-21 /* The following iterative sequence is defined for the set of positive integers: 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 /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -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 along with this program. If not, see . */ - #ifndef PROBLEM14_HPP #define PROBLEM14_HPP @@ -53,14 +52,16 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getResult(); //Return a string with the solution to the problem - uint64_t getLength() const; //Returns the length of the requested chain + 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 getStartingNumber() const; //Returns the starting number of the requested chain }; + /* Results: The number 837799 produced a chain of 525 steps It took an average of 197.008 milliseconds to run this problem over 100 iterations */ -#endif //PROBLEM14_HPP + +#endif //PROBLEM14_HPP diff --git a/headers/Problems/Problem15.hpp b/headers/Problems/Problem15.hpp index 1f63216..1f3d2e1 100644 --- a/headers/Problems/Problem15.hpp +++ b/headers/Problems/Problem15.hpp @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem15.hpp //Matthew Ellison // 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? //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 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 reset(); //Reset the problem so it can be run again //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 }; + + /* Results: The number of routes is 137846528820 It took an average of 18.010 minutes to run this problem through 10 iterations */ -#endif //PROBLEM15_HPP + +#endif //PROBLEM15_HPP diff --git a/headers/Problems/Problem16.hpp b/headers/Problems/Problem16.hpp index 6edc41c..a90160f 100644 --- a/headers/Problems/Problem16.hpp +++ b/headers/Problems/Problem16.hpp @@ -1,14 +1,14 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem16.hpp //Matthew Ellison // Created: 09-28-18 -//Modified: 08-28-20 +//Modified: 07-02-21 //What is the sum of the digits of the number 2^1000? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses //This file contains a header from the gmp library. The library is used for large integers. //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 /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -23,7 +23,6 @@ You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ - #ifndef PROBLEM16_HPP #define PROBLEM16_HPP @@ -49,15 +48,17 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getResult(); //Return a string with the solution to the problem + virtual std::string getResult() const; //Return a string with the solution to the problem mpz_class getNumber() const; //Returns the number that was calculated int getSum() const; //Return the sum of the digits of the number }; + /* Results: 2^1000 = 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376 The sum of the elements is 1366 It took an average of 4.806 microseconds to run this problem over 100 iterations */ -#endif //PROBLEM16_HPP + +#endif //PROBLEM16_HPP diff --git a/headers/Problems/Problem17.hpp b/headers/Problems/Problem17.hpp index 678bd15..178b7d7 100644 --- a/headers/Problems/Problem17.hpp +++ b/headers/Problems/Problem17.hpp @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem17.hpp //Matthew Ellison // 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? //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 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 reset(); //Reset the problem so it can be run again //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 }; + + /* Results: The number of letters is 21124 It took an average of 220.126 microseconds to run this problem over 100 iterations */ -#endif //Problem17_HPP + +#endif //Problem17_HPP diff --git a/headers/Problems/Problem18.hpp b/headers/Problems/Problem18.hpp index dfeaf00..c5d60ec 100644 --- a/headers/Problems/Problem18.hpp +++ b/headers/Problems/Problem18.hpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem18.hpp //Matthew Ellison // Created: 11-01-18 -//Modified: 08-28-20 +//Modified: 07-02-21 //Find the maximum total from top to bottom /* 75 @@ -23,7 +23,7 @@ //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 /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -38,14 +38,13 @@ You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ - #ifndef PROBLEM18_HPP #define PROBLEM18_HPP -#include #include #include +#include #include "Problem.hpp" @@ -69,7 +68,7 @@ private: //Functions void invert(); //This list turns every number in the vector into 100 - num - void setupList(); + void setupList(); //Setup the list of numbers protected: //Variables //Static variables @@ -81,10 +80,10 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getResult(); //Return a string with the solution to the problem - std::string getPyramid(); //Returns the pyramid that was traversed as a string - std::string getTrail(); //Returns the trail the algorithm took as a string - int getTotal() const; //Returns the total that was asked for + virtual std::string getResult() const; //Return a string with the solution to the problem + std::string getPyramid() const; //Returns the pyramid that was traversed as a string + std::string getTrail() const; //Returns the trail the algorithm took as a string + int getTotal() const; //Returns the total that was asked for }; /* 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 */ -#endif //PROBLEM18_HPP +#endif //PROBLEM18_HPP diff --git a/headers/Problems/Problem19.hpp b/headers/Problems/Problem19.hpp index 9eb3a3e..5a2c218 100644 --- a/headers/Problems/Problem19.hpp +++ b/headers/Problems/Problem19.hpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem19.hpp //Matthew Ellison // 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)? /* 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 /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -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 along with this program. If not, see . */ - #ifndef PROBLEM19_HPP #define PROBLEM19_HPP @@ -62,12 +61,15 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getResult(); //Return a string with the solution to the problem - uint64_t getTotalSundays() const; //Returns the total sundays that were asked for + 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 }; + + /* Results 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 */ -#endif //PROBLEM19_HPP + +#endif //PROBLEM19_HPP diff --git a/headers/Problems/Problem2.hpp b/headers/Problems/Problem2.hpp index 64a1d05..f8164b1 100644 --- a/headers/Problems/Problem2.hpp +++ b/headers/Problems/Problem2.hpp @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem2.hpp //Matthew Ellison // Created: 09-28-18 -//Modified: 08-28-20 +//Modified: 07-02-21 //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 /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -20,11 +20,10 @@ You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ - - #ifndef PROBLEM2_HPP #define PROBLEM2_HPP + #include #include #include "Problem.hpp" @@ -44,13 +43,15 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getResult(); //Return a string with the solution to the problem + virtual std::string getResult() const; //Return a string with the solution to the problem uint64_t getSum() const; //Returns the requested sum }; + /* Results: 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 */ -#endif //PROBLEM2_HPP + +#endif //PROBLEM2_HPP diff --git a/headers/Problems/Problem20.hpp b/headers/Problems/Problem20.hpp index d1175d6..c6e96e0 100644 --- a/headers/Problems/Problem20.hpp +++ b/headers/Problems/Problem20.hpp @@ -1,14 +1,14 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem20.hpp //Matthew Ellison // Created: 11-07-18 -//Modified: 08-28-20 +//Modified: 07-02-21 //What is the sum of the digits of 100!? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses //This file contains a header from the gmp library. The library is used for large integers. //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 /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -23,7 +23,6 @@ You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ - #ifndef PROBLEM20_HPP #define PROBLEM20_HPP @@ -47,16 +46,18 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getResult(); //Return a string with the solution to the problem + virtual std::string getResult() const; //Return a string with the solution to the problem mpz_class getNumber() const; //Returns the number 100! std::string getNumberString() const; //Returns the number 100! in a string uint64_t getSum() const; //Returns the sum of the digits of 100! }; + /* Results: 100! = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 The sum of the digits is: 648 It took an average of 4.094 microseconds to run this problem over 100 iterations */ -#endif //PROBLEM20_HPP + +#endif //PROBLEM20_HPP diff --git a/headers/Problems/Problem21.hpp b/headers/Problems/Problem21.hpp index d9c0344..d8aed34 100644 --- a/headers/Problems/Problem21.hpp +++ b/headers/Problems/Problem21.hpp @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem21.hpp //Matthew Ellison // Created: 11-08-18 -//Modified: 08-28-20 +//Modified: 07-02-21 //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 /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -20,14 +20,13 @@ You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ - #ifndef PROBLEM21_HPP #define PROBLEM21_HPP #include -#include #include +#include #include "Problem.hpp" @@ -49,7 +48,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getResult(); //Return a string with the solution to the problem + virtual std::string getResult() const; //Return a string with the solution to the problem std::vector 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 }; @@ -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 */ -#endif //PROBLEM21_HPP + +#endif //PROBLEM21_HPP diff --git a/headers/Problems/Problem22.hpp b/headers/Problems/Problem22.hpp index dd31a0b..3f157d0 100644 --- a/headers/Problems/Problem22.hpp +++ b/headers/Problems/Problem22.hpp @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem22.hpp //Matthew Ellison // Created: 11-09-18 -//Modified: 08-28-20 +//Modified: 07-02-21 //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 /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -20,7 +20,6 @@ You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ - #ifndef PROBLEM22_HPP #define PROBLEM22_HPP @@ -50,14 +49,16 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getResult(); //Return a string with the solution to the problem + virtual std::string getResult() const; //Return a string with the solution to the problem std::vector getNames() const; //Returns the vector of the names being scored uint64_t getNameScoreSum() const; //Returns the sum of the names scores }; + /* Results: The answer to the question is 871198282 It took an average of 436.559 microseconds to run this problem over 100 iterations */ -#endif //Problem22 + +#endif //Problem22 diff --git a/headers/Problems/Problem23.hpp b/headers/Problems/Problem23.hpp index 593956e..27e0b60 100644 --- a/headers/Problems/Problem23.hpp +++ b/headers/Problems/Problem23.hpp @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem23.hpp //Matthew Ellison // 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 //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 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 along with this program. If not, see . */ - #ifndef PROBLEM23_HPP #define PROBLEM23_HPP -#include #include #include +#include #include "Problem.hpp" @@ -50,13 +49,15 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getResult(); //Return a string with the solution to the problem + 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 }; + /* Results: The answer is 4179871 It took an average of 5.902 seconds to run this problem over 100 iterations */ -#endif //PROBLEM23_HPP + +#endif //PROBLEM23_HPP diff --git a/headers/Problems/Problem24.hpp b/headers/Problems/Problem24.hpp index c54c5de..e177948 100644 --- a/headers/Problems/Problem24.hpp +++ b/headers/Problems/Problem24.hpp @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem24.hpp //Matthew Ellison // 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? //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 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 along with this program. If not, see . */ - #ifndef PROBLEM24_HPP #define PROBLEM24_HPP @@ -44,14 +43,16 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getResult(); //Return a string with the solution to the problem + virtual std::string getResult() const; //Return a string with the solution to the problem std::vector getPermutationsList() const; //Returns a vector with all of the permutations std::string getPermutation() const; //Returns the specific permutations you are looking for }; + /* Results The 1 millionth permutation is 2783915460 It took an average of 1.157 seconds to run this problem over 100 iterations */ -#endif //PROBLEM24_HPP + +#endif //PROBLEM24_HPP diff --git a/headers/Problems/Problem25.hpp b/headers/Problems/Problem25.hpp index 5fc8d7a..8abe2fe 100644 --- a/headers/Problems/Problem25.hpp +++ b/headers/Problems/Problem25.hpp @@ -1,14 +1,14 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem25.hpp //Matthew Ellison // 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? //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. //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 /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -23,13 +23,12 @@ You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ - #ifndef PROBLEM25_HPP #define PROBLEM25_HPP #include -#include "gmpxx.h" +#include #include "Problem.hpp" @@ -48,7 +47,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getResult(); //Return a string with the solution to the problem + virtual std::string getResult() const; //Return a string with the solution to the problem mpz_class getNumber() const; //Returns the Fibonacci number asked for std::string getNumberString() const; //Returns the Fibonacci number asked for as a string mpz_class getIndex() const; //Returns the index of the requested Fibonacci number @@ -62,4 +61,4 @@ Its index is 4782 It took an average of 241.017 milliseconds to run this problem over 100 iterations */ -#endif //PROBLEM25_HPP +#endif //PROBLEM25_HPP diff --git a/headers/Problems/Problem26.hpp b/headers/Problems/Problem26.hpp index cd675a1..c558f4b 100644 --- a/headers/Problems/Problem26.hpp +++ b/headers/Problems/Problem26.hpp @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem26.hpp //Matthew Ellison // 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. //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 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 along with this program. If not, see . */ - - #ifndef PROBLEM26_HPP #define PROBLEM26_HPP @@ -45,7 +43,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getResult(); //Return a string with the solution to the problem + 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 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 */ -#endif //PROBLEM26_HPP + +#endif //PROBLEM26_HPP diff --git a/headers/Problems/Problem27.hpp b/headers/Problems/Problem27.hpp index 92c5f1d..4d99771 100644 --- a/headers/Problems/Problem27.hpp +++ b/headers/Problems/Problem27.hpp @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem27.hpp //Matthew Ellison // 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. //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 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 along with this program. If not, see . */ - #ifndef PROBLEM27_HPP #define PROBLEM27_HPP -#include #include +#include #include #include "Problem.hpp" @@ -46,12 +45,13 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getResult(); //Return a string with the solution to the problem + 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 getTopB() const; //Returns the top B that was generated int64_t getTopN() const; //Returns the top N that was generated }; + /* Results: The greatest number of primes found is 70 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 */ -#endif //PROBLEM27_HPP + +#endif //PROBLEM27_HPP diff --git a/headers/Problems/Problem28.hpp b/headers/Problems/Problem28.hpp index b0055ea..86d366d 100644 --- a/headers/Problems/Problem28.hpp +++ b/headers/Problems/Problem28.hpp @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem28.hpp //Matthew Ellison // 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 //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 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 along with this program. If not, see . */ - - #ifndef PROBLEM28_HPP #define PROBLEM28_HPP -#include #include +#include #include #include "Problem.hpp" @@ -50,14 +48,16 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getResult(); //Return a string with the solution to the problem + virtual std::string getResult() const; //Return a string with the solution to the problem std::vector> getGrid() const; //Returns the grid uint64_t getSum() const; //Returns the sum of the diagonals }; + /* Results: 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 */ -#endif //PROBLEM28_HPP + +#endif //PROBLEM28_HPP diff --git a/headers/Problems/Problem29.hpp b/headers/Problems/Problem29.hpp index c852e94..3f06577 100644 --- a/headers/Problems/Problem29.hpp +++ b/headers/Problems/Problem29.hpp @@ -1,14 +1,14 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem29.hpp //Matthew Ellison // 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? //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. //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 /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -23,14 +23,12 @@ You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ - - #ifndef PROBLEM29_HPP #define PROBLEM29_HPP -#include #include +#include #include #include #include "Problem.hpp" @@ -41,9 +39,9 @@ private: //Variables //Static variables 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 TOP_B; //The highest possible value for b + static unsigned int TOP_B; //The highest possible value for b //Instance variables std::vector unique; //Holds all values in powers, except repeats public: @@ -53,17 +51,20 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getResult(); //Return a string with the solution to the problem + 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 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 getTopB() const; //Returns the highest possible value for b + unsigned int getTopB() const; //Returns the highest possible value for b std::vector 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: 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 */ -#endif //PROBLEM29_HPP + +#endif //PROBLEM29_HPP diff --git a/headers/Problems/Problem3.hpp b/headers/Problems/Problem3.hpp index b2d9cd8..e3e23f7 100644 --- a/headers/Problems/Problem3.hpp +++ b/headers/Problems/Problem3.hpp @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem3.hpp //Matthew Ellison // Created: 09-28-18 -//Modified: 08-28-20 +//Modified: 07-02-21 //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 /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -20,14 +20,13 @@ You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ - #ifndef PROBLEM3_HPP #define PROBLEM3_HPP -#include #include #include +#include #include "Problem.hpp" @@ -45,15 +44,16 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getResult(); //Return a string with the solution to the problem + virtual std::string getResult() const; //Return a string with the solution to the problem std::vector getFactors() const; //Returns the list of factors of the number uint64_t getLargestFactor() const; //Returns the largest factor of the number - uint64_t getGoalNumber() const; //Returns the number }; + /* Results: The largest factor of the number 600851475143 is 6857 It took an average of 50.300 milliseconds to run this problem over 100 iterations */ -#endif //PROBLEM3_HPP + +#endif //PROBLEM3_HPP diff --git a/headers/Problems/Problem30.hpp b/headers/Problems/Problem30.hpp index a52393a..5a39f26 100644 --- a/headers/Problems/Problem30.hpp +++ b/headers/Problems/Problem30.hpp @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem30.hpp //Matthew Ellison // 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. //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 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 along with this program. If not, see . */ - #ifndef PROBLEM30_HPP #define PROBLEM30_HPP -#include #include +#include #include #include "Problem.hpp" @@ -51,7 +50,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getResult(); //Return a string with the solution to the problem + 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 std::vector 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 diff --git a/headers/Problems/Problem31.hpp b/headers/Problems/Problem31.hpp index a777961..ea37622 100644 --- a/headers/Problems/Problem31.hpp +++ b/headers/Problems/Problem31.hpp @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem31.hpp //Matthew Ellison // 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? //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 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 along with this program. If not, see . */ - #ifndef PROBLEM31_HPP #define PROBLEM31_HPP #include -#include #include "Problem.hpp" @@ -44,7 +42,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getResult(); //Return a string with the solution to the problem + 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 }; diff --git a/headers/Problems/Problem32.hpp b/headers/Problems/Problem32.hpp index 1940586..8540d07 100644 --- a/headers/Problems/Problem32.hpp +++ b/headers/Problems/Problem32.hpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem32.hpp //Matthew Ellison // 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. //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 along with this program. If not, see . */ - #ifndef PROBLEM32_HPP #define PROBLEM32_HPP @@ -69,8 +68,8 @@ public: void solve(); //Solve the problem void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getResult(); //Return a string with the solution to the problem - int64_t getSumOfPandigitals(); //Returns the sum of the pandigitals + virtual std::string getResult() const; //Return a string with the solution to the problem + int64_t getSumOfPandigitals() const; //Returns the sum of the pandigitals }; /* Results: diff --git a/headers/Problems/Problem33.hpp b/headers/Problems/Problem33.hpp index 04ff3ee..671d3bc 100644 --- a/headers/Problems/Problem33.hpp +++ b/headers/Problems/Problem33.hpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem33.hpp //Matthew Ellison -// Created: 02-05-2021 -//Modified: 02-05-2021 +// Created: 02-05-21 +//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 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 along with this program. If not, see . */ - #ifndef PROBLEM33_HPP #define PROBLEM33_HPP -#include #include #include #include "Problem.hpp" @@ -40,14 +38,14 @@ class Problem33: public Problem{ private: //Variables //Static variables - static int MIN_NUMERATOR; //The lowest the numerator can be - static int MAX_NUMERATOR; //The highest 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 MIN_DENOMINATOR; //The lowest the denominator can be static int MAX_DENOMINATOR; //The highest the denominator can be //Instance variables std::vector numerators; //Holds the numerators that were found std::vector denominators; //Holds the denominators that were found - int prodDenominator; //Holds the answer to the question + int prodDenominator; //Holds the answer to the question public: //Constructor Problem33(); @@ -55,15 +53,17 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getResult(); //Return a string with the solution to the problem - std::vector getNumerators(); //Returns the list of numerators - std::vector getDenominators(); //Returns the list of denominators - int getProdDenominator(); //Returns the answer to the question + virtual std::string getResult() const; //Return a string with the solution to the problem + std::vector getNumerators() const; //Returns the list of numerators + std::vector getDenominators() const; //Returns the list of denominators + int getProdDenominator() const; //Returns the answer to the question }; + /* Results: The denominator of the product is 100 It took an average of 69.741 microseconds to run this problem over 100 iterations */ -#endif //PROBLEM33_HPP + +#endif //PROBLEM33_HPP diff --git a/headers/Problems/Problem34.hpp b/headers/Problems/Problem34.hpp index 75d00d1..943454c 100644 --- a/headers/Problems/Problem34.hpp +++ b/headers/Problems/Problem34.hpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem34.hpp //Matthew Ellison // 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 //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 along with this program. If not, see . */ - #ifndef PROBLEM34_HPP #define PROBLEM34_HPP @@ -45,14 +44,16 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getResult(); //Return a string with the solution to the problem - std::vector getFactorials(); //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 + virtual std::string getResult() const; //Return a string with the solution to the problem + std::vector getFactorials() const; //Returns the list of factorials from 0-9 + int getSum() const; //Returns the sum of all numbers equal to the sum of their digit's factorials }; + /* Results: 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 */ -#endif //PROBLEM34_HPP + +#endif //PROBLEM34_HPP diff --git a/headers/Problems/Problem35.hpp b/headers/Problems/Problem35.hpp index 41ce025..e8327ec 100644 --- a/headers/Problems/Problem35.hpp +++ b/headers/Problems/Problem35.hpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem35.hpp //Matthew Ellison // Created: 06-02-21 -//Modified: 06-02-21 +//Modified: 07-02-21 //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 /* @@ -20,7 +20,6 @@ You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ - #ifndef PROBLEM35_HPP #define PROBLEM35_HPP @@ -47,15 +46,17 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getResult(); //Returns a string with the solution to the problem - std::vector getPrimes(); //Returns the vector of primes < MAX_NUM - std::vector getCircularPrimes(); //Returns the vector of circular primes < MAX_NUM - int getNumCircularPrimes(); //Returns the number of circular primes + virtual std::string getResult() const; //Returns a string with the solution to the problem + std::vector getPrimes() const; //Returns the vector of primes < MAX_NUM + std::vector getCircularPrimes() const; //Returns the vector of circular primes < MAX_NUM + int getNumCircularPrimes() const; //Returns the number of circular primes }; + /* Results: 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 */ -#endif //PROBLEM35_HPP + +#endif //PROBLEM35_HPP diff --git a/headers/Problems/Problem36.hpp b/headers/Problems/Problem36.hpp index e53726e..ea43937 100644 --- a/headers/Problems/Problem36.hpp +++ b/headers/Problems/Problem36.hpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem36.hpp //Matthew Ellison // 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. //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 along with this program. If not, see . */ - #ifndef PROBLEM36_HPP #define PROBLEM36_HPP @@ -28,7 +27,6 @@ #include #include #include "Problem.hpp" -#include "Algorithms.hpp" class Problem36 : public Problem{ @@ -47,11 +45,12 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getResult(); //Returns a string with the solution to the problem - std::vector getPalindromes(); //Return the vector of palindromes < MAX_NUM - int getSumOfPalindromes(); //Return the sum of all elements in the vector of palindromes + virtual std::string getResult() const; //Returns a string with the solution to the problem + std::vector getPalindromes() const; //Return the vector of palindromes < MAX_NUM + int getSumOfPalindromes() const; //Return the sum of all elements in the vector of palindromes }; + /* Results: 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 diff --git a/headers/Problems/Problem37.hpp b/headers/Problems/Problem37.hpp index f3d2b90..dea6213 100644 --- a/headers/Problems/Problem37.hpp +++ b/headers/Problems/Problem37.hpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem37.hpp //Matthew Ellison // 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). //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 along with this program. If not, see . */ - #ifndef PROBLEM37_HPP #define PROBLEM37_HPP +#include #include #include #include "Problem.hpp" @@ -46,11 +46,12 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getResult(); //Returns a string with the solution to the problem - std::vector getTruncatablePrimes(); //Returns the list of primes that can be truncated - uint64_t getSumOfPrimes(); //Get the sum of all primes in truncPrimes + virtual std::string getResult() const; //Returns a string with the solution to the problem + std::vector getTruncatablePrimes() const; //Returns the list of primes that can be truncated + uint64_t getSumOfPrimes() const; //Get the sum of all primes in truncPrimes }; + /* Results: 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 diff --git a/headers/Problems/Problem4.hpp b/headers/Problems/Problem4.hpp index 96047e3..77050ae 100644 --- a/headers/Problems/Problem4.hpp +++ b/headers/Problems/Problem4.hpp @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem4.hpp //Matthew Ellison // 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 //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 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 along with this program. If not, see . */ - #ifndef PROBLEM4_HPP #define PROBLEM4_HPP -#include +#include #include +#include #include "Problem.hpp" @@ -45,7 +45,7 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getResult(); //Return a string with the solution to the problem + virtual std::string getResult() const; //Return a string with the solution to the problem std::vector getPalindromes() const; //Returns the list of all palindromes 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 */ -#endif //PROBLEM4_HPP + +#endif //PROBLEM4_HPP diff --git a/headers/Problems/Problem5.hpp b/headers/Problems/Problem5.hpp index 107199e..0852280 100644 --- a/headers/Problems/Problem5.hpp +++ b/headers/Problems/Problem5.hpp @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem5.hpp //Matthew Ellison // 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? //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 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 along with this program. If not, see . */ - #ifndef PROBLEM5_HPP #define PROBLEM5_HPP -#include -#include #include #include "Problem.hpp" @@ -43,13 +40,15 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getResult(); //Return a string with the solution to the problem + virtual std::string getResult() const; //Return a string with the solution to the problem int getNumber() const; //Returns the requested number }; + /* Results: 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 diff --git a/headers/Problems/Problem6.hpp b/headers/Problems/Problem6.hpp index c0d43bf..fa746fd 100644 --- a/headers/Problems/Problem6.hpp +++ b/headers/Problems/Problem6.hpp @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem6.hpp //Matthew Ellison // 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. //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 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 along with this program. If not, see . */ - #ifndef PROBLEM6_HPP #define PROBLEM6_HPP @@ -46,15 +45,17 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getResult(); //Return a string with the solution to the problem + 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 getSquareOfSum() const; //Returns the square of all of the sums 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 It took an average of 76.000 nanoseconds to run this problem over 100 iterations */ -#endif //PROBLEM6_HPP + +#endif //PROBLEM6_HPP diff --git a/headers/Problems/Problem67.hpp b/headers/Problems/Problem67.hpp index ff96574..928b58f 100644 --- a/headers/Problems/Problem67.hpp +++ b/headers/Problems/Problem67.hpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem67.hpp //Matthew Ellison // Created: 11-02-18 -//Modified: 07-09-20 +//Modified: 07-02-21 //The way to do this is using a breadth first search /* 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 /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -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 along with this program. If not, see . */ - #ifndef PROBLEM67_HPP #define PROBLEM67_HPP +#include "Problem67.hpp" #include "Problem18.hpp" @@ -136,15 +136,14 @@ class Problem67 : public Problem18{ private: void setupList(); public: - Problem67() : Problem18(){ - list.clear(); - setupList(); - } + Problem67(); }; + /* Results: The value of the longest path is 7273 It took an average of 366.373 milliseconds to run this problem over 100 iterations */ -#endif //PROBLEM67_HPP + +#endif //PROBLEM67_HPP diff --git a/headers/Problems/Problem7.hpp b/headers/Problems/Problem7.hpp index 3bc9c25..92cd2e0 100644 --- a/headers/Problems/Problem7.hpp +++ b/headers/Problems/Problem7.hpp @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem7.hpp //Matthew Ellison // Created: 09-28-18 -//Modified: 08-28-20 +//Modified: 07-02-21 //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 /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -20,14 +20,13 @@ You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ - #ifndef PROBLEM7_HPP #define PROBLEM7_HPP -#include #include #include +#include #include "Problem.hpp" @@ -45,13 +44,15 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getResult(); //Return a string with the solution to the problem + virtual std::string getResult() const; //Return a string with the solution to the problem uint64_t getPrime() const; //Returns the requested prime number }; -/* Results + +/* Results: The 10001th prime number is 104743 It took an average of 3.864 milliseconds to run this problem over 100 iterations */ -#endif //PROBLEM7_HPP + +#endif //PROBLEM7_HPP diff --git a/headers/Problems/Problem8.hpp b/headers/Problems/Problem8.hpp index f354092..7cefac4 100644 --- a/headers/Problems/Problem8.hpp +++ b/headers/Problems/Problem8.hpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem8.hpp //Matthew Ellison // 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? /* 73167176531330624919225119674426574742355349194934 @@ -27,7 +27,7 @@ */ //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 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 along with this program. If not, see . */ - #ifndef PROBLEM8_HPP #define PROBLEM8_HPP #include #include -#include #include "Problem.hpp" @@ -68,16 +66,17 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getResult(); //Return a string with the solution to the problem + 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 uint64_t getLargestProduct() const; //Returns the requested product }; -/* Results +/* Results: The greatest product is 23514624000 The numbers are 5576689664895 It took an average of 129.024 microseconds to run this problem over 100 iterations */ -#endif //PROBLEM8_HPP + +#endif //PROBLEM8_HPP diff --git a/headers/Problems/Problem9.hpp b/headers/Problems/Problem9.hpp index cb26f62..4e91898 100644 --- a/headers/Problems/Problem9.hpp +++ b/headers/Problems/Problem9.hpp @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCPP/headers/Problems/Problem9.hpp //Matthew Ellison // 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. //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 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 along with this program. If not, see . */ - #ifndef PROBLEM9_HPP #define PROBLEM9_HPP -#include #include #include "Problem.hpp" @@ -47,17 +45,19 @@ public: virtual void solve(); //Solve the problem virtual void reset(); //Reset the problem so it can be run again //Gets - virtual std::string getResult(); //Return a string with the solution to the problem + 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 getSideB() const; //Returns the length of the second side int getSideC() const; //Returns the length of the hyp int getProduct() const; //Returns the product of the 3 sides }; + /* Results: The Pythagorean triplet is 200 375 425 The numbers' product is 31875000 It took an average of 154.595 microseconds to run this problem over 100 iterations */ -#endif //PROBLEM9_HPP + +#endif //PROBLEM9_HPP diff --git a/headers/Unsolved.hpp b/headers/Unsolved.hpp index 6f0ab29..09ffd36 100644 --- a/headers/Unsolved.hpp +++ b/headers/Unsolved.hpp @@ -1,10 +1,24 @@ //ProjectEuler/ProjectEulerCPP/headers/Unsolved.hpp //Matthew Ellison // 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 +/* + 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 . +*/ #ifndef UNSOLVED_HPP #define UNSOLVED_HPP diff --git a/headers/benchmark.hpp b/headers/benchmark.hpp index 338eb37..a15a415 100644 --- a/headers/benchmark.hpp +++ b/headers/benchmark.hpp @@ -1,10 +1,10 @@ //ProjectEuler/ProjectEulerCPP/headers/benchmark.hpp //Matthew Ellison // 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 /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -19,11 +19,10 @@ You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ - - #ifndef BENCHMARK_HPP #define BENCHMARK_HPP + #include #include #include "ProblemSelection.hpp" diff --git a/makefile b/makefile index 5a946db..0a4c4c0 100644 --- a/makefile +++ b/makefile @@ -1,7 +1,7 @@ NUMCORES = $(shell grep -c "^processor" /proc/cpuinfo) NUMCORESWIN = ${NUMBER_OF_PROCESSORS} -LIBFLAGS = -shared -std=c++17 -O3 -fPIC -Wall -EXEFLAGS = -Wall -std=c++17 -O3 -Wl,-rpath,'$$ORIGIN/lib' +LIBFLAGS = -shared -std=c++20 -O3 -fPIC -Wall -fcoroutines +EXEFLAGS = -Wall -std=c++20 -O3 -Wl,-rpath,'$$ORIGIN/lib' 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 SOURCE_DIR = src diff --git a/src/Problems/Problem1.cpp b/src/Problems/Problem1.cpp index 1a14965..2e3ff07 100644 --- a/src/Problems/Problem1.cpp +++ b/src/Problems/Problem1.cpp @@ -1,11 +1,11 @@ -//ProjectEuler/ProjectEulerCPP/Source/Problem1.cpp +//ProjectEuler/ProjectEulerCPP/src/Problems/Problem1.cpp //Matthew Ellison // 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 //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 it under the terms of the GNU Lesser General Public License as published by @@ -22,11 +22,11 @@ */ -#include -#include #include -#include #include +#include +#include +#include #include "Problems/Problem1.hpp" @@ -51,12 +51,15 @@ void Problem1::solve(){ if(solved){ return; } + //Start the timer timer.start(); + //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); + //Stop the timer timer.stop(); @@ -71,19 +74,14 @@ void Problem1::reset(){ //Gets //Return a string with the solution to the problem -std::string Problem1::getResult(){ - if(!solved){ - throw Unsolved(); - } +std::string Problem1::getResult() const{ + Problem::solvedCheck("result"); std::stringstream result; result << "The sum of all the numbers < " << MAX_NUMBER + 1 << " that are divisible by 3 or 5 is " << fullSum; return result.str(); } //Returns the requested sum uint64_t Problem1::getSum() const{ - //If the prblem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + Problem::solvedCheck("sum"); return fullSum; } diff --git a/src/Problems/Problem10.cpp b/src/Problems/Problem10.cpp index f5f5534..42ee28d 100644 --- a/src/Problems/Problem10.cpp +++ b/src/Problems/Problem10.cpp @@ -1,11 +1,11 @@ -//ProjectEuler/ProjectEulerCPP/Source/Problem10.cpp +//ProjectEuler/ProjectEulerCPP/src/Problems/Problem10.cpp //Matthew Ellison // Created: 09-28-18 -//Modified: 08-28-20 +//Modified: 07-02-21 //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 /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -23,9 +23,10 @@ #include -#include #include -#include "Algorithms.hpp" +#include +#include "mee/numberAlgorithms.hpp" +#include "mee/vectorAlgorithms.hpp" #include "Problems/Problem10.hpp" @@ -42,12 +43,15 @@ void Problem10::solve(){ if(solved){ return; } + //Start the timer timer.start(); + //Get the sum of all prime numbers <= GOAL_NUMBER sum = mee::getSum(mee::getPrimes(GOAL_NUMBER)); + //Stop the timer timer.stop(); @@ -62,19 +66,14 @@ void Problem10::reset(){ //Gets //Return a string with the solution to the problem -std::string Problem10::getResult(){ - if(!solved){ - throw Unsolved(); - } +std::string Problem10::getResult() const{ + solvedCheck("result"); std::stringstream result; result << "The sum of all the primes less than " << GOAL_NUMBER + 1 << " is " << sum; return result.str(); } //Returns the sum that was requested uint64_t Problem10::getSum() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("sum"); return sum; } diff --git a/src/Problems/Problem11.cpp b/src/Problems/Problem11.cpp index f89fbff..c8054f0 100644 --- a/src/Problems/Problem11.cpp +++ b/src/Problems/Problem11.cpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Source/Problem11.cpp +//ProjectEuler/ProjectEulerCPP/src/Problems/Problem11.cpp //Matthew Ellison // 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? /* 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 /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -44,10 +44,10 @@ */ -#include -#include #include -#include "Algorithms.hpp" +#include +#include +#include "mee/vectorAlgorithms.hpp" #include "Problems/Problem11.hpp" @@ -93,6 +93,7 @@ void Problem11::solve(){ //Start the timer timer.start(); + //Loop through every row and column for(unsigned int row = 0;row < 20;++row){ for(unsigned int col = 0;col < grid[row].size();++col){ @@ -117,10 +118,10 @@ void Problem11::solve(){ //Right if(right){ //Fill the product - currentProduct.at(0) = grid[row].at(col); - currentProduct.at(1) = grid[row].at(col + 1); - currentProduct.at(2) = grid[row].at(col + 2); - currentProduct.at(3) = grid[row].at(col + 3); + currentProduct[0] = grid[row][col]; + currentProduct[1] = grid[row][col + 1]; + currentProduct[2] = grid[row][col + 2]; + currentProduct[3] = grid[row][col + 3]; //If the current numbers' product is greater than the greatest product replace it if(mee::getProduct(currentProduct) > mee::getProduct(greatestProduct)){ @@ -130,10 +131,10 @@ void Problem11::solve(){ //Down if(down){ //Fill the product - currentProduct.at(0) = grid[row].at(col); - currentProduct.at(1) = grid[row + 1].at(col); - currentProduct.at(2) = grid[row + 2].at(col); - currentProduct.at(3) = grid[row + 3].at(col); + currentProduct[0] = grid[row][col]; + currentProduct[1] = grid[row + 1][col]; + currentProduct[2] = grid[row + 2][col]; + currentProduct[3] = grid[row + 3][col]; //If the current numbers' product is greater than the greatest product replace it if(mee::getProduct(currentProduct) > mee::getProduct(greatestProduct)){ @@ -143,10 +144,10 @@ void Problem11::solve(){ //LeftDown if(left && down){ //Fill the product - currentProduct.at(0) = grid[row].at(col); - currentProduct.at(1) = grid[row + 1].at(col - 1); - currentProduct.at(2) = grid[row + 2].at(col - 2); - currentProduct.at(3) = grid[row + 3].at(col - 3); + currentProduct[0] = grid[row][col]; + currentProduct[1] = grid[row + 1][col - 1]; + currentProduct[2] = grid[row + 2][col - 2]; + currentProduct[3] = grid[row + 3][col - 3]; //If the current numbers' product is greater than the greatest product replace it if(mee::getProduct(currentProduct) > mee::getProduct(greatestProduct)){ @@ -156,10 +157,10 @@ void Problem11::solve(){ //RightDown if(right && down){ //Fill the product - currentProduct.at(0) = grid[row].at(col); - currentProduct.at(1) = grid[row + 1].at(col + 1); - currentProduct.at(2) = grid[row + 2].at(col + 2); - currentProduct.at(3) = grid[row + 3].at(col + 3); + currentProduct[0] = grid[row][col]; + currentProduct[1] = grid[row + 1][col + 1]; + currentProduct[2] = grid[row + 2][col + 2]; + currentProduct[3] = grid[row + 3][col + 3]; //If the current numbers' product is greater than the greatest product replace it if(mee::getProduct(currentProduct) > mee::getProduct(greatestProduct)){ @@ -169,6 +170,7 @@ void Problem11::solve(){ } } + //Stop the timer timer.stop(); @@ -183,28 +185,20 @@ void Problem11::reset(){ //Gets //Return a string with the solution to the problem -std::string Problem11::getResult(){ - if(!solved){ - throw Unsolved(); - } +std::string Problem11::getResult() const{ + solvedCheck("result"); std::stringstream result; result << "The greatest product of 4 number in a line is " << mee::getProduct(greatestProduct) - << "\nThe numbers are " << greatestProduct.at(0) << ' ' << greatestProduct.at(1) << ' ' << greatestProduct.at(2) << ' ' << greatestProduct.at(3); + << "\nThe numbers are " << greatestProduct[0] << ' ' << greatestProduct[1] << ' ' << greatestProduct[2] << ' ' << greatestProduct[3]; return result.str(); } //Returns the numbers that were being searched std::vector Problem11::getNumbers() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("numbers"); return greatestProduct; } //Returns the product that was requested int Problem11::getProduct() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("product of the numbers"); return mee::getProduct(greatestProduct); } diff --git a/src/Problems/Problem12.cpp b/src/Problems/Problem12.cpp index c38e446..694299d 100644 --- a/src/Problems/Problem12.cpp +++ b/src/Problems/Problem12.cpp @@ -1,10 +1,10 @@ -//ProjectEuler/ProjectEulerCPP/Source/Problem12.cpp +//ProjectEuler/ProjectEulerCPP/src/Problems/Problem12.cpp //Matthew Ellison // 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? //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 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 . */ -#include + #include -#include #include -#include "Algorithms.hpp" +#include +#include +#include "mee/numberAlgorithms.hpp" #include "Problems/Problem12.hpp" @@ -48,6 +49,7 @@ void Problem12::solve(){ //Start the timer timer.start(); + while(!foundNumber){ divisors = mee::getDivisors(sum); //If the number of divisors is correct set the flag. It must be > 500 @@ -61,6 +63,7 @@ void Problem12::solve(){ } } + //Stop the timer timer.stop(); @@ -77,43 +80,32 @@ void Problem12::reset(){ //Gets //Return a string with the solution to the problem -std::string Problem12::getResult(){ - if(!solved){ - throw Unsolved(); - } +std::string Problem12::getResult() const{ + solvedCheck("result"); std::stringstream result; result << "The triangular number " << sum << " is a sum of all numbers >= " << counter - 1 << " and has " << divisors.size() << " divisors"; return result.str(); } //Returns the triangular number int64_t Problem12::getTriangularNumber() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("triangular number"); return sum; } //Get the final number that was added to the triangular number int64_t Problem12::getLastNumberAdded() const{ //If the problem hasn't been solved throw an exception 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; } //Returns the list of divisors of the requested number std::vector Problem12::getDivisorsOfTriangularNumber() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("divisors of the triangular number"); return divisors; } //Returns the number of divisors of the requested number size_t Problem12::getNumberOfDivisors() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("number of divisors of the triangular number"); return divisors.size(); } diff --git a/src/Problems/Problem13.cpp b/src/Problems/Problem13.cpp index 7504acb..3c95cc0 100644 --- a/src/Problems/Problem13.cpp +++ b/src/Problems/Problem13.cpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Source/Problem13.cpp +//ProjectEuler/ProjectEulerCPP/src/Problems/Problem13.cpp //Matthew Ellison // 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 /* 37107287533902102798797998220837590246510135740250 @@ -110,7 +110,7 @@ //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 /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -131,8 +131,8 @@ #include #include #include -#include "gmpxx.h" //This is part of the gmp library -#include "Algorithms.hpp" +#include //This is part of the gmp library +#include "mee/vectorAlgorithms.hpp" #include "Problems/Problem13.hpp" @@ -284,10 +284,8 @@ void Problem13::reset(){ //Gets //Return a string with the solution to the problem -std::string Problem13::getResult(){ - if(!solved){ - throw Unsolved(); - } +std::string Problem13::getResult() const{ + solvedCheck("result"); std::stringstream result; result << "The sum of all " << nums.size() << " numbers is " << sum << "\nThe first 10 digits of the sum of the numbers is " << sum.get_str().substr(0, 10); @@ -295,17 +293,11 @@ std::string Problem13::getResult(){ } //Returns the list 50-digit numbers std::vector Problem13::getNumbers() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("numbers"); return nums; } //Returns the sum of the 50-digit numbers mpz_class Problem13::getSum() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("sum"); return sum; } diff --git a/src/Problems/Problem14.cpp b/src/Problems/Problem14.cpp index f58fc97..1b617e9 100644 --- a/src/Problems/Problem14.cpp +++ b/src/Problems/Problem14.cpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Source/Problem14.cpp +//ProjectEuler/ProjectEulerCPP/src/Problems/Problem14.cpp //Matthew Ellison // Created: 09-29-18 -//Modified: 08-28-20 +//Modified: 07-02-21 /* The following iterative sequence is defined for the set of positive integers: 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 /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -28,8 +28,8 @@ Which starting number, under one million, produces the longest chain? #include -#include #include +#include #include "Problems/Problem14.hpp" @@ -67,6 +67,7 @@ void Problem14::solve(){ //Start the timer timer.start(); + //Loop through all numbers less than 1000000 and check them agains the series for(uint64_t currentNum = 1;currentNum <= MAX_NUM;++currentNum){ uint64_t currentLength = checkSeries(currentNum); @@ -77,6 +78,7 @@ void Problem14::solve(){ } } + //Stop the timer timer.stop(); @@ -92,27 +94,19 @@ void Problem14::reset(){ //Gets //Return a string with the solution to the problem -std::string Problem14::getResult(){ - if(!solved){ - throw Unsolved(); - } +std::string Problem14::getResult() const{ + solvedCheck("result"); std::stringstream result; result << "The number " << maxNum << " produced a chain of " << maxLength << " steps"; return result.str(); } //Returns the length of the requested chain uint64_t Problem14::getLength() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("length of the longest chain"); return maxLength; } //Returns the starting number of the requested chain uint64_t Problem14::getStartingNumber() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("starting number of the longest chain"); return maxNum; } diff --git a/src/Problems/Problem15.cpp b/src/Problems/Problem15.cpp index 3682e62..3e14a94 100644 --- a/src/Problems/Problem15.cpp +++ b/src/Problems/Problem15.cpp @@ -1,11 +1,11 @@ -//ProjectEuler/ProjectEulerCPP/Source/Problem15.hpp +//ProjectEuler/ProjectEulerCPP/src/Problems/Problem15.hpp //Matthew Ellison // 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? //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 it under the terms of the GNU Lesser General Public License as published by @@ -23,8 +23,8 @@ #include -#include #include +#include #include "Problems/Problem15.hpp" @@ -69,10 +69,12 @@ void Problem15::solve(){ //Start the timer timer.start(); + //We write this as a recursive function //When in a location it always moves right first, then down move(currentX, currentY); + //Stop the timer timer.stop(); @@ -87,19 +89,14 @@ void Problem15::reset(){ //Gets //Return a string with the solution to the problem -std::string Problem15::getResult(){ - if(!solved){ - throw Unsolved(); - } +std::string Problem15::getResult() const{ + solvedCheck("result"); std::stringstream result; result << "The number of routes is " << numOfRoutes; return result.str(); } //Returns the number of routes found uint64_t Problem15::getNumberOfRoutes() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("number of routes"); return numOfRoutes; } diff --git a/src/Problems/Problem16.cpp b/src/Problems/Problem16.cpp index aa971e2..993f737 100644 --- a/src/Problems/Problem16.cpp +++ b/src/Problems/Problem16.cpp @@ -1,14 +1,14 @@ -//ProjectEuler/ProjectEulerCPP/Source/Problem16.cpp +//ProjectEuler/ProjectEulerCPP/src/Problems/Problem16.cpp //Matthew Ellison // Created: 09-28-18 -//Modified: 08-28-20 +//Modified: 07-02-21 //What is the sum of the digits of the number 2^1000? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses //This file contains a header from the gmp library. The library is used for large integers. //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 /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -24,8 +24,9 @@ along with this program. If not, see . */ -#include + #include +#include #include #include "Problems/Problem16.hpp" @@ -47,12 +48,11 @@ void Problem16::solve(){ //Start the timer timer.start(); + //Get the number mpz_ui_pow_ui(num.get_mpz_t(), NUM_TO_POWER, POWER); - //Get a string of the number std::string numString = num.get_str(); - //Add up the individual characters of the string for(char number : numString){ std::string tempString; @@ -60,6 +60,7 @@ void Problem16::solve(){ sumOfElements += std::stoi(tempString); } + //Stop the timer timer.stop(); @@ -75,10 +76,8 @@ void Problem16::reset(){ //Gets //Return a string with the solution to the problem -std::string Problem16::getResult(){ - if(!solved){ - throw Unsolved(); - } +std::string Problem16::getResult() const{ + solvedCheck("result"); std::stringstream result; result << NUM_TO_POWER << '^' << POWER << " = " << num << "\nThe sum of the elements is " << sumOfElements; @@ -86,17 +85,11 @@ std::string Problem16::getResult(){ } //Returns the number that was calculated mpz_class Problem16::getNumber() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("number"); return num; } //Return the sum of the digits of the number int Problem16::getSum() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("sum"); return sumOfElements; } diff --git a/src/Problems/Problem17.cpp b/src/Problems/Problem17.cpp index a0782f1..8c8d58b 100644 --- a/src/Problems/Problem17.cpp +++ b/src/Problems/Problem17.cpp @@ -1,11 +1,11 @@ -//ProjectEuler/ProjectEulerCPP/Source/Problem17.cpp +//ProjectEuler/ProjectEulerCPP/src/Problems/Problem17.cpp //Matthew Ellison // 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? //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 it under the terms of the GNU Lesser General Public License as published by @@ -23,9 +23,8 @@ #include -#include #include -#include "Stopwatch.hpp" +#include #include "Problems/Problem17.hpp" @@ -164,7 +163,7 @@ uint64_t Problem17::countLetters(std::string str){ uint64_t letterCount = 0; //Step through every character in the string and count how many letters there are for(unsigned int cnt = 0;cnt < str.size();++cnt){ - if(isalpha(str.at(cnt))){ + if(isalpha(str[cnt])){ ++letterCount; } } @@ -185,12 +184,14 @@ void Problem17::solve(){ //Start the timer timer.start(); + //Step through every element in nums and get the word representations of the numbers for(int cnt = START_NUM;cnt <= STOP_NUM;++cnt){ 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 } + //Stop the timer timer.stop(); @@ -205,19 +206,14 @@ void Problem17::reset(){ //Gets //Return a string with the solution to the problem -std::string Problem17::getResult(){ - if(!solved){ - throw Unsolved(); - } +std::string Problem17::getResult() const{ + solvedCheck("result"); std::stringstream result; result << "The number of letters is " << letterCount; return result.str(); } //Returns the number of letters asked for uint64_t Problem17::getLetterCount() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("letter count"); return letterCount; } diff --git a/src/Problems/Problem18.cpp b/src/Problems/Problem18.cpp index 2757374..97b1142 100644 --- a/src/Problems/Problem18.cpp +++ b/src/Problems/Problem18.cpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Source/Problem18.hpp +//ProjectEuler/ProjectEulerCPP/src/Problems/Problem18.hpp //Matthew Ellison // Created: 11-01-18 -//Modified: 08-28-20 +//Modified: 07-02-21 //Find the maximum total from top to bottom /* 75 @@ -23,7 +23,7 @@ //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 /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -40,10 +40,10 @@ */ -#include #include -#include #include +#include +#include #include "Problems/Problem18.hpp" @@ -92,6 +92,7 @@ void Problem18::solve(){ //Start the timer timer.start(); + //The method that I am using looks for the smallest numbers, so I need to invert the numbers in this list invert(); //Now l[i][j] == 100 - l[i][j]; @@ -135,6 +136,7 @@ void Problem18::solve(){ 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 + //Stop the timer timer.stop(); @@ -151,22 +153,16 @@ void Problem18::reset(){ //Gets //Return a string with the solution to the problem -std::string Problem18::getResult(){ - if(!solved){ - throw Unsolved(); - } +std::string Problem18::getResult() const{ + solvedCheck("result"); std::stringstream result; result << "The value of the longest path is " << actualTotal; return result.str(); } //Returns the pyramid that was traversed as a string -std::string Problem18::getPyramid(){ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } +std::string Problem18::getPyramid() const{ + solvedCheck("pyramid of numbers"); std::stringstream results; - //Print the triangle list of numbers for(unsigned int rowCnt = 0;rowCnt < list.size();++rowCnt){ for(unsigned int colCnt = 0;colCnt < list[rowCnt].size();++colCnt){ @@ -176,11 +172,8 @@ std::string Problem18::getPyramid(){ return results.str(); } //Returns the trail the algorithm took as a string -std::string Problem18::getTrail(){ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } +std::string Problem18::getTrail() const{ + solvedCheck("trail of the shortest path"); std::stringstream results; //Print the trail the algorithm took @@ -190,13 +183,13 @@ std::string Problem18::getTrail(){ while(!top){ bool found = false; int loc = foundPoints.size() - 1; - std::list::iterator toAdd = foundPoints.begin(); + std::list::const_iterator toAdd = foundPoints.begin(); while(!found){ if(loc < 0){ results << "Error: Location < 0\n"; exit(1); } - std::list::iterator it = foundPoints.begin(); + std::list::const_iterator it = foundPoints.begin(); std::advance(it, loc); if(trail.front().fromRight){ 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 int Problem18::getTotal() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("total"); return actualTotal; } diff --git a/src/Problems/Problem19.cpp b/src/Problems/Problem19.cpp index da6a17b..28267a2 100644 --- a/src/Problems/Problem19.cpp +++ b/src/Problems/Problem19.cpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Source/Problem19.cpp +//ProjectEuler/ProjectEulerCPP/src/Problems/Problem19.cpp //Matthew Ellison // 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)? /* 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 /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -34,8 +34,8 @@ A leap year occurs on any year evenly divisible by 4, but not on a century unles #include -#include #include +#include #include "Problems/Problem19.hpp" @@ -158,6 +158,7 @@ void Problem19::solve(){ //Start the timer timer.start(); + //Run for all years up to 2000 for(unsigned int year = START_YEAR;year <= END_YEAR;++year){ //Run for all months in the year @@ -172,6 +173,7 @@ void Problem19::solve(){ } } + //Stop the timer timer.stop(); @@ -186,19 +188,14 @@ void Problem19::reset(){ //Gets //Return a string with the solution to the problem -std::string Problem19::getResult(){ - if(!solved){ - throw Unsolved(); - } +std::string Problem19::getResult() const{ + solvedCheck("result"); std::stringstream result; result << "There are " << totalSundays << " Sundays that landed on the first of the months from " << START_YEAR << " to " << END_YEAR; return result.str(); } //Returns the total sundays that were asked for uint64_t Problem19::getTotalSundays() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("total number of sundays"); return totalSundays; } diff --git a/src/Problems/Problem2.cpp b/src/Problems/Problem2.cpp index 3dc5290..1e74652 100644 --- a/src/Problems/Problem2.cpp +++ b/src/Problems/Problem2.cpp @@ -1,11 +1,11 @@ -//ProjectEuler/ProjectEulerCPP/Source/Problem2.cpp +//ProjectEuler/ProjectEulerCPP/src/Problems/Problem2.cpp //Matthew Ellison // Created: 09-28-18 -//Modified: 08-28-20 +//Modified: 07-02-21 //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 /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -23,9 +23,9 @@ #include -#include #include -#include "Algorithms.hpp" +#include +#include "mee/numberAlgorithms.hpp" #include "Problems/Problem2.hpp" @@ -42,9 +42,11 @@ void Problem2::solve(){ if(solved){ return; } + //Start the timer timer.start(); + //Get all the Fibonacci numbers <= TOP_NUM std::vector fibList = mee::getAllFib(TOP_NUM); //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 } + //Stop the timer timer.stop(); @@ -70,19 +73,14 @@ void Problem2::reset(){ //Gets //Return a string with the solution to the problem -std::string Problem2::getResult(){ - if(!solved){ - throw Unsolved(); - } +std::string Problem2::getResult() const{ + solvedCheck("result"); std::stringstream result; result << "The sum of the even Fibonacci numbers less than 4,000,000 is " << fullSum; return result.str(); } //Returns the requested sum uint64_t Problem2::getSum() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("sum"); return fullSum; } diff --git a/src/Problems/Problem20.cpp b/src/Problems/Problem20.cpp index d72b73d..1153dae 100644 --- a/src/Problems/Problem20.cpp +++ b/src/Problems/Problem20.cpp @@ -1,14 +1,14 @@ -//ProjectEuler/ProjectEulerCPP/Source/Problem20.cpp +//ProjectEuler/ProjectEulerCPP/src/Problems/Problem20.cpp //Matthew Ellison // Created: 11-07-18 -//Modified: 08-28-20 +//Modified: 07-02-21 //What is the sum of the digits of 100!? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses //This file contains a header from the gmp library. The library is used for large integers. //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 /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -26,8 +26,8 @@ #include -#include #include +#include #include "gmpxx.h" #include "Problems/Problem20.hpp" @@ -48,6 +48,7 @@ void Problem20::solve(){ //Start the timer timer.start(); + //Run through every number from 1 to 100 and multiply it by the current num for(int cnt = 1;cnt <= 100;++cnt){ num *= cnt; @@ -60,6 +61,7 @@ void Problem20::solve(){ sum += std::stoi(numString.substr(cnt, 1)); } + //Stop the timer timer.stop(); @@ -75,10 +77,8 @@ void Problem20::reset(){ //Gets //Return a string with the solution to the problem -std::string Problem20::getResult(){ - if(!solved){ - throw Unsolved(); - } +std::string Problem20::getResult() const{ + solvedCheck("result"); std::stringstream result; result << "100! = " << num.get_str() << "\nThe sum of the digits is: " << sum; @@ -86,25 +86,16 @@ std::string Problem20::getResult(){ } //Returns the number 100! mpz_class Problem20::getNumber() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("number"); return num; } //Returns the number 100! in a string std::string Problem20::getNumberString() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("number as a string"); return num.get_str(); } //Returns the sum of the digits of 100! uint64_t Problem20::getSum() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("sum of the digits"); return sum; } diff --git a/src/Problems/Problem21.cpp b/src/Problems/Problem21.cpp index 670e0ae..2e607fe 100644 --- a/src/Problems/Problem21.cpp +++ b/src/Problems/Problem21.cpp @@ -1,11 +1,11 @@ -//ProjectEuler/ProjectEulerCPP/Source/Problem21.cpp +//ProjectEuler/ProjectEulerCPP/src/Problems/Problem21.cpp //Matthew Ellison // Created: 11-08-18 -//Modified: 08-28-20 +//Modified: 07-02-21 //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 /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -26,7 +26,8 @@ #include #include #include -#include "Algorithms.hpp" +#include "mee/numberAlgorithms.hpp" +#include "mee/vectorAlgorithms.hpp" #include "Problems/Problem21.hpp" @@ -54,23 +55,24 @@ void Problem21::solve(){ //Start the timer timer.start(); + //Generate the divisors of all the numbers < 10000, get their sum, and add it to the list for(int cnt = 1;cnt < LIMIT;++cnt){ std::vector divisors = mee::getDivisors(cnt); //Get all the divisors of a number if(divisors.size() > 1){ 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 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(sum >= divisorSum.size()){ continue; } - //We know that divisorSum.at(cnt) == sum, so if divisorSum.at(sum) == cnt we found an amicable number - if(divisorSum.at(sum) == cnt){ + //We know that divisorSum[cnt] == sum, so if divisorSum[sum] == cnt we found an amicable number + if(divisorSum[sum] == cnt){ //A number can't be amicable with itself if(sum == cnt){ continue; @@ -83,6 +85,7 @@ void Problem21::solve(){ //Sort the vector for neatness std::sort(amicable.begin(), amicable.end()); + //Stop the timer timer.stop(); @@ -99,31 +102,23 @@ void Problem21::reset(){ //Gets //Return a string with the solution to the problem -std::string Problem21::getResult(){ - if(!solved){ - throw Unsolved(); - } +std::string Problem21::getResult() const{ + solvedCheck("result"); std::stringstream result; result << "All amicable numbers less than 10000 are\n"; for(unsigned int cnt = 0;cnt < amicable.size();++cnt){ - result << amicable.at(cnt) << '\n'; + result << amicable[cnt] << '\n'; } result << "The sum of all of these amicable numbers is " << mee::getSum(amicable); return result.str(); } //Returns a vector with all of the amicable numbers calculated std::vector Problem21::getAmicable() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("amicable numbers"); return amicable; } //Returns the sum of all of the amicable numbers uint64_t Problem21::getSum() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("sum of the amicable numbers"); return mee::getSum(amicable); } diff --git a/src/Problems/Problem22.cpp b/src/Problems/Problem22.cpp index 674a5d6..3672d53 100644 --- a/src/Problems/Problem22.cpp +++ b/src/Problems/Problem22.cpp @@ -1,11 +1,11 @@ -//ProjectEuler/ProjectEulerCPP/Source/Problem22.cpp +//ProjectEuler/ProjectEulerCPP/src/Problems/Problem22.cpp //Matthew Ellison // Created: 11-09-18 -//Modified: 08-28-20 +//Modified: 07-02-21 //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 /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -27,7 +27,7 @@ #include #include #include -#include "Algorithms.hpp" +#include "mee/vectorAlgorithms.hpp" #include "Problems/Problem22.hpp" @@ -426,6 +426,7 @@ void Problem22::solve(){ //Start the timer timer.start(); + //Sort all the names std::sort(names.begin(), names.end()); //Step through every name @@ -433,16 +434,17 @@ void Problem22::solve(){ //Step through every character in the current name 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 - sums.at(nameCnt) += (names[nameCnt][charCnt] - 64); + sums[nameCnt] += (names[nameCnt][charCnt] - 64); } } //Get the product for all numbers 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); + //Stop the timer timer.stop(); @@ -460,27 +462,19 @@ void Problem22::reset(){ //Gets //Return a string with the solution to the problem -std::string Problem22::getResult(){ - if(!solved){ - throw Unsolved(); - } +std::string Problem22::getResult() const{ + solvedCheck("result"); std::stringstream result; result << "The answer to the question is " << sum; return result.str(); } //Returns the vector of the names being scored std::vector Problem22::getNames() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("names"); return names; } //Returns the sum of the names' scores uint64_t Problem22::getNameScoreSum() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("score of the names"); return mee::getSum(prod); } diff --git a/src/Problems/Problem23.cpp b/src/Problems/Problem23.cpp index 538d4e6..f59ba91 100644 --- a/src/Problems/Problem23.cpp +++ b/src/Problems/Problem23.cpp @@ -1,11 +1,11 @@ -//ProjectEuler/ProjectEulerCPP/Source/Problem23.cpp +//ProjectEuler/ProjectEulerCPP/src/Problems/Problem23.cpp //Matthew Ellison // 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 //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 it under the terms of the GNU Lesser General Public License as published by @@ -27,7 +27,8 @@ #include #include #include -#include "Algorithms.hpp" +#include "mee/numberAlgorithms.hpp" +#include "mee/vectorAlgorithms.hpp" #include "Problems/Problem23.hpp" @@ -41,7 +42,7 @@ bool Problem23::isSum(const std::vector& abund, int num){ for(unsigned int firstNum = 0;firstNum < abund.size();++firstNum){ //Pick a number for the second part of the sum for(unsigned int secondNum = firstNum;secondNum < abund.size();++secondNum){ - sum = abund.at(firstNum) + abund.at(secondNum); + sum = abund[firstNum] + abund[secondNum]; if(sum == num){ return true; } @@ -75,19 +76,20 @@ void Problem23::solve(){ //Start the timer timer.start(); + //Get the sum of the divisors of all numbers < MAX_NUM for(int cnt = 1;cnt <= MAX_NUM;++cnt){ std::vector div = mee::getDivisors(cnt); if(div.size() > 1){ 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 std::vector abund; for(unsigned int cnt = 0;cnt < divisorSums.size();++cnt){ - if(divisorSums.at(cnt) > cnt){ + if(divisorSums[cnt] > cnt){ abund.push_back(cnt); } } @@ -99,6 +101,7 @@ void Problem23::solve(){ } } + //Stop the timer timer.stop(); @@ -115,19 +118,14 @@ void Problem23::reset(){ //Gets //Return a string with the solution to the problem -std::string Problem23::getResult(){ - if(!solved){ - throw Unsolved(); - } +std::string Problem23::getResult() const{ + solvedCheck("result"); std::stringstream result; result << "The answer is " << sum; return result.str(); } //Returns the sum of the numbers asked for uint64_t Problem23::getSum() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("sum"); return sum; } diff --git a/src/Problems/Problem24.cpp b/src/Problems/Problem24.cpp index 8c2c7bc..b75b9e5 100644 --- a/src/Problems/Problem24.cpp +++ b/src/Problems/Problem24.cpp @@ -1,11 +1,11 @@ -//ProjectEuler/ProjectEulerCPP/Source/Problem24.cpp +//ProjectEuler/ProjectEulerCPP/src/Problems/Problem24.cpp //Matthew Ellison // 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? //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 it under the terms of the GNU Lesser General Public License as published by @@ -25,7 +25,7 @@ #include #include #include -#include "Algorithms.hpp" +#include "mee/stringAlgorithms.hpp" #include "Problems/Problem24.hpp" @@ -48,9 +48,11 @@ void Problem24::solve(){ //Start the timer timer.start(); + //Get all permutations of the string permutations = mee::getPermutations(nums); + //Stop the timer timer.stop(); @@ -65,27 +67,19 @@ void Problem24::reset(){ //Gets //Return a string with the solution to the problem -std::string Problem24::getResult(){ - if(!solved){ - throw Unsolved(); - } +std::string Problem24::getResult() const{ + solvedCheck("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(); } //Returns a vector with all of the permutations std::vector Problem24::getPermutationsList() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("permutations"); return permutations; } //Returns the specific permutations you are looking for std::string Problem24::getPermutation() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - return permutations.at(NEEDED_PERM - 1); + solvedCheck("1,000,000th permutation"); + return permutations[NEEDED_PERM - 1]; } diff --git a/src/Problems/Problem25.cpp b/src/Problems/Problem25.cpp index d5dc3e1..592aac0 100644 --- a/src/Problems/Problem25.cpp +++ b/src/Problems/Problem25.cpp @@ -1,14 +1,14 @@ -//ProjectEuler/ProjectEulerCPP/Source/Problem25.cpp +//ProjectEuler/ProjectEulerCPP/src/Problems/Problem25.cpp //Matthew Ellison // 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? //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. //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 /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -25,10 +25,10 @@ */ -#include #include -#include "gmpxx.h" -#include "Algorithms.hpp" +#include +#include +#include "mee/numberAlgorithms.hpp" #include "Problems/Problem25.hpp" @@ -51,12 +51,14 @@ void Problem25::solve(){ //Start the timer timer.start(); + //Move through all Fibonacci numbers until you reach the one with at least NUM_DIGITS 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 number = mee::getFib(index); //Calculate the number } + //Stop the timer timer.stop(); @@ -72,10 +74,8 @@ void Problem25::reset(){ //Gets //Return a string with the solution to the problem -std::string Problem25::getResult(){ - if(!solved){ - throw Unsolved(); - } +std::string Problem25::getResult() const{ + solvedCheck("result"); std::stringstream result; result << "The first Fibonacci number with " << NUM_DIGITS << " digits is " << number << "\nIts index is " << index; @@ -83,41 +83,26 @@ std::string Problem25::getResult(){ } //Returns the Fibonacci number asked for mpz_class Problem25::getNumber() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("fibonacci number"); return number; } //Returns the Fibonacci number asked for as a string std::string Problem25::getNumberString() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("fibonacci number as a string"); return number.get_str(); } //Returns the index of the requested Fibonacci number mpz_class Problem25::getIndex() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("index of the fibonacci number"); return index; } //Returns the index of the requested Fibonacci number as a string std::string Problem25::getIndexString() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("index of the fibonacci number as a string"); return index.get_str(); } //Returns the index of the requested Fibonacci number as a uint64_t uint64_t Problem25::getIndexInt() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("index of the fibonacci number as an int"); return index.get_ui(); } diff --git a/src/Problems/Problem26.cpp b/src/Problems/Problem26.cpp index 66a9136..efed4e9 100644 --- a/src/Problems/Problem26.cpp +++ b/src/Problems/Problem26.cpp @@ -1,11 +1,11 @@ -//ProjectEuler/ProjectEulerCPP/Source/Problem26.cpp +//ProjectEuler/ProjectEulerCPP/src/Problems/Problem26.cpp //Matthew Ellison // 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. //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 it under the terms of the GNU Lesser General Public License as published by @@ -25,7 +25,7 @@ #include #include #include -#include "Algorithms.hpp" +#include "mee/vectorAlgorithms.hpp" #include "Problems/Problem26.hpp" @@ -48,6 +48,7 @@ void Problem26::solve(){ //Start the timer timer.start(); + //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 for(unsigned int denominator = 2;denominator <= TOP_NUMBER;++denominator){ @@ -86,6 +87,7 @@ void Problem26::solve(){ } } + //Stop the timer timer.stop(); @@ -101,10 +103,8 @@ void Problem26::reset(){ //Gets //Return a string with the solution to the problem -std::string Problem26::getResult(){ - if(!solved){ - throw Unsolved(); - } +std::string Problem26::getResult() const{ + solvedCheck("result"); std::stringstream result; result << "The longest cycle is " << longestCycle << " digits long" << "\nIt is started with the number " << longestNumber; @@ -112,17 +112,11 @@ std::string Problem26::getResult(){ } //Returns the length of the longest cycle unsigned int Problem26::getLongestCycle() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("length of the longest cycle"); return longestCycle; } //Returns the denominator that starts the longest cycle unsigned int Problem26::getLongestNumber() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("denominator that starts the longest cycle"); return longestNumber; } diff --git a/src/Problems/Problem27.cpp b/src/Problems/Problem27.cpp index a5b58df..a09fc58 100644 --- a/src/Problems/Problem27.cpp +++ b/src/Problems/Problem27.cpp @@ -1,11 +1,11 @@ -//ProjectEuler/ProjectEulerCPP/Source/Problem27.cpp +//ProjectEuler/ProjectEulerCPP/src/Problems/Problem27.cpp //Matthew Ellison // 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. //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 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 . */ + #include #include #include -#include "Algorithms.hpp" +#include "mee/numberAlgorithms.hpp" #include "Problems/Problem27.hpp" @@ -43,6 +44,7 @@ void Problem27::solve(){ //Start the timer timer.start(); + //Start with the lowest possible A and check all possibilities after that for(int64_t a = -999;a <= 999;++a){ //Start with the lowest possible B and check all possibilities after that @@ -65,6 +67,7 @@ void Problem27::solve(){ } } + //Stop the timer timer.stop(); @@ -80,10 +83,8 @@ void Problem27::reset(){ //Gets //Return a string with the solution to the problem -std::string Problem27::getResult(){ - if(!solved){ - throw Unsolved(); - } +std::string Problem27::getResult() const{ + solvedCheck("result"); std::stringstream result; result << "The greatest number of primes found is " << topN << "\nIt was found with A = " << topA << ", B = " << topB @@ -92,25 +93,16 @@ std::string Problem27::getResult(){ } //Returns the top A that was generated int64_t Problem27::getTopA() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("largest A"); return topA; } //Returns the top B that was generated int64_t Problem27::getTopB() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("largest B"); return topB; } //Returns the top N that was generated int64_t Problem27::getTopN() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("largest N"); return topN; } diff --git a/src/Problems/Problem28.cpp b/src/Problems/Problem28.cpp index 10fc98a..36d96c5 100644 --- a/src/Problems/Problem28.cpp +++ b/src/Problems/Problem28.cpp @@ -1,11 +1,11 @@ -//ProjectEuler/ProjectEulerCPP/Source/Problem28.cpp +//ProjectEuler/ProjectEulerCPP/src/Problems/Problem28.cpp //Matthew Ellison // 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 //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 it under the terms of the GNU Lesser General Public License as published by @@ -22,10 +22,10 @@ */ -#include #include -#include #include +#include +#include #include "Problems/Problem28.hpp" @@ -35,7 +35,7 @@ void Problem28::setupGrid(){ for(int cnt = 0;cnt < 1001;++cnt){ grid.emplace_back(); 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 int xLocation = 500; int yLocation = 500; - grid.at(yLocation).at(xLocation) = currentNum++; + grid[yLocation][xLocation] = currentNum++; //Move right the first time ++xLocation; //Move in a circular pattern until you reach the final location while(!finalLocation){ //Move down until you reach a blank location on the left - while(grid.at(yLocation).at(xLocation - 1) != 0){ - grid.at(yLocation).at(xLocation) = currentNum++; + while(grid[yLocation][xLocation - 1] != 0){ + grid[yLocation][xLocation] = currentNum++; ++yLocation; } //Move left until you reach a blank location above - while(grid.at(yLocation - 1).at(xLocation) != 0){ - grid.at(yLocation).at(xLocation) = currentNum++; + while(grid[yLocation - 1][xLocation] != 0){ + grid[yLocation][xLocation] = currentNum++; --xLocation; } //Move up until you reach a blank location to the right - while(grid.at(yLocation).at(xLocation + 1) != 0){ - grid.at(yLocation).at(xLocation) = currentNum++; + while(grid[yLocation][xLocation + 1] != 0){ + grid[yLocation][xLocation] = currentNum++; --yLocation; } //Move right until you reach a blank location below - while(grid.at(yLocation + 1).at(xLocation) != 0){ - grid.at(yLocation).at(xLocation) = currentNum++; + while(grid[yLocation + 1][xLocation] != 0){ + grid[yLocation][xLocation] = currentNum++; ++xLocation; //Check if you are at the final location and break the loop if you are if(xLocation == (int)grid.size()){ @@ -92,11 +92,11 @@ void Problem28::findSum(){ while(row < grid.size()){ //This ensures the middle location is only counted once if(leftSide == rightSide){ - sumOfDiagonals += grid.at(row).at(leftSide); + sumOfDiagonals += grid[row][leftSide]; } else{ - sumOfDiagonals += grid.at(row).at(leftSide); - sumOfDiagonals += grid.at(row).at(rightSide); + sumOfDiagonals += grid[row][leftSide]; + sumOfDiagonals += grid[row][rightSide]; } ++row; ++leftSide; @@ -143,27 +143,19 @@ void Problem28::reset(){ //Gets //Return a string with the solution to the problem -std::string Problem28::getResult(){ - if(!solved){ - throw Unsolved(); - } +std::string Problem28::getResult() const{ + solvedCheck("result"); std::stringstream result; result << "The sum of the diagonals in the given grid is " << sumOfDiagonals; return result.str(); } //Returns the grid std::vector> Problem28::getGrid() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("grid"); return grid; } //Returns the sum of the diagonals uint64_t Problem28::getSum() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("sum"); return sumOfDiagonals; } diff --git a/src/Problems/Problem29.cpp b/src/Problems/Problem29.cpp index acb04f3..c0a815f 100644 --- a/src/Problems/Problem29.cpp +++ b/src/Problems/Problem29.cpp @@ -1,14 +1,14 @@ -//ProjectEuler/ProjectEulerCPP/Source/Problem29.cpp +//ProjectEuler/ProjectEulerCPP/src/Problems/Problem29.cpp //Matthew Ellison // 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? //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. //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 /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -24,14 +24,15 @@ along with this program. If not, see . */ -#include -#include + #include -#include #include +#include +#include +#include #include +#include "mee/vectorAlgorithms.hpp" #include "Problems/Problem29.hpp" -#include "Algorithms.hpp" //The lowest possible value for a @@ -57,6 +58,7 @@ void Problem29::solve(){ //Start the timer timer.start(); + //Start with the first A and move towards the top for(unsigned long currentA = BOTTOM_A;currentA <= TOP_A;++currentA){ //Start with the first B and move towards the top @@ -70,6 +72,7 @@ void Problem29::solve(){ } } + //Stop the timer timer.stop(); @@ -84,51 +87,39 @@ void Problem29::reset(){ //Gets //Return a string with the solution to the problem -std::string Problem29::getResult(){ - if(!solved){ - throw Unsolved(); - } +std::string Problem29::getResult() const{ + solvedCheck("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(); return result.str(); } //Returns the lowest possible value for a unsigned int Problem29::getBottomA() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("lowest a"); return BOTTOM_A; } //Returns the highest possible value for a unsigned int Problem29::getTopA() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("highest a"); return TOP_A; } //Returns the lowest possible value for b unsigned int Problem29::getBottomB() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("lowest b"); return BOTTOM_B; } //Returns the highest possible value for b unsigned int Problem29::getTopB() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("highest b"); return TOP_B; } //Returns a vector of all the unique values for a^b std::vector Problem29::getUnique() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("the unique values for a^b"); 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(); +} diff --git a/src/Problems/Problem3.cpp b/src/Problems/Problem3.cpp index b833432..de0a6d6 100644 --- a/src/Problems/Problem3.cpp +++ b/src/Problems/Problem3.cpp @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCPP/Source/Problem3.cpp //Matthew Ellison // Created: 09-28-18 -//Modified: 08-28-20 +//Modified: 07-02-21 //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 /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -22,11 +22,11 @@ */ -#include #include -#include #include -#include "Algorithms.hpp" +#include +#include +#include "mee/numberAlgorithms.hpp" #include "Problems/Problem3.hpp" @@ -43,12 +43,15 @@ void Problem3::solve(){ if(solved){ return; } + //Start the timer timer.start(); + //Find the factors of GOAL_NUMBER factors = mee::getFactors(GOAL_NUMBER); + //Stop the timer timer.stop(); @@ -63,36 +66,19 @@ void Problem3::reset(){ //Gets //Return a string with the solution to the problem -std::string Problem3::getResult(){ - if(!solved){ - throw Unsolved(); - } +std::string Problem3::getResult() const{ + solvedCheck("result"); std::stringstream result; result << "The largest factor of the number " << GOAL_NUMBER << " is " << factors[factors.size() - 1]; return result.str(); } //Returns the list of factors of the number std::vector Problem3::getFactors() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("factors"); return factors; } //Returns the largest factor of the number uint64_t Problem3::getLargestFactor() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - + solvedCheck("largest factor"); 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; -} diff --git a/src/Problems/Problem30.cpp b/src/Problems/Problem30.cpp index 359abcf..200ec70 100644 --- a/src/Problems/Problem30.cpp +++ b/src/Problems/Problem30.cpp @@ -1,11 +1,11 @@ -//ProjectEuler/ProjectEulerCPP/Source/Problem30.cpp +//ProjectEuler/ProjectEulerCPP/src/Problems/Problem30.cpp //Matthew Ellison // 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. //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 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 . */ - -#include -#include #include -#include #include +#include +#include +#include +#include "mee/vectorAlgorithms.hpp" #include "Problems/Problem30.hpp" @@ -80,7 +80,7 @@ void Problem30::solve(){ } } - sum = getSumOfList(); + sum = mee::getSum(sumOfFifthNumbers); //Stop the timer timer.stop(); @@ -96,43 +96,24 @@ void Problem30::reset(){ //Gets //Return a string with the solution to the problem -std::string Problem30::getResult(){ - if(!solved){ - throw Unsolved(); - } +std::string Problem30::getResult() const{ + solvedCheck("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; return result.str(); } //This returns the top number to be checked uint64_t Problem30::getTopNum() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - + solvedCheck("largest number checked"); 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 std::vector Problem30::getListOfSumOfFifths() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - + solvedCheck("list of all numbers that are the sum of the 5th power of their digits"); return sumOfFifthNumbers; } //This returns the sum of all entries in sumOfFifthNumbers uint64_t Problem30::getSumOfList() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - - uint64_t sum = 0; - for(uint64_t num : sumOfFifthNumbers){ - sum += num; - } - + solvedCheck("sum of all numbers that are the sum of the 5th power of their digits"); return sum; } diff --git a/src/Problems/Problem31.cpp b/src/Problems/Problem31.cpp index 4ec887f..96459a1 100644 --- a/src/Problems/Problem31.cpp +++ b/src/Problems/Problem31.cpp @@ -1,11 +1,11 @@ -//ProjectEuler/ProjectEulerCPP/Source/Problem31.cpp +//ProjectEuler/ProjectEulerCPP/src/Problems/Problem31.cpp //Matthew Ellison // 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? //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 it under the terms of the GNU Lesser General Public License as published by @@ -22,8 +22,8 @@ */ -#include #include +#include #include #include "Problems/Problem31.hpp" @@ -46,6 +46,7 @@ void Problem31::solve(){ //Start the timer timer.start(); + //Start with 200p and remove the necessary coins with each loop for(int pound2 = desiredValue;pound2 >= 0;pound2 -= 200){ for(int pound1 = pound2;pound1 >= 0;pound1 -= 100){ @@ -78,19 +79,14 @@ void Problem31::reset(){ //Gets //Return a string with the solution to the problem -std::string Problem31::getResult(){ - if(!solved){ - throw Unsolved(); - } +std::string Problem31::getResult() const{ + solvedCheck("result"); std::stringstream result; result << "There are " << permutations << " ways to make 2 pounds with the given denominations of coins"; return result.str(); } //Returns the number of correct permutations of the coins int Problem31::getPermutations() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("number of correct permutations of the coins"); return permutations; } diff --git a/src/Problems/Problem32.cpp b/src/Problems/Problem32.cpp index f14354f..24e7ee1 100644 --- a/src/Problems/Problem32.cpp +++ b/src/Problems/Problem32.cpp @@ -1,11 +1,11 @@ -//ProjectEuler/ProjectEulerCPP/Source/Problem32.cpp +//ProjectEuler/ProjectEulerCPP/src/Problems/Problem32.cpp //Matthew Ellison // 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. //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 it under the terms of the GNU Lesser General Public License as published by @@ -26,14 +26,13 @@ #include #include #include -#include "Algorithms.hpp" +#include "mee/stringAlgorithms.hpp" #include "Problems/Problem32.hpp" -#include + int Problem32::TOP_MULTIPLICAND = 99; //The largest multiplicand to check int Problem32::TOP_MULTIPLIER = 4999; //The largest multiplier to check - //Returns true if the passed productset is 1-9 pandigital bool Problem32::isPandigital(ProductSet currentSet){ //Get the numbers out of the object and put them into a string @@ -68,6 +67,7 @@ void Problem32::solve(){ //Start the timer timer.start(); + //Create the multiplicand and start working your way up for(int multiplicand = 1;multiplicand <= TOP_MULTIPLICAND;++multiplicand){ //Run through all possible multipliers @@ -91,6 +91,7 @@ void Problem32::solve(){ sumOfPandigitals += prod.getProduct(); } + //Stop the timer timer.stop(); @@ -106,19 +107,14 @@ void Problem32::reset(){ //Gets //Return a string with the solution to the problem -std::string Problem32::getResult(){ - if(!solved){ - throw Unsolved(); - } +std::string Problem32::getResult() const{ + solvedCheck("result"); std::stringstream result; result << "There are " << listOfProducts.size() << " unique 1-9 pandigitals\nThe sum of the products of these pandigitals is " << sumOfPandigitals; return result.str(); } //Returns the sum of the pandigitals -int64_t Problem32::getSumOfPandigitals(){ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } +int64_t Problem32::getSumOfPandigitals() const{ + solvedCheck("sum of the pandigitals"); return sumOfPandigitals; } \ No newline at end of file diff --git a/src/Problems/Problem33.cpp b/src/Problems/Problem33.cpp index cf513b8..efe387d 100644 --- a/src/Problems/Problem33.cpp +++ b/src/Problems/Problem33.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/src/Problems/Problem33.cpp //Matthew Ellison -// Created: 02-05-2021 -//Modified: 02-06-2021 +// Created: 02-05-21 +//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 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 #include -#include #include +#include #include +#include "mee/vectorAlgorithms.hpp" #include "Problems/Problem33.hpp" -#include "Algorithms.hpp" //The lowest the numerator can be @@ -60,6 +60,7 @@ void Problem33::solve(){ //Start the timer timer.start(); + //Search every possible numerator/denominator pair for(int denominator = MIN_DENOMINATOR;denominator <= MAX_DENOMINATOR;++denominator){ for(int numerator = MIN_NUMERATOR;(numerator < denominator) && (numerator <= MAX_NUMERATOR);++numerator){ @@ -106,6 +107,7 @@ void Problem33::solve(){ //Save the denominator prodDenominator = denomProd / gcd; + //Stop the timer timer.stop(); @@ -121,36 +123,24 @@ void Problem33::reset(){ //Gets //Return a string with the solution to the problem -std::string Problem33::getResult(){ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } +std::string Problem33::getResult() const{ + solvedCheck("result"); std::stringstream result; result << "The denominator of the product is " << prodDenominator; return result.str(); } //Returns the list of numerators -std::vector Problem33::getNumerators(){ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } +std::vector Problem33::getNumerators() const{ + solvedCheck("list of numerators"); return numerators; } //Returns the list of denominators -std::vector Problem33::getDenominators(){ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } +std::vector Problem33::getDenominators() const{ + solvedCheck("list of denominators"); return denominators; } //Returns the answer to the question -int Problem33::getProdDenominator(){ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } +int Problem33::getProdDenominator() const{ + solvedCheck("denominator"); return prodDenominator; } diff --git a/src/Problems/Problem34.cpp b/src/Problems/Problem34.cpp index 8db4dbe..2ce25d4 100644 --- a/src/Problems/Problem34.cpp +++ b/src/Problems/Problem34.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/src/Problems/Problem34.cpp //Matthew Ellison // 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 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses /* @@ -25,8 +25,8 @@ #include #include #include +#include "mee/numberAlgorithms.hpp" #include "Problems/Problem34.hpp" -#include "Algorithms.hpp" //The largest num that can be the sum of its own digits @@ -50,6 +50,7 @@ void Problem34::solve(){ //Start the timer timer.start(); + //Pre-compute the possible factorials from 0! to 9! for(int cnt = 0;cnt <= 9;++cnt){ factorials[cnt] = mee::factorial(cnt); @@ -69,6 +70,7 @@ void Problem34::solve(){ } } + //Stop the timer timer.stop(); @@ -86,28 +88,19 @@ void Problem34::reset(){ //Gets //Return a string with the solution to the problem -std::string Problem34::getResult(){ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } +std::string Problem34::getResult() const{ + solvedCheck("result"); std::stringstream result; result << "The sum of all numbers that are the sum of their digit's factorials is " << sum; return result.str(); } //Returns the list of factorials from 0-9 -std::vector Problem34::getFactorials(){ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } +std::vector Problem34::getFactorials() const{ + solvedCheck("list of factorials"); return factorials; } //Returns the sum of all numbers equal to the sum of their digit's factorials -int Problem34::getSum(){ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } +int Problem34::getSum() const{ + solvedCheck("sum"); return sum; } diff --git a/src/Problems/Problem35.cpp b/src/Problems/Problem35.cpp index c39e8fd..7aa8732 100644 --- a/src/Problems/Problem35.cpp +++ b/src/Problems/Problem35.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/src/Problems/Problem35.cpp //Matthew Ellison // Created: 06-02-21 -//Modified: 06-02-21 +//Modified: 07-02-21 //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 /* @@ -24,11 +24,12 @@ //TODO: This could also be improved by skipping any prime that includes 2, 4, 5, 6, 8, or 0 -#include #include +#include #include +#include "mee/numberAlgorithms.hpp" +#include "mee/vectorAlgorithms.hpp" #include "Problems/Problem35.hpp" -#include "Algorithms.hpp" //The largest number that we are checking for primes @@ -61,6 +62,7 @@ void Problem35::solve(){ //Start the timer timer.start(); + //Get all primes under 1,000,000 primes = mee::getPrimes(MAX_NUM); //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 timer.stop(); @@ -95,36 +98,24 @@ void Problem35::reset(){ } //Gets //Returns a string with the solution to the problem -std::string Problem35::getResult(){ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } +std::string Problem35::getResult() const{ + solvedCheck("result"); std::stringstream result; result << "The number of all circular prime numbers under " << MAX_NUM << " is " << circularPrimes.size(); return result.str(); } //Returns the vector of primes < MAX_NUM -std::vector Problem35::getPrimes(){ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } +std::vector Problem35::getPrimes() const{ + solvedCheck("list of primes"); return primes; } //Returns the vector of circular primes < MAX_NUM -std::vector Problem35::getCircularPrimes(){ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } +std::vector Problem35::getCircularPrimes() const{ + solvedCheck("list of circular primes"); return circularPrimes; } //Returns the number of circular primes -int Problem35::getNumCircularPrimes(){ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } +int Problem35::getNumCircularPrimes() const{ + solvedCheck("number of circular primes"); return circularPrimes.size(); } diff --git a/src/Problems/Problem36.cpp b/src/Problems/Problem36.cpp index 11d6df0..a1f0e41 100644 --- a/src/Problems/Problem36.cpp +++ b/src/Problems/Problem36.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/src/Problems/Problem36.cpp //Matthew Ellison // 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. //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses /* @@ -22,11 +22,13 @@ */ -#include #include +#include #include +#include "mee/numberAlgorithms.hpp" +#include "mee/stringAlgorithms.hpp" +#include "mee/vectorAlgorithms.hpp" #include "Problems/Problem36.hpp" -#include "Algorithms.hpp" //The largest number that will be checked @@ -47,6 +49,7 @@ void Problem36::solve(){ //Start the timer timer.start(); + //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){ //Check if num is a palindrome @@ -62,6 +65,7 @@ void Problem36::solve(){ //Get the sum of all palindromes in the vector sum = mee::getSum(palindromes); + //Stop the timer timer.stop(); @@ -77,28 +81,19 @@ void Problem36::reset(){ //Gets //Returns a string with the solution to the problem -std::string Problem36::getResult(){ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } +std::string Problem36::getResult() const{ + solvedCheck("result"); std::stringstream result; result << "The sum of all base 10 and base 2 palindromic numbers < " << MAX_NUM << " is " << sum; return result.str(); } //Return the vector of palindromes < MAX_NUM -std::vector Problem36::getPalindromes(){ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } +std::vector Problem36::getPalindromes() const{ + solvedCheck("list of palindromes"); return palindromes; } //Return the sum of all elements in the vector of palindromes -int Problem36::getSumOfPalindromes(){ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } +int Problem36::getSumOfPalindromes() const{ + solvedCheck("sum of all palindromes"); return sum; } \ No newline at end of file diff --git a/src/Problems/Problem37.cpp b/src/Problems/Problem37.cpp index 52f8dfc..634e914 100644 --- a/src/Problems/Problem37.cpp +++ b/src/Problems/Problem37.cpp @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCPP/src/Problems/Problem37.cpp //Matthew Ellison // 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). //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses /* @@ -23,11 +23,13 @@ #include -#include #include +#include #include +#include "mee/Generator.hpp" +#include "mee/numberAlgorithms.hpp" +#include "mee/vectorAlgorithms.hpp" #include "Problems/Problem37.hpp" -#include "Algorithms.hpp" //The last prime before 11 since single digit primes aren't checked @@ -48,8 +50,9 @@ void Problem37::solve(){ //Start the timer timer.start(); + //Create the sieve and get the first prime number - mee::SieveOfEratosthenes sieve; + mee::Generator sieve = mee::sieveOfEratosthenes(); uint64_t currentPrime = sieve.next(); //Loop through the sieve until you get to 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 sum = mee::getSum(truncPrimes); + //Stop the timer timer.stop(); @@ -125,28 +129,19 @@ void Problem37::reset(){ //Gets //Returns a string with the solution to the problem -std::string Problem37::getResult(){ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } +std::string Problem37::getResult() const{ + solvedCheck("result"); std::stringstream result; result << "The sum of all left and right truncatable primes is " << sum; return result.str(); } //Returns the list of primes that can be truncated -std::vector Problem37::getTruncatablePrimes(){ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } +std::vector Problem37::getTruncatablePrimes() const{ + solvedCheck("list of truncatable primes"); return truncPrimes; } //Get the sum of all primes in truncPrimes -uint64_t Problem37::getSumOfPrimes(){ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } +uint64_t Problem37::getSumOfPrimes() const{ + solvedCheck("sum of truncatable primes"); return sum; } diff --git a/src/Problems/Problem4.cpp b/src/Problems/Problem4.cpp index b20ac4e..52d6c2f 100644 --- a/src/Problems/Problem4.cpp +++ b/src/Problems/Problem4.cpp @@ -1,11 +1,11 @@ -//ProjectEuler/ProjectEulerCPP/Source/Problem4.cpp +//ProjectEuler/ProjectEulerCPP/src/Problems/Problem4.cpp //Matthew Ellison // 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 //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 it under the terms of the GNU Lesser General Public License as published by @@ -22,10 +22,10 @@ */ -#include -#include -#include #include +#include +#include +#include #include "Problems/Problem4.hpp" @@ -44,9 +44,11 @@ void Problem4::solve(){ if(solved){ return; } + //Start the timer timer.start(); + //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){ //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 } } - //Sort the palindromes so that the last one is the largest std::sort(palindromes.begin(), palindromes.end()); - + + //Stop the timer timer.stop(); @@ -84,27 +86,19 @@ void Problem4::reset(){ //Gets //Return a string with the solution to the problem -std::string Problem4::getResult(){ - if(!solved){ - throw Unsolved(); - } +std::string Problem4::getResult() const{ + solvedCheck("result"); std::stringstream result; result << "The largest palindrome is " << *(palindromes.end() - 1); return result.str(); } //Returns the list of all palindromes std::vector Problem4::getPalindromes() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("palindromes"); return palindromes; } //Returns the largest palindrome uint64_t Problem4::getLargestPalindrome() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("largest palindrome"); return *(palindromes.end() - 1); } diff --git a/src/Problems/Problem5.cpp b/src/Problems/Problem5.cpp index 7885ff4..e6e1e1e 100644 --- a/src/Problems/Problem5.cpp +++ b/src/Problems/Problem5.cpp @@ -1,11 +1,11 @@ -//ProjectEuler/ProjectEulerCPP/Source/Problem5.cpp +//ProjectEuler/ProjectEulerCPP/src/Problems/Problem5.cpp //Matthew Ellison // 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? //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 it under the terms of the GNU Lesser General Public License as published by @@ -22,10 +22,12 @@ */ -#include #include -#include #include +#include +#include +#include "mee/numberAlgorithms.hpp" +#include "mee/vectorAlgorithms.hpp" #include "Problems/Problem5.hpp" @@ -39,15 +41,16 @@ void Problem5::solve(){ if(solved){ return; } - //Setup your variables - bool numFound = false; //Used to determine if the number has been found //Start the timer 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 = 20; + + int currentNum = 0; //The current number being checked + //Get all primes < 20 and multiply them together. That will be the starting point as the smallest possible answer + std::vector primes = mee::getPrimes(20); + currentNum = mee::getProduct(primes); + bool numFound = false; //Used to determine if the number has been found while((currentNum > 0) && (!numFound)){ //Start by assuming you found the number (because we throw a flag if we didn't find it) numFound = true; @@ -59,14 +62,15 @@ void Problem5::solve(){ 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){ currentNum += 2; } } - + //Save the current number as the smallest smallestNum = currentNum; + //Stop the timer timer.stop(); @@ -81,19 +85,14 @@ void Problem5::reset(){ //Gets //Return a string with the solution to the problem -std::string Problem5::getResult(){ - if(!solved){ - throw Unsolved(); - } +std::string Problem5::getResult() const{ + solvedCheck("result"); std::stringstream result; result << "The smallest positive number evenly divisible by all numbers 1-20 is " << smallestNum; return result.str(); } //Returns the requested number int Problem5::getNumber() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("number"); return smallestNum; } diff --git a/src/Problems/Problem6.cpp b/src/Problems/Problem6.cpp index 469b2a3..88f848d 100644 --- a/src/Problems/Problem6.cpp +++ b/src/Problems/Problem6.cpp @@ -1,11 +1,11 @@ -//ProjectEuler/ProjectEulerCPP/Source/Problem6.cpp +//ProjectEuler/ProjectEulerCPP/src/Problems/Problem6.cpp //Matthew Ellison // 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. //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 it under the terms of the GNU Lesser General Public License as published by @@ -24,8 +24,8 @@ #include #include -#include #include +#include #include "Problems/Problem6.hpp" @@ -42,9 +42,11 @@ void Problem6::solve(){ if(solved){ return; } + //Start the timer timer.start(); + //Run through all numbers and add them to the appropriate sums for(int currentNum = START_NUM;currentNum <= END_NUM;++currentNum){ sumOfSquares += (currentNum * currentNum); //Add the square to the correct variable @@ -53,6 +55,7 @@ void Problem6::solve(){ //Square the sum that needs it squareOfSum *= squareOfSum; + //Stop the timer timer.stop(); @@ -67,35 +70,24 @@ void Problem6::reset(){ //Gets //Return a string with the solution to the problem -std::string Problem6::getResult(){ - if(!solved){ - throw Unsolved(); - } +std::string Problem6::getResult() const{ + solvedCheck("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); return result.str(); } //Returns the sum of all the squares uint64_t Problem6::getSumOfSquares() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("sum of the squares"); return sumOfSquares; } //Returns the square of all of the sums uint64_t Problem6::getSquareOfSum() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("square of the sums"); return squareOfSum; } //Returns the requested difference uint64_t Problem6::getDifference() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } - return abs(sumOfSquares - squareOfSum); + solvedCheck("difference between the two numbers"); + return std::abs((int64_t)(sumOfSquares - squareOfSum)); } diff --git a/src/Problems/Problem67.cpp b/src/Problems/Problem67.cpp index cdddb7c..676eb06 100644 --- a/src/Problems/Problem67.cpp +++ b/src/Problems/Problem67.cpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Source/Problem67.cpp +//ProjectEuler/ProjectEulerCPP/src/Problems/Problem67.cpp //Matthew Ellison // Created: 11-02-18 -//Modified: 08-28-20 +//Modified: 07-02-21 //The way to do this is using a breadth first search /* 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 /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -130,7 +130,6 @@ Find the maximum total from top to bottom //This is the list you are trying to find a path through - void Problem67::setupList(){ list.push_back({59}); 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({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(); +} diff --git a/src/Problems/Problem7.cpp b/src/Problems/Problem7.cpp index 0d104d9..f908090 100644 --- a/src/Problems/Problem7.cpp +++ b/src/Problems/Problem7.cpp @@ -1,11 +1,11 @@ -//ProjectEuler/ProjectEulerCPP/Source/Problem7.cpp +//ProjectEuler/ProjectEulerCPP/src/Problems/Problem7.cpp //Matthew Ellison // Created: 09-28-18 -//Modified: 08-28-20 +//Modified: 07-02-21 //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 /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -22,11 +22,10 @@ */ -#include #include -#include #include -#include "Algorithms.hpp" +#include +#include "mee/numberAlgorithms.hpp" #include "Problems/Problem7.hpp" @@ -43,12 +42,15 @@ void Problem7::solve(){ if(solved){ return; } + //Start the timer timer.start(); + //Get the correct number of prime numbers primes = mee::getNumPrimes(NUMBER_OF_PRIMES); + //Stop the timer timer.stop(); @@ -62,19 +64,14 @@ void Problem7::reset(){ //Gets //Return a string with the solution to the problem -std::string Problem7::getResult(){ - if(!solved){ - throw Unsolved(); - } +std::string Problem7::getResult() const{ + solvedCheck("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(); } //Returns the requested prime number uint64_t Problem7::getPrime() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("prime"); return *primes.end(); } diff --git a/src/Problems/Problem8.cpp b/src/Problems/Problem8.cpp index 8ac7a93..85bf049 100644 --- a/src/Problems/Problem8.cpp +++ b/src/Problems/Problem8.cpp @@ -1,7 +1,7 @@ -//ProjectEuler/ProjectEulerCPP/Source/Problem8.cpp +//ProjectEuler/ProjectEulerCPP/src/Problems/Problem8.cpp //Matthew Ellison // 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? /* 73167176531330624919225119674426574742355349194934 @@ -27,7 +27,7 @@ */ //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 it under the terms of the GNU Lesser General Public License as published by @@ -45,9 +45,9 @@ #include +#include #include #include -#include #include "Problems/Problem8.hpp" @@ -64,9 +64,11 @@ void Problem8::solve(){ if(solved){ return; } + //Start the timer timer.start(); + //Cycle through the string of numbers looking for the maximum product 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)); @@ -79,6 +81,7 @@ void Problem8::solve(){ } } + //Stop the timer timer.stop(); @@ -94,10 +97,8 @@ void Problem8::reset(){ //Gets //Return a string with the solution to the problem -std::string Problem8::getResult(){ - if(!solved){ - throw Unsolved(); - } +std::string Problem8::getResult() const{ + solvedCheck("result"); std::stringstream result; result << "The greatest product is " << maxProduct << "\nThe numbers are " << maxNums; @@ -105,17 +106,11 @@ std::string Problem8::getResult(){ } //Returns the string of numbers that produces the largest product std::string Problem8::getLargestNums() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("numbers that make the largest product"); return maxNums; } //Returns the requested product uint64_t Problem8::getLargestProduct() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("product of the numbers"); return maxProduct; } diff --git a/src/Problems/Problem9.cpp b/src/Problems/Problem9.cpp index c7abb7e..be62cdc 100644 --- a/src/Problems/Problem9.cpp +++ b/src/Problems/Problem9.cpp @@ -1,11 +1,11 @@ -//ProjectEuler/ProjectEulerCPP/Source/Problem9.cpp +//ProjectEuler/ProjectEulerCPP/src/Problems/Problem9.cpp //Matthew Ellison // 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. //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 it under the terms of the GNU Lesser General Public License as published by @@ -23,8 +23,8 @@ #include -#include #include +#include #include "Problems/Problem9.hpp" @@ -41,18 +41,20 @@ void Problem9::solve(){ if(solved){ return; } + //Start the timer timer.start(); + //Loop through all possible a's while((a < GOAL_SUM) && !found){ 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 while((a + b + c) < GOAL_SUM){ ++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 @@ -64,19 +66,17 @@ void Problem9::solve(){ } } + //Stop the timer timer.stop(); - //Save the results + //Throw a flag to show the problem is solved if(found){ solved = true; } else{ 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 void Problem9::reset(){ @@ -89,10 +89,8 @@ void Problem9::reset(){ //Gets //Return a string with the solution to the problem -std::string Problem9::getResult(){ - if(!solved){ - throw Unsolved(); - } +std::string Problem9::getResult() const{ + solvedCheck("result"); std::stringstream result; result << "The Pythagorean triplet 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 int Problem9::getSideA() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("first side"); return a; } //Returns the length of the second side int Problem9::getSideB() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("second side"); return b; } //Returns the length of the hyp int Problem9::getSideC() const{ -//If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("third side"); return (int)c; } //Returns the product of the 3 sides int Problem9::getProduct() const{ - //If the problem hasn't been solved throw an exception - if(!solved){ - throw Unsolved(); - } + solvedCheck("product of all three sides"); return a * b * (int)c; } diff --git a/src/main.cpp b/src/main.cpp index 3c6b85c..44e993f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,11 +1,11 @@ //ProjectEuler/ProjectEulerCPP/main.cpp //Matthew Ellison // Created: 07-04-19 -//Modified: 07-09-20 +//Modified: 07-01-21 //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 /* - Copyright (C) 2020 Matthew Ellison + 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 @@ -25,7 +25,6 @@ #include #include #include "benchmark.hpp" -#include "Algorithms.hpp" #include "Problem.hpp" #include "ProblemSelection.hpp" @@ -102,9 +101,9 @@ void solveMenu(){ //Solve to every valid problem number, skipping over 0 for(unsigned int problemLocation = PROBLEM_NUMBERS[1];problemLocation < PROBLEM_NUMBERS.size();++problemLocation){ //Generate the current problem - problem = getProblem(PROBLEM_NUMBERS.at(problemLocation)); + problem = getProblem(PROBLEM_NUMBERS[problemLocation]); //Solve the problem - std::cout << PROBLEM_NUMBERS.at(problemLocation) << ". "; + std::cout << PROBLEM_NUMBERS[problemLocation] << ". "; solveProblem(problem); //Release the memory delete problem; @@ -137,9 +136,9 @@ void descriptionMenu(){ //Print description for every valid problem number, skipping over 0 for(unsigned int problemLocation = PROBLEM_NUMBERS[1];problemLocation < PROBLEM_NUMBERS.size();++problemLocation){ //Generate the problem - problem = getProblem(PROBLEM_NUMBERS.at(problemLocation)); + problem = getProblem(PROBLEM_NUMBERS[problemLocation]); //Print the problem's description - std::cout << PROBLEM_NUMBERS.at(problemLocation) << ". "; + std::cout << PROBLEM_NUMBERS[problemLocation] << ". "; printDescription(problem); std::cout << '\n'; //Release the memory