Added solution to problem 37

This commit is contained in:
2021-07-01 11:36:26 -04:00
parent ebdb92dc2f
commit fd54eb90d8
6 changed files with 217 additions and 3 deletions

View File

@@ -62,6 +62,7 @@
#include "Problems/Problem34.hpp"
#include "Problems/Problem35.hpp"
#include "Problems/Problem36.hpp"
#include "Problems/Problem37.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, 67};
31, 32, 33, 34, 35, 36, 37, 67};
//This function returns a pointer to a problem of type number
Problem* getProblem(unsigned int problemNumber){
@@ -113,6 +114,7 @@ Problem* getProblem(unsigned int problemNumber){
case 34 : problem = new Problem34; break;
case 35 : problem = new Problem35; break;
case 36 : problem = new Problem36; break;
case 37 : problem = new Problem37; break;
case 67 : problem = new Problem67; break;
}

View File

@@ -40,6 +40,7 @@ private:
std::vector<int> palindromes; //All numbers that are palindromes in base 10 and 2
int sum; //The sum of all elements in the vector palindromes
public:
//Functions
//Constructor
Problem36();
//Operational functions

View File

@@ -0,0 +1,60 @@
//ProjectEuler/ProjectEulerCPP/headers/Problems/Problem37.hpp
//Matthew Ellison
// Created: 06-30-21
//Modified: 06-30-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
/*
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef PROBLEM37_HPP
#define PROBLEM37_HPP
#include <string>
#include <vector>
#include "Problem.hpp"
class Problem37 : public Problem{
private:
//Variables
//Static variables
static uint64_t LAST_PRIME_BEFORE_CHECK; //The last prime before 11 since single digit primes aren't checked
//Instance variables
std::vector<uint64_t> truncPrimes; //All numbers that are truncatable primes
uint64_t sum; //The sum of all elements in truncPrimes
public:
//Functions
//Constructor
Problem37();
//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(); //Returns a string with the solution to the problem
std::vector<uint64_t> getTruncatablePrimes(); //Returns the list of primes that can be truncated
uint64_t getSumOfPrimes(); //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
*/
#endif //PROBLEM37_HPP