mirror of
https://bitbucket.org/Mattrixwv/projecteulercpp.git
synced 2025-12-06 17:13:59 -05:00
128 lines
3.7 KiB
C++
128 lines
3.7 KiB
C++
//ProjectEuler/C++/Source/Problem30.cpp
|
|
//Matthew Ellison
|
|
// Created: 10-27-19
|
|
//Modified: 10-27-19
|
|
//Find the sum of all the numbers that can be written as the sum of the fifth powers of their digits.
|
|
//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 <vector>
|
|
#include <cmath>
|
|
#include "../Headers/Problem30.hpp"
|
|
|
|
|
|
//Returns a vector with the indivitual digits of the number passed into it
|
|
std::vector<uint64_t> Problem30::getDigits(uint64_t num){
|
|
std::vector<uint64_t> listOfDigits; //This vector holds the individual digits of num
|
|
//The easiest way to get the individual digits of a number is by converting it to a string
|
|
std::string digits = std::to_string(num); //Holds a string representation of num
|
|
//Start with the first digit, convert it to an integer, store it in the vector, and move to the next digit
|
|
for(int cnt = 0;cnt < digits.size();++cnt){
|
|
listOfDigits.push_back(std::stoi(digits.substr(cnt, 1)));
|
|
}
|
|
//Return the list of digits
|
|
return listOfDigits;
|
|
}
|
|
|
|
Problem30::Problem30(){
|
|
|
|
}
|
|
|
|
void Problem30::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 number and increment until you reach the largest number
|
|
for(uint64_t currentNum = BOTTOM_NUM;currentNum <= TOP_NUM;++currentNum){
|
|
//Get the digits of the number
|
|
std::vector<uint64_t> digits = getDigits(currentNum);
|
|
//Get the sum of the powers
|
|
uint64_t sumOfPowers = 0;
|
|
for(uint64_t num : digits){
|
|
sumOfPowers += std::pow(num, POWER_RAISED);
|
|
}
|
|
//Check if the sum of the powers is the same as the number
|
|
//If it is add it to the list, otherwise continue to the next number
|
|
if(sumOfPowers == currentNum){
|
|
sumOfFifthNumbers.push_back(currentNum);
|
|
}
|
|
}
|
|
|
|
//Stop the timer
|
|
timer.stop();
|
|
|
|
//Throw a flag to show the problem is solved
|
|
solved = true;
|
|
}
|
|
|
|
std::string Problem30::getString() const{
|
|
//If the problem hasn't been solved throw an exception
|
|
if(!solved){
|
|
throw unsolved();
|
|
}
|
|
|
|
std::stringstream results;
|
|
results << "The sum of all the numbers that can be written as the sum of the fifth powers of their digits is " << getSumOfList();
|
|
|
|
return results.str();
|
|
}
|
|
|
|
//This returns the top number to be checked
|
|
uint64_t Problem30::getTopNum() const{
|
|
//If the problem hasn't been solved throw an exception
|
|
if(!solved){
|
|
throw unsolved();
|
|
}
|
|
|
|
return TOP_NUM;
|
|
}
|
|
|
|
//This returns a copy of the vector holding all the numbers that are the sum of the fifth power of their digits
|
|
std::vector<uint64_t> Problem30::getListOfSumOfFifths() const{
|
|
//If the problem hasn't been solved throw an exception
|
|
if(!solved){
|
|
throw unsolved();
|
|
}
|
|
|
|
return sumOfFifthNumbers;
|
|
}
|
|
|
|
//This returns the sum of all entries in sumOfFifthNumbers
|
|
uint64_t Problem30::getSumOfList() const{
|
|
//If the problem hasn't been solved throw an exception
|
|
if(!solved){
|
|
throw unsolved();
|
|
}
|
|
|
|
uint64_t sum = 0;
|
|
for(uint64_t num : sumOfFifthNumbers){
|
|
sum += num;
|
|
}
|
|
|
|
return sum;
|
|
}
|