Updated comments and made sure style was consistent

This commit is contained in:
2020-07-10 13:36:16 -04:00
parent 7257a118d4
commit c72754dcf8
65 changed files with 1160 additions and 747 deletions

View File

@@ -1,11 +1,11 @@
//ProjectEuler/C++/Source/Problem23.cpp
//ProjectEuler/ProjectEulerCPP/Source/Problem23.cpp
//Matthew Ellison
// Created: 11-09-18
//Modified: 07-14-19
//Modified: 07-09-20
//Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers
//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
@@ -32,18 +32,10 @@
#include "../Headers/Problem23.hpp"
//The largest possible number that can not be written as the sum of two abundant numbers
int Problem23::MAX_NUM = 28123;
Problem23::Problem23() : Problem("Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers"), sum(0){
reserveVectors();
}
void Problem23::reserveVectors(){
//This makes sure the vector is the correct size
divisorSums.reserve(MAX_NUM);
divisorSums.resize(MAX_NUM);
}
//A function that returns true if num can be created by adding two elements from abund and false if it cannot
bool Problem23::isSum(const std::vector<int>& abund, int num){
int64_t sum = 0;
//Pick a number for the first part of the sum
@@ -63,6 +55,19 @@ bool Problem23::isSum(const std::vector<int>& abund, int num){
return false;
}
//Reserve the size of the vector to speed up insertion
void Problem23::reserveVectors(){
//This makes sure the vector is the correct size
divisorSums.reserve(MAX_NUM + 1);
divisorSums.resize(MAX_NUM + 1);
}
//Constructor
Problem23::Problem23() : Problem("Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers"), sum(0){
reserveVectors();
}
//Solve the problem
void Problem23::solve(){
//If the problem has already been solved do nothing and end the function
if(solved){
@@ -73,7 +78,7 @@ void Problem23::solve(){
timer.start();
//Get the sum of the divisors of all numbers < MAX_NUM
for(int cnt = 1;cnt < MAX_NUM;++cnt){
for(int cnt = 1;cnt <= MAX_NUM;++cnt){
std::vector<int> div = mee::getDivisors(cnt);
if(div.size() > 1){
div.pop_back(); //Remove the last element, which is the number itself. This gives us the propper divisors
@@ -90,7 +95,7 @@ void Problem23::solve(){
}
//Check if each number can be the sum of 2 abundant numbers and add to the sum if no
for(int cnt = 1;cnt < MAX_NUM;++cnt){
for(int cnt = 1;cnt <= MAX_NUM;++cnt){
if(!isSum(abund, cnt)){
sum += cnt;
}
@@ -103,6 +108,15 @@ void Problem23::solve(){
solved = true;
}
//Reset the problem so it can be run again
void Problem23::reset(){
Problem::reset();
sum = 0;
divisorSums.clear();
reserveVectors();
}
//Return a string with the solution to the problem
std::string Problem23::getString() const{
//If the problem hasn't been solved throw an exception
if(!solved){
@@ -113,6 +127,7 @@ std::string Problem23::getString() const{
return results.str();
}
//Returns the sum of the numbers asked for
uint64_t Problem23::getSum() const{
//If the problem hasn't been solved throw an exception
if(!solved){
@@ -120,10 +135,3 @@ uint64_t Problem23::getSum() const{
}
return sum;
}
void Problem23::reset(){
Problem::reset();
sum = 0;
divisorSums.clear();
reserveVectors();
}