Added solution to problem 38

This commit is contained in:
2021-10-10 23:00:10 -04:00
parent 6e4dd45702
commit 82634c02ed
4 changed files with 159 additions and 2 deletions

View File

@@ -62,6 +62,7 @@
#include "Problems/Problem35.hpp"
#include "Problems/Problem36.hpp"
#include "Problems/Problem37.hpp"
#include "Problems/Problem38.hpp"
#include "Problems/Problem67.hpp"
@@ -69,7 +70,7 @@
std::vector<unsigned int> PROBLEM_NUMBERS = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
31, 32, 33, 34, 35, 36, 37, 67};
31, 32, 33, 34, 35, 36, 37, 38, 67};
//This function returns a pointer to a problem of type number
Problem* getProblem(unsigned int problemNumber){
@@ -114,6 +115,7 @@ Problem* getProblem(unsigned int problemNumber){
case 35 : problem = new Problem35; break;
case 36 : problem = new Problem36; break;
case 37 : problem = new Problem37; break;
case 38 : problem = new Problem38; break;
case 67 : problem = new Problem67; break;
}

View File

@@ -0,0 +1,51 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem38.hpp
//Matthew Ellison
// Created: 10-10-21
//Modified: 10-10-21
//What is the largest 1-9 pandigital number that can be formed as the concatenated product of an integer with 1, 2, ... n where n > 1
/*
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <cinttypes>
#include <string>
#include "Problem.hpp"
class Problem38 : public Problem{
private:
//Variables
//Static variables
static int HIGHEST_POSSIBLE_NUM; //The highest number that needs to be checked for a 1-9 pandigital
//Instance variables
uint64_t largestNum;
uint64_t pandigital;
//Functions
std::string executeFormula(int num); //Take the number and add its multiples to a string to return
public:
//Functions
//Constructor
Problem38();
//Operational functions
virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again
//Gets
virtual std::string getResult() const; //Returns a string with the solution to the problem
uint64_t getLargestNum() const; //Returns the largest number
uint64_t getPandigital() const; //Returns the pandigital of the number
};

View File

@@ -3,7 +3,7 @@ NUMCORESWIN = ${NUMBER_OF_PROCESSORS}
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
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 38 67
SOURCE_DIR = src
PROBLEM_DIR = $(SOURCE_DIR)/Problems
INCLUDE_DIR = headers

104
src/Problems/Problem38.cpp Normal file
View File

@@ -0,0 +1,104 @@
//ProjectEuler/ProjectEulerCPP/src/Problems/Problem38.cpp
//Matthew Ellison
// Created: 10-10-21
//Modified: 10-10-21
//What is the largest 1-9 pandigital number that can be formed as the concatenated product of an integer with 1, 2, ... n where n > 1
/*
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <sstream>
#include <string>
#include "mee/stringAlgorithms.hpp"
#include "Problems/Problem38.hpp"
int Problem38::HIGHEST_POSSIBLE_NUM = 9999;
//Constructor
Problem38::Problem38() : Problem("What is the largest 1-9 pandigital number that can be formed as the concatenated product of an integer with 1, 2, ... n where n > 1"), largestNum(0), pandigital(0){
}
//Operational functions
//Take the number and add its multiples to a string to return
std::string Problem38::executeFormula(int num){
//Turn the current number into a string
std::string numStr = std::to_string(num);
int cnt = 2;
//Multiply the number and append the product to the string until you have one long enough
do{
numStr += std::to_string(num * cnt);
++cnt;
}while(numStr.size() < 9);
return numStr;
}
//Solve the porblem
void Problem38::solve(){
//If the problem has already been solved do nothing and end the function
if(solved){
return;
}
//Start the timer
timer.start();
//Loop from 1 -> HIGHEST_POSSIBLE_NUM checking for pandigitals
for(int cnt = 1;cnt <= HIGHEST_POSSIBLE_NUM;++cnt){
//Get the string from the formula
std::string numStr = executeFormula(cnt);
uint64_t panNum = std::stoull(numStr);
//If the number is pandigital save it as the highest number
if(mee::isPandigital(numStr) && (panNum > pandigital)){
largestNum = cnt;
pandigital = panNum;
}
}
//Stop the timer
timer.stop();
//Throw a flag to show the porblem is solved
solved = true;
}
//Returns a string with the solutino to the problem
void Problem38::reset(){
largestNum = 0;
pandigital = 0;
}
//Gets
//Returns a string with the solution to the porblem
std::string Problem38::getResult() const{
solvedCheck("result");
std::stringstream result;
result << "The largest appended product pandigital is " << pandigital;
return result.str();
}
//Returns the largest number
uint64_t Problem38::getLargestNum() const{
solvedCheck("largest number");
return largestNum;
}
//Returns the pandigital of the number
uint64_t Problem38::getPandigital() const{
solvedCheck("pandigital");
return pandigital;
}