mirror of
https://bitbucket.org/Mattrixwv/projecteulercpp.git
synced 2025-12-06 17:13:59 -05:00
Inital commit with existing files
This commit is contained in:
110
Source/Problem27.cpp
Normal file
110
Source/Problem27.cpp
Normal file
@@ -0,0 +1,110 @@
|
||||
//ProjectEuler/C++/Source/Problem27.cpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-14-19
|
||||
//Modified: 09-14-19
|
||||
//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) 2019 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 <string>
|
||||
#include <sstream>
|
||||
#include <cinttypes>
|
||||
#include "Algorithms.hpp"
|
||||
#include "../Headers/Problem27.hpp"
|
||||
|
||||
|
||||
Problem27::Problem27(){
|
||||
topA = topB = topN = 0;
|
||||
primes = mee::getPrimes((int64_t)(12000));
|
||||
}
|
||||
|
||||
void Problem27::solve(){
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
return;
|
||||
}
|
||||
|
||||
//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
|
||||
for(int64_t b = -1000;b <= 1000;++b){
|
||||
//Start with n=0 and check the formula to see how many primes you can get with concecutive n's
|
||||
int64_t n = 0;
|
||||
int64_t quadratic = (n * n) + (a * n) + b;
|
||||
while(mee::isFound(primes, quadratic)){
|
||||
++n;
|
||||
quadratic = (n * n) + (a * n) + b;
|
||||
}
|
||||
--n; //Negate an n because the last formula failed
|
||||
|
||||
//Set all the largest numbers if this created more primes than any other
|
||||
if(n > topN){
|
||||
topN = n;
|
||||
topB = b;
|
||||
topA = a;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Stop the timer
|
||||
timer.stop();
|
||||
|
||||
//Throw a flag to show the problem is solved
|
||||
solved = true;
|
||||
}
|
||||
|
||||
std::string Problem27::getString() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
throw unsolved();
|
||||
}
|
||||
|
||||
std::stringstream results;
|
||||
results << "The greatest number of primes found is " << topN
|
||||
<< "\nIt was found with A = " << topA << ", B = " << topB
|
||||
<< "\nThe product of A and B is " << topA * topB;
|
||||
|
||||
return results.str();
|
||||
}
|
||||
|
||||
int64_t Problem27::getTopA() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
throw unsolved();
|
||||
}
|
||||
return topA;
|
||||
}
|
||||
|
||||
int64_t Problem27::getTopB() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
throw unsolved();
|
||||
}
|
||||
return topB;
|
||||
}
|
||||
|
||||
int64_t Problem27::getTopN() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
throw unsolved();
|
||||
}
|
||||
return topN;
|
||||
}
|
||||
Reference in New Issue
Block a user