From a7d960c3b1052c9c8c8813540b47a5dc59bf1303 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Fri, 19 Jun 2020 19:57:59 -0400 Subject: [PATCH] Added solution to problem 31 --- Headers/Problem31.hpp | 52 ++++++++++++++++++++++++++ Problems.hpp | 32 ++++++++++++++++ Source/Problem31.cpp | 86 +++++++++++++++++++++++++++++++++++++++++++ main.cpp | 37 ++----------------- makefile | 2 +- 5 files changed, 175 insertions(+), 34 deletions(-) create mode 100644 Headers/Problem31.hpp create mode 100644 Problems.hpp create mode 100644 Source/Problem31.cpp diff --git a/Headers/Problem31.hpp b/Headers/Problem31.hpp new file mode 100644 index 0000000..f252ca3 --- /dev/null +++ b/Headers/Problem31.hpp @@ -0,0 +1,52 @@ +//ProjectEuler/C++/Headers/Problem31.hpp +//Matthew Ellison +// Created: 06-19-20 +//Modified: 06-19-20 +//How many different ways can £2 be made using any number of coins? +//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses +/* + Copyright (C) 2020 Matthew Ellison + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . +*/ + +#ifndef PROBLEM31_HPP +#define PROBLEM31_HPP + + +#include +#include +#include "Problem.hpp" + + +class Problem31 : public Problem{ +private: + static int desiredValue; + int permutations; +public: + Problem31(); + virtual void solve(); + virtual std::string getString() const; + //Returns the number of correct permutations of the coins + int getPermutations() const; +}; + + +/* Results: +There are 73682 ways to make 2 pounds with the given denominations of coins +It took 0.000 nanoseconds to solve this problem. +*/ + + +#endif //PROBLEM31_HPP diff --git a/Problems.hpp b/Problems.hpp new file mode 100644 index 0000000..3b0effc --- /dev/null +++ b/Problems.hpp @@ -0,0 +1,32 @@ +#include "Headers/Problem1.hpp" +#include "Headers/Problem2.hpp" +#include "Headers/Problem3.hpp" +#include "Headers/Problem4.hpp" +#include "Headers/Problem5.hpp" +#include "Headers/Problem6.hpp" +#include "Headers/Problem7.hpp" +#include "Headers/Problem8.hpp" +#include "Headers/Problem9.hpp" +#include "Headers/Problem10.hpp" +#include "Headers/Problem11.hpp" +#include "Headers/Problem12.hpp" +#include "Headers/Problem13.hpp" +#include "Headers/Problem14.hpp" +#include "Headers/Problem15.hpp" +#include "Headers/Problem16.hpp" +#include "Headers/Problem17.hpp" +#include "Headers/Problem18.hpp" +#include "Headers/Problem19.hpp" +#include "Headers/Problem20.hpp" +#include "Headers/Problem21.hpp" +#include "Headers/Problem22.hpp" +#include "Headers/Problem23.hpp" +#include "Headers/Problem24.hpp" +#include "Headers/Problem25.hpp" +#include "Headers/Problem26.hpp" +#include "Headers/Problem27.hpp" +#include "Headers/Problem28.hpp" +#include "Headers/Problem29.hpp" +#include "Headers/Problem30.hpp" +#include "Headers/Problem31.hpp" +#include "Headers/Problem67.hpp" diff --git a/Source/Problem31.cpp b/Source/Problem31.cpp new file mode 100644 index 0000000..389ecf0 --- /dev/null +++ b/Source/Problem31.cpp @@ -0,0 +1,86 @@ +//ProjectEuler/C++/Source/Problem31.cpp +//Matthew Ellison +// Created: 06-19-20 +//Modified: 06-19-20 +//How many different ways can £2 be made using any number of coins? +//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses +/* + Copyright (C) 2020 Matthew Ellison + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . +*/ + + +#include +#include +#include +#include "../Headers/Problem31.hpp" + + +int Problem31::desiredValue = 200; + +Problem31::Problem31() : Problem("How many different ways can 2 pounds be made using any number of coins?"){ + permutations = 0; +} + +void Problem31::solve(){ + //If the problem has alread been solved do nothing and end the function + if(solved){ + return; + } + + //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){ + for(int pence50 = pound1;pence50 >= 0;pence50 -= 50){ + for(int pence20 = pence50;pence20 >= 0;pence20 -= 20){ + for(int pence10 = pence20;pence10 >= 0;pence10 -= 10){ + for(int pence5 = pence10;pence5 >= 0;pence5 -= 5){ + for (int pence2 = pence5; pence2 >= 0; pence2 -= 2){ + ++permutations; + } + } + } + } + } + } + } + + + //Stop the timer + timer.stop(); + + //Throw a flag to show the problem is solved + solved = true; +} + +std::string Problem31::getString() const{ + //If the problem hasn't been solved throw an exception + if(!solved){ + throw unsolved(); + } + + std::stringstream results; + results << "There are " << permutations << " ways to make 2 pounds with the given denominations of coins"; + + return results.str(); +} + +//Returns the number of correct permutations of the coins +int Problem31::getPermutations() const{ + return permutations; +} diff --git a/main.cpp b/main.cpp index 717786e..150727c 100644 --- a/main.cpp +++ b/main.cpp @@ -1,7 +1,7 @@ //ProjectEuler/C++/main.cpp //Matthew Ellison // Created: 07-04-19 -//Modified: 07-08-19 +//Modified: 06-19-20 //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 @@ -10,37 +10,7 @@ #include #include "Algorithms.hpp" #include "Headers/Problem.hpp" -#include "Headers/Problem1.hpp" -#include "Headers/Problem2.hpp" -#include "Headers/Problem3.hpp" -#include "Headers/Problem4.hpp" -#include "Headers/Problem5.hpp" -#include "Headers/Problem6.hpp" -#include "Headers/Problem7.hpp" -#include "Headers/Problem8.hpp" -#include "Headers/Problem9.hpp" -#include "Headers/Problem10.hpp" -#include "Headers/Problem11.hpp" -#include "Headers/Problem12.hpp" -#include "Headers/Problem13.hpp" -#include "Headers/Problem14.hpp" -#include "Headers/Problem15.hpp" -#include "Headers/Problem16.hpp" -#include "Headers/Problem17.hpp" -#include "Headers/Problem18.hpp" -#include "Headers/Problem19.hpp" -#include "Headers/Problem20.hpp" -#include "Headers/Problem21.hpp" -#include "Headers/Problem22.hpp" -#include "Headers/Problem23.hpp" -#include "Headers/Problem24.hpp" -#include "Headers/Problem25.hpp" -#include "Headers/Problem26.hpp" -#include "Headers/Problem27.hpp" -#include "Headers/Problem28.hpp" -#include "Headers/Problem29.hpp" -#include "Headers/Problem30.hpp" -#include "Headers/Problem67.hpp" +#include "Problems.hpp" //Some helper functions to help with the menus @@ -61,7 +31,7 @@ enum MenuOptions {SOLVE = 1, DESCRIPTION, LIST, EXIT, SIZE}; std::vector PROBLEM_NUMBERS = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, - 67}; + 31, 67}; int main(){ int selection = 0; //Holds the menu selection of the user @@ -177,6 +147,7 @@ Problem* getProblem(unsigned int problemNumber){ case 28 : problem = new Problem28; break; case 29 : problem = new Problem29; break; case 30 : problem = new Problem30; break; + case 31 : problem = new Problem31; break; case 67 : problem = new Problem67; break; } diff --git a/makefile b/makefile index b807346..82c733e 100644 --- a/makefile +++ b/makefile @@ -3,7 +3,7 @@ NUMCORESWIN = ${NUMBER_OF_PROCESSORS} LIBFLAGS = -shared -std=c++17 -O3 -fPIC -Wall EXEFLAGS = -Wall -std=c++11 -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 67 +PROBLEM_NUMBERS = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 67 PROBLEM_FILES = $(patsubst %,Source/libProblem%.cpp,$(PROBLEM_NUMBERS)) LIBDIR = ./lib LIBS = $(patsubst %, -lProblem%,$(PROBLEM_NUMBERS))