mirror of
https://bitbucket.org/Mattrixwv/projecteulercpp.git
synced 2025-12-06 17:13:59 -05:00
Updated comments and made sure style was consistent
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/C++/Source/Problem14.cpp
|
||||
//ProjectEuler/ProjectEulerCPP/Source/Problem14.cpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-29-18
|
||||
//Modified: 07-14-19
|
||||
//Modified: 07-09-20
|
||||
/*
|
||||
The following iterative sequence is defined for the set of positive integers:
|
||||
n → n/2 (n is even)
|
||||
@@ -10,7 +10,7 @@ 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
|
||||
Copyright (C) 2020 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
|
||||
@@ -35,8 +35,10 @@ Which starting number, under one million, produces the longest chain?
|
||||
#include "../Headers/Problem14.hpp"
|
||||
|
||||
|
||||
uint64_t Problem14::MAX_NUM = 1000000;
|
||||
//This is the top number that you will be checking against the series
|
||||
uint64_t Problem14::MAX_NUM = 999999;
|
||||
|
||||
//This function follows the rules of the sequence and returns its length
|
||||
uint64_t Problem14::checkSeries(uint64_t num){
|
||||
uint64_t length = 1; //Start at 1 because you need to count the starting number
|
||||
|
||||
@@ -53,9 +55,11 @@ uint64_t Problem14::checkSeries(uint64_t num){
|
||||
return length;
|
||||
}
|
||||
|
||||
//Constructor
|
||||
Problem14::Problem14() : Problem("Which starting number, under one million, produces the longest chain using the itterative sequence?"), maxLength(0), maxNum(0){
|
||||
}
|
||||
|
||||
//Solve the problem
|
||||
void Problem14::solve(){
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
@@ -66,7 +70,7 @@ void Problem14::solve(){
|
||||
timer.start();
|
||||
|
||||
//Loop through all numbers less than 1000000 and check them agains the series
|
||||
for(uint64_t currentNum = 1;currentNum < MAX_NUM;++currentNum){
|
||||
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){
|
||||
@@ -82,6 +86,14 @@ void Problem14::solve(){
|
||||
solved = true;
|
||||
}
|
||||
|
||||
//Reset the problem so it can be run again
|
||||
void Problem14::reset(){
|
||||
Problem::reset();
|
||||
maxLength = 0;
|
||||
maxNum = 0;
|
||||
}
|
||||
|
||||
//Return a string with the solution to the problem
|
||||
std::string Problem14::getString() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -93,6 +105,7 @@ std::string Problem14::getString() const{
|
||||
return results.str();
|
||||
}
|
||||
|
||||
//Returns the length of the requested chain
|
||||
uint64_t Problem14::getLength() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -101,6 +114,7 @@ uint64_t Problem14::getLength() const{
|
||||
return maxLength;
|
||||
}
|
||||
|
||||
//Returns the starting number of the requested chain
|
||||
uint64_t Problem14::getStartingNumber() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -108,9 +122,3 @@ uint64_t Problem14::getStartingNumber() const{
|
||||
}
|
||||
return maxNum;
|
||||
}
|
||||
|
||||
void Problem14::reset(){
|
||||
Problem::reset();
|
||||
maxLength = 0;
|
||||
maxNum = 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user