//ProjectEuler/C++/Source/Problem1.cpp //Matthew Ellison // Created: 07-10-19 //Modified: 07-10-19 //What is the sum of all the multiples of 3 or 5 that are less than 1000 //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 . */ #include #include #include #include #include "Stopwatch.hpp" #include "../Headers/Problem.hpp" #include "../Headers/Problem1.hpp" uint64_t Problem1::MAX_NUMBER = 1000; Problem1::Problem1() : Problem("What is the sum of all the multiples of 3 or 5 that are less than 1000?"), fullSum(0){ } void Problem1::solve(){ //If the problem has already been solved do nothing and end the function if(solved){ return; } //Start the timer timer.start(); //Step through every number < 1000 and see if either 3 or 5 divide it evenly for(uint64_t cnt = 1;cnt < MAX_NUMBER;++cnt){ //If either divides it then add it to the vector if((cnt % 3) == 0){ numbers.push_back(cnt); } else if((cnt % 5) == 0){ numbers.push_back(cnt); } } //Get the sum of all numbers for(uint64_t num : numbers){ fullSum += num; } //Stop the timer timer.stop(); //Throw a flag to show the problem is solved solved = true; } std::string Problem1::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 < 1000 that are divisible by 3 or 5 is " << fullSum; return results.str(); } uint64_t Problem1::getSum() const{ if(!solved){ throw unsolved(); } return fullSum; } void Problem1::reset(){ Problem::reset(); fullSum = 0; numbers.clear(); }