mirror of
https://bitbucket.org/Mattrixwv/projecteulercpp.git
synced 2025-12-06 17:13:59 -05:00
104 lines
3.0 KiB
C++
104 lines
3.0 KiB
C++
//ProjectEuler/C++/Source/Problem20.cpp
|
|
//Matthew Ellison
|
|
// Created: 11-07-18
|
|
//Modified: 07-14-19
|
|
//What is the sum of the digits of 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 <cinttypes>
|
|
#include <string>
|
|
#include <sstream>
|
|
#include "gmpxx.h"
|
|
#include "Stopwatch.hpp"
|
|
#include "../Headers/Problem20.hpp"
|
|
|
|
|
|
Problem20::Problem20() : Problem("What is the sum of the digits of 100!?"), num(1), sum(0){
|
|
//num Starts at 1 because multiplication is performed on it
|
|
//sum starts at 0 because addition is performed on it
|
|
}
|
|
|
|
void Problem20::solve(){
|
|
//If the problem has already been solved do nothing and end the function
|
|
if(solved){
|
|
return;
|
|
}
|
|
|
|
//Start the timer
|
|
timer.start();
|
|
|
|
//Run through every number from 1 to 100 and multiply it by the current num
|
|
for(int cnt = 1;cnt <= 100;++cnt){
|
|
num *= cnt;
|
|
}
|
|
|
|
//Get a string of the number because it is easier to pull appart the individual characters
|
|
std::string numString = num.get_str();
|
|
//Run through every character in the string, convert it back to an integer and add it to the running sum
|
|
for(unsigned int cnt = 0;cnt < numString.size();++cnt){
|
|
sum += std::stoi(numString.substr(cnt, 1));
|
|
}
|
|
|
|
//Stop the timer
|
|
timer.stop();
|
|
|
|
//Throw a flag to show the problem is solved
|
|
solved = true;
|
|
}
|
|
|
|
std::string Problem20::getString() const{
|
|
//If the problem hasn't been solved throw an exception
|
|
if(!solved){
|
|
throw unsolved();
|
|
}
|
|
std::stringstream results;
|
|
//Print the results
|
|
results << "100! = " << num.get_str()
|
|
<< "\nThe sum of the digits is: " << sum;
|
|
return results.str();
|
|
}
|
|
|
|
mpz_class Problem20::getNumber() const{
|
|
//If the problem hasn't been solved throw an exception
|
|
if(!solved){
|
|
throw unsolved();
|
|
}
|
|
return num;
|
|
}
|
|
|
|
std::string Problem20::getNumberString() const{
|
|
//If the problem hasn't been solved throw an exception
|
|
if(!solved){
|
|
throw unsolved();
|
|
}
|
|
return num.get_str();
|
|
}
|
|
|
|
uint64_t Problem20::getSum() const{
|
|
//If the problem hasn't been solved throw an exception
|
|
if(!solved){
|
|
throw unsolved();
|
|
}
|
|
return sum;
|
|
}
|