mirror of
https://bitbucket.org/Mattrixwv/projecteulercpp.git
synced 2025-12-06 17:13:59 -05:00
112 lines
3.0 KiB
C++
112 lines
3.0 KiB
C++
//ProjectEuler/C++/Source/Problem14.cpp
|
|
//Matthew Ellison
|
|
// Created: 09-29-18
|
|
//Modified: 07-14-19
|
|
/*
|
|
The following iterative sequence is defined for the set of positive integers:
|
|
n → n/2 (n is even)
|
|
n → 3n + 1 (n is odd)
|
|
Which starting number, under one million, produces the longest chain?
|
|
*/
|
|
//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 <cinttypes>
|
|
#include <string>
|
|
#include <sstream>
|
|
#include "Stopwatch.hpp"
|
|
#include "../Headers/Problem.hpp"
|
|
#include "../Headers/Problem14.hpp"
|
|
|
|
|
|
uint64_t Problem14::MAX_NUM = 1000000;
|
|
|
|
uint64_t Problem14::checkSeries(uint64_t num){
|
|
uint64_t length = 1; //Start at 1 because you need to count the starting number
|
|
|
|
//Follow the series, adding 1 for each step you take
|
|
while(num > 1){
|
|
if((num % 2) == 0){
|
|
num /= 2;
|
|
}
|
|
else{
|
|
num = (3 * num) + 1;
|
|
}
|
|
++length;
|
|
}
|
|
return length;
|
|
}
|
|
|
|
Problem14::Problem14() : Problem("Which starting number, under one million, produces the longest chain using the itterative sequence?"), maxLength(0), maxNum(0){
|
|
|
|
}
|
|
|
|
void Problem14::solve(){
|
|
//If the problem has already been solved do nothing and end the function
|
|
if(solved){
|
|
return;
|
|
}
|
|
|
|
//Start the timer
|
|
timer.start();
|
|
|
|
//Loop through all numbers less than 1000000 and check them agains the series
|
|
for(uint64_t currentNum = 1;currentNum < MAX_NUM;++currentNum){
|
|
uint64_t currentLength = checkSeries(currentNum);
|
|
//If the current number has a longer series than the max then the current becomes the max
|
|
if(currentLength > maxLength){
|
|
maxLength = currentLength;
|
|
maxNum = currentNum;
|
|
}
|
|
}
|
|
|
|
//Stop the timer
|
|
timer.stop();
|
|
|
|
//Throw a flag to show the problem is solved
|
|
solved = true;
|
|
}
|
|
|
|
std::string Problem14::getString() const{
|
|
//If the problem hasn't been solved throw an exception
|
|
if(!solved){
|
|
throw unsolved();
|
|
}
|
|
|
|
std::stringstream results;
|
|
results << "The number " << maxNum << " produced a chain of " << maxLength << " steps";
|
|
return results.str();
|
|
}
|
|
|
|
uint64_t Problem14::getLength() const{
|
|
//If the problem hasn't been solved throw an exception
|
|
if(!solved){
|
|
throw unsolved();
|
|
}
|
|
return maxLength;
|
|
}
|
|
|
|
uint64_t Problem14::getStartingNumber() const{
|
|
//If the problem hasn't been solved throw an exception
|
|
if(!solved){
|
|
throw unsolved();
|
|
}
|
|
return maxNum;
|
|
}
|