Files
ProjectEulerCPP/Source/Problem29.cpp

121 lines
3.3 KiB
C++

//ProjectEuler/C++/Source/Problem29.cpp
//Matthew Ellison
// Created: 10-06-19
//Modified: 10-06-19
//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) 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 <vector>
#include <cmath>
#include <gmpxx.h>
#include "../Headers/Problem29.hpp"
#include "Algorithms.hpp"
Problem29::Problem29(){
}
void Problem29::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 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
for(unsigned long currentB = BOTTOM_B;currentB <= TOP_B;++currentB){
mpz_class num;
mpz_ui_pow_ui(num.get_mpz_t(), currentA, currentB);
//If the number is not found add it to the list
if(!mee::isFound(unique, num)){
unique.push_back(num);
}
}
}
//Stop the timer
timer.stop();
//Throw a flag to show the problem is solved
solved = true;
}
std::string Problem29::getString() const{
//If the problem hasn't been solved throw an exception
if(!solved){
throw unsolved();
}
std::stringstream results;
results << "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 results.str();
}
unsigned int Problem29::getBottomA() const{
//If the problem hasn't been solved throw an exception
if(!solved){
throw unsolved();
}
return BOTTOM_A;
}
unsigned int Problem29::getTopA() const{
//If the problem hasn't been solved throw an exception
if(!solved){
throw unsolved();
}
return TOP_A;
}
unsigned int Problem29::getBottomB() const{
//If the problem hasn't been solved throw an exception
if(!solved){
throw unsolved();
}
return BOTTOM_B;
}
unsigned int Problem29::getTopB() const{
//If the problem hasn't been solved throw an exception
if(!solved){
throw unsolved();
}
return TOP_B;
}
std::vector<mpz_class> Problem29::getUnique() const{
//If the problem hasn't been solved throw an exception
if(!solved){
throw unsolved();
}
return unique;
}