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,7 +1,7 @@
//ProjectEuler/C++/Source/Problem13.cpp
//ProjectEuler/ProjectEulerCPP/Source/Problem13.cpp
//Matthew Ellison
// Created: 09-29-18
//Modified: 07-14-19
//Modified: 07-09-20
//Work out the first ten digits of the sum of the following one-hundred 50-digit numbers
/*
37107287533902102798797998220837590246510135740250
@@ -110,7 +110,7 @@
//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
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
@@ -138,16 +138,7 @@
#include "../Headers/Problem13.hpp"
Problem13::Problem13() : Problem("Work out the first ten digits of the sum of the one-hundred 50-digit numbers"), sum(0){
reserveVectors();
}
void Problem13::reserveVectors(){
//Make sure the vector is the correct size
nums.reserve(100);
nums.resize(100);
}
//A function to set the nums vector
void Problem13::setNums(){
//Set the numbers
nums[0] = "37107287533902102798797998220837590246510135740250";
@@ -252,6 +243,19 @@ void Problem13::setNums(){
nums[99] = "53503534226472524250874054075591789781264330331690";
}
//Reserve the size of the vector to speed up insertion
void Problem13::reserveVectors(){
//Make sure the vector is the correct size
nums.reserve(100);
nums.resize(100);
}
//Constructor
Problem13::Problem13() : Problem("Work out the first ten digits of the sum of the one-hundred 50-digit numbers"), sum(0){
reserveVectors();
}
//Solve the problem
void Problem13::solve(){
//If the problem has already been solved do nothing and end the function
if(solved){
@@ -274,6 +278,15 @@ void Problem13::solve(){
solved = true;
}
//Reset the problem so it can be run again
void Problem13::reset(){
Problem::reset();
sum = 0;
nums.clear();
reserveVectors();
}
//Return a string with the solution to the problem
std::string Problem13::getString() const{
//If the problem hasn't been solved throw an exception
if(!solved){
@@ -286,6 +299,7 @@ std::string Problem13::getString() const{
return results.str();
}
//Returns the list 50-digit numbers
std::vector<mpz_class> Problem13::getNumbers() const{
//If the problem hasn't been solved throw an exception
if(!solved){
@@ -294,6 +308,7 @@ std::vector<mpz_class> Problem13::getNumbers() const{
return nums;
}
//Returns the sum of the 50-digit numbers
mpz_class Problem13::getSum() const{
//If the problem hasn't been solved throw an exception
if(!solved){
@@ -301,10 +316,3 @@ mpz_class Problem13::getSum() const{
}
return sum;
}
void Problem13::reset(){
Problem::reset();
sum = 0;
nums.clear();
reserveVectors();
}