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,11 +1,11 @@
|
||||
//ProjectEuler/C++/Source/Problem1.cpp
|
||||
//ProjectEuler/ProjectEulerCPP/Source/Problem1.cpp
|
||||
//Matthew Ellison
|
||||
// Created: 07-10-19
|
||||
//Modified: 07-10-19
|
||||
//Modified: 07-09-20
|
||||
//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
|
||||
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
|
||||
@@ -31,11 +31,15 @@
|
||||
#include "../Headers/Problem1.hpp"
|
||||
|
||||
|
||||
uint64_t Problem1::MAX_NUMBER = 1000;
|
||||
//The highest number to be tested
|
||||
uint64_t Problem1::MAX_NUMBER = 999;
|
||||
|
||||
//Constructor
|
||||
Problem1::Problem1() : Problem("What is the sum of all the multiples of 3 or 5 that are less than 1000?"), fullSum(0){
|
||||
}
|
||||
|
||||
//Operational functions
|
||||
//Solve the problem
|
||||
void Problem1::solve(){
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
@@ -45,8 +49,8 @@ void Problem1::solve(){
|
||||
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
|
||||
for(uint64_t cnt = 1;cnt <= MAX_NUMBER;++cnt){
|
||||
//If either 3 or 5 divides it evenly then add it to the vector
|
||||
if((cnt % 3) == 0){
|
||||
numbers.push_back(cnt);
|
||||
}
|
||||
@@ -67,25 +71,31 @@ void Problem1::solve(){
|
||||
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;
|
||||
}
|
||||
|
||||
//Reset the problem so it can be run again
|
||||
void Problem1::reset(){
|
||||
Problem::reset();
|
||||
fullSum = 0;
|
||||
numbers.clear();
|
||||
}
|
||||
|
||||
|
||||
//Gets
|
||||
//Return a string with the solution to the problem
|
||||
std::string Problem1::getString() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
throw unsolved();
|
||||
}
|
||||
//Create a string with the results and return it
|
||||
std::stringstream results;
|
||||
results << "The sum of all the numbers < " << MAX_NUMBER + 1 << " that are divisible by 3 or 5 is " << fullSum;
|
||||
return results.str();
|
||||
}
|
||||
|
||||
uint64_t Problem1::getSum() const{
|
||||
//If the prblem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
throw unsolved();
|
||||
}
|
||||
return fullSum;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
//ProjectEuler/C++/Source/Problem10.cpp
|
||||
//ProjectEuler/ProjectEulerCPP/Source/Problem10.cpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-28-18
|
||||
//Modified: 07-14-19
|
||||
//Modified: 07-09-20
|
||||
//Find the sum of all the primes below two million
|
||||
//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
|
||||
@@ -31,11 +31,13 @@
|
||||
#include "../Headers/Problem10.hpp"
|
||||
|
||||
|
||||
uint64_t Problem10::GOAL_NUMBER = 2000000;
|
||||
uint64_t Problem10::GOAL_NUMBER = 2000000 - 1; //2,000,000 - 1 because is needs to be < instead of <=
|
||||
|
||||
//Constructor
|
||||
Problem10::Problem10() : Problem("Find the sum of all the primes below two million"), sum(0){
|
||||
}
|
||||
|
||||
//Solve
|
||||
void Problem10::solve(){
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
@@ -44,8 +46,8 @@ void Problem10::solve(){
|
||||
//Start the timer
|
||||
timer.start();
|
||||
|
||||
//Get the sum of all prime numbers < GOAL_NUMBER
|
||||
sum = mee::getSum(mee::getPrimes(GOAL_NUMBER - 1)); //Subtract 1 because it is supposed to be < 2000000
|
||||
//Get the sum of all prime numbers <= GOAL_NUMBER
|
||||
sum = mee::getSum(mee::getPrimes(GOAL_NUMBER));
|
||||
|
||||
//Stop the timer
|
||||
timer.stop();
|
||||
@@ -54,16 +56,24 @@ void Problem10::solve(){
|
||||
solved = true;
|
||||
}
|
||||
|
||||
//Reset the rpoblem so it can be run agian
|
||||
void Problem10::reset(){
|
||||
Problem::reset();
|
||||
sum = 0;
|
||||
}
|
||||
|
||||
//Return a string with the solution to the problem
|
||||
std::string Problem10::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 primes less than " << GOAL_NUMBER << " is " << sum;
|
||||
results << "The sum of all the primes less than " << GOAL_NUMBER + 1 << " is " << sum;
|
||||
return results.str();
|
||||
}
|
||||
|
||||
//Returns the sum that was requested
|
||||
uint64_t Problem10::getSum() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -71,8 +81,3 @@ uint64_t Problem10::getSum() const{
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
void Problem10::reset(){
|
||||
Problem::reset();
|
||||
sum = 0;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/C++/Source/Problem11.cpp
|
||||
//MatthewEllison
|
||||
//ProjectEuler/ProjectEulerCPP/Source/Problem11.cpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-29-18
|
||||
//Modified: 07-14-19
|
||||
//Modified: 07-09-20
|
||||
//What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid?
|
||||
/*
|
||||
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
|
||||
@@ -27,7 +27,7 @@
|
||||
*/
|
||||
//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
|
||||
@@ -53,6 +53,7 @@
|
||||
#include "../Headers/Problem11.hpp"
|
||||
|
||||
|
||||
//This is the grid of number that we will be working with
|
||||
std::vector<int> Problem11::grid[20] = {{ 8, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12, 50, 77, 91, 8},
|
||||
{49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 04, 56, 62, 00},
|
||||
{81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 03, 49, 13, 36, 65},
|
||||
@@ -74,9 +75,11 @@ std::vector<int> Problem11::grid[20] = {{ 8, 02, 22, 97, 38, 15, 00, 40, 00, 75,
|
||||
{20, 73, 35, 29, 78, 31, 90, 01, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 05, 54},
|
||||
{01, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 01, 89, 19, 67, 48}};
|
||||
|
||||
//Constructor
|
||||
Problem11::Problem11() : Problem("What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20x20 grid?"){
|
||||
}
|
||||
|
||||
//Solve the problem
|
||||
void Problem11::solve(){
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
@@ -175,6 +178,13 @@ void Problem11::solve(){
|
||||
solved = true;
|
||||
}
|
||||
|
||||
//Reset the problem so it can be run again
|
||||
void Problem11::reset(){
|
||||
Problem::reset();
|
||||
greatestProduct.clear();
|
||||
}
|
||||
|
||||
//Return a string with the solution to the problem
|
||||
std::string Problem11::getString() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -186,6 +196,7 @@ std::string Problem11::getString() const{
|
||||
return results.str();
|
||||
}
|
||||
|
||||
//Returns the numbers that were being searched
|
||||
std::vector<int> Problem11::getNumbers() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -194,6 +205,7 @@ std::vector<int> Problem11::getNumbers() const{
|
||||
return greatestProduct;
|
||||
}
|
||||
|
||||
//Returns the product that was requested
|
||||
int Problem11::getProduct() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -201,8 +213,3 @@ int Problem11::getProduct() const{
|
||||
}
|
||||
return mee::getProduct(greatestProduct);
|
||||
}
|
||||
|
||||
void Problem11::reset(){
|
||||
Problem::reset();
|
||||
greatestProduct.clear();
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
//ProjectEuler/C++/Source/Problem12.cpp
|
||||
//ProjectEuler/ProjectEulerCPP/Source/Problem12.cpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-27-18
|
||||
//Modified: 07-14-19
|
||||
//Modified: 07-09-20
|
||||
//What is the value of the first triangle number to have over five hundred divisors?
|
||||
//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
|
||||
@@ -30,11 +30,14 @@
|
||||
#include "../Headers/Problem12.hpp"
|
||||
|
||||
|
||||
//The number of divisors that you want
|
||||
uint64_t Problem12::GOAL_DIVISORS = 500;
|
||||
|
||||
//Constructor
|
||||
Problem12::Problem12() : Problem("What is the value of the first triangle number to have over five hundred divisors?"), sum(1), counter(2){
|
||||
}
|
||||
|
||||
//Solve the problem
|
||||
void Problem12::solve(){
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
@@ -67,6 +70,15 @@ void Problem12::solve(){
|
||||
solved = true;
|
||||
}
|
||||
|
||||
//Reset the problem so it can be run again
|
||||
void Problem12::reset(){
|
||||
Problem::reset();
|
||||
divisors.clear();
|
||||
sum = 1;
|
||||
counter = 2;
|
||||
}
|
||||
|
||||
//Return a string with the solution to the problem
|
||||
std::string Problem12::getString() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -77,6 +89,7 @@ std::string Problem12::getString() const{
|
||||
return results.str();
|
||||
}
|
||||
|
||||
//Returns the triangular number
|
||||
int64_t Problem12::getTriangularNumber() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -85,6 +98,7 @@ int64_t Problem12::getTriangularNumber() const{
|
||||
return sum;
|
||||
}
|
||||
|
||||
//Get the final number that was added to the triangular number
|
||||
int64_t Problem12::getLastNumberAdded() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -93,6 +107,7 @@ int64_t Problem12::getLastNumberAdded() const{
|
||||
return counter - 1;
|
||||
}
|
||||
|
||||
//Returns the list of divisors of the requested number
|
||||
std::vector<int64_t> Problem12::getDivisorsOfTriangularNumber() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -101,6 +116,7 @@ std::vector<int64_t> Problem12::getDivisorsOfTriangularNumber() const{
|
||||
return divisors;
|
||||
}
|
||||
|
||||
//Returns the number of divisors of the requested number
|
||||
size_t Problem12::getNumberOfDivisors() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -108,10 +124,3 @@ size_t Problem12::getNumberOfDivisors() const{
|
||||
}
|
||||
return divisors.size();
|
||||
}
|
||||
|
||||
void Problem12::reset(){
|
||||
Problem::reset();
|
||||
divisors.clear();
|
||||
sum = 1;
|
||||
counter = 2;
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
//ProjectEuler/C++/Source/Problem15.hpp
|
||||
//ProjectEuler/ProjectEulerCPP/Source/Problem15.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-29-18
|
||||
//Modified: 07-14-19
|
||||
//Modified: 07-09-20
|
||||
//How many routes from the top left corner to the bottom right corner are there through a 20×20 grid if you can only move right and down?
|
||||
//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
|
||||
@@ -30,9 +30,11 @@
|
||||
#include "../Headers/Problem15.hpp"
|
||||
|
||||
|
||||
int Problem15::WIDTH = 20;
|
||||
int Problem15::LENGTH = 20;
|
||||
int Problem15::WIDTH = 20; //The width of the grid
|
||||
int Problem15::LENGTH = 20; //The length of the grid
|
||||
|
||||
//This function acts as a handler for moving the position on the grid and counting the distance
|
||||
//It moves right first, then down
|
||||
void Problem15::move(int currentX, int currentY, uint64_t& numOfRoutes){
|
||||
//Check if you are at the end
|
||||
if((currentX == WIDTH) && (currentY == LENGTH)){
|
||||
@@ -51,9 +53,11 @@ void Problem15::move(int currentX, int currentY, uint64_t& numOfRoutes){
|
||||
}
|
||||
}
|
||||
|
||||
//Constructor
|
||||
Problem15::Problem15() : Problem("How many routes from the top left corner to the bottom right corner are there through a 20×20 grid if you can only move right and down?"), numOfRoutes(0){
|
||||
}
|
||||
|
||||
//Solve the problem
|
||||
void Problem15::solve(){
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
@@ -78,6 +82,13 @@ void Problem15::solve(){
|
||||
solved = true;
|
||||
}
|
||||
|
||||
//Reset the problem so it can be run again
|
||||
void Problem15::reset(){
|
||||
Problem::reset();
|
||||
numOfRoutes = 0;
|
||||
}
|
||||
|
||||
//Return a string with the solution to the problem
|
||||
std::string Problem15::getString() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -89,6 +100,7 @@ std::string Problem15::getString() const{
|
||||
return results.str();
|
||||
}
|
||||
|
||||
//Returns the number of routes found
|
||||
uint64_t Problem15::getNumberOfRoutes() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -96,8 +108,3 @@ uint64_t Problem15::getNumberOfRoutes() const{
|
||||
}
|
||||
return numOfRoutes;
|
||||
}
|
||||
|
||||
void Problem15::reset(){
|
||||
Problem::reset();
|
||||
numOfRoutes = 0;
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
//ProjectEuler/C++/Source/Problem16.cpp
|
||||
//ProjectEuler/ProjectEulerCPP/Source/Problem16.cpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-28-18
|
||||
//Modified: 07-14-19
|
||||
//Modified: 07-09-20
|
||||
//What is the sum of the digits of the number 2^1000?
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
|
||||
//This file contains a header from the gmp library. The library is used for large integers.
|
||||
//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
|
||||
@@ -35,9 +35,11 @@
|
||||
int Problem16::NUM_TO_POWER = 2; //The number that is going to be raised to a power
|
||||
int Problem16::POWER = 1000; //The power that the number is going to be raised to
|
||||
|
||||
//Constructor
|
||||
Problem16::Problem16() : Problem("What is the sum of the digits of the number 2^1000?"), num(0), sumOfElements(0){
|
||||
}
|
||||
|
||||
//Solve the problem
|
||||
void Problem16::solve(){
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
@@ -67,6 +69,15 @@ void Problem16::solve(){
|
||||
solved = true;
|
||||
}
|
||||
|
||||
//Reset the problem so it can be run again
|
||||
void Problem16::reset(){
|
||||
Problem::reset();
|
||||
num = 0;
|
||||
sumOfElements = 0;
|
||||
}
|
||||
|
||||
|
||||
//Return a string with the solution to the problem
|
||||
std::string Problem16::getString() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -79,6 +90,7 @@ std::string Problem16::getString() const{
|
||||
return results.str();
|
||||
}
|
||||
|
||||
//Returns the number that was calculated
|
||||
mpz_class Problem16::getNumber() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -87,6 +99,7 @@ mpz_class Problem16::getNumber() const{
|
||||
return num;
|
||||
}
|
||||
|
||||
//Return the sum of the digits of the number
|
||||
int Problem16::getSum() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -94,9 +107,3 @@ int Problem16::getSum() const{
|
||||
}
|
||||
return sumOfElements;
|
||||
}
|
||||
|
||||
void Problem16::reset(){
|
||||
Problem::reset();
|
||||
num = 0;
|
||||
sumOfElements = 0;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
//ProjectEuler/C++/Source/Problem17.cpp
|
||||
//ProjectEuler/ProjectEulerCPP/Source/Problem17.cpp
|
||||
//Matthew Ellison
|
||||
// Created: 10-05-18
|
||||
//Modified: 07-14-19
|
||||
//Modified: 07-09-20
|
||||
//If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
|
||||
//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
|
||||
@@ -30,11 +30,10 @@
|
||||
#include "../Headers/Problem17.hpp"
|
||||
|
||||
|
||||
//This is the largest number to get the words of
|
||||
int Problem17::TOP_NUM = 1000;
|
||||
|
||||
Problem17::Problem17() : Problem("If all the numbers from 1 to 1000 inclusive were written out in words, how many letters would be used?"), letterCount(0){
|
||||
}
|
||||
|
||||
//This function makes a word out of the number passed into it
|
||||
std::string Problem17::makeWord(int num){
|
||||
int currentDivider = 1000;
|
||||
int currentNum;
|
||||
@@ -142,6 +141,7 @@ std::string Problem17::makeWord(int num){
|
||||
return word;
|
||||
}
|
||||
|
||||
//This function helps makeWord() by returning the words for the numbers 1-9
|
||||
std::string Problem17::wordHelper(int num){
|
||||
std::string tempString;
|
||||
switch(num){
|
||||
@@ -159,6 +159,7 @@ std::string Problem17::wordHelper(int num){
|
||||
return tempString;
|
||||
}
|
||||
|
||||
//This counts the number of letters in the string that is passed in (ignoring numbers and punctuation)
|
||||
uint64_t Problem17::countLetters(std::string str){
|
||||
uint64_t letterCount = 0;
|
||||
//Step through every character in the string and count how many letters there are
|
||||
@@ -170,6 +171,11 @@ uint64_t Problem17::countLetters(std::string str){
|
||||
return letterCount;
|
||||
}
|
||||
|
||||
//Constructor
|
||||
Problem17::Problem17() : Problem("If all the numbers from 1 to 1000 inclusive were written out in words, how many letters would be used?"), letterCount(0){
|
||||
}
|
||||
|
||||
//Solve the problem
|
||||
void Problem17::solve(){
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
@@ -192,6 +198,13 @@ void Problem17::solve(){
|
||||
solved = true;
|
||||
}
|
||||
|
||||
//Reset the problem so it can be run again
|
||||
void Problem17::reset(){
|
||||
Problem::reset();
|
||||
letterCount = 0;
|
||||
}
|
||||
|
||||
//Return a string with the solution to the problem
|
||||
std::string Problem17::getString() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -202,6 +215,7 @@ std::string Problem17::getString() const{
|
||||
return results.str();
|
||||
}
|
||||
|
||||
//Returns the number of letters asked for
|
||||
uint64_t Problem17::getLetterCount() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -209,8 +223,3 @@ uint64_t Problem17::getLetterCount() const{
|
||||
}
|
||||
return letterCount;
|
||||
}
|
||||
|
||||
void Problem17::reset(){
|
||||
Problem::reset();
|
||||
letterCount = 0;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/C++/Source/Problem18.hpp
|
||||
//ProjectEuler/ProjectEulerCPP/Source/Problem18.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 11-01-18
|
||||
//Modified: 07-14-19
|
||||
//Modified: 07-09-20
|
||||
//Find the maximum total from top to bottom
|
||||
/*
|
||||
75
|
||||
@@ -23,7 +23,7 @@
|
||||
//This is done using a breadth first search
|
||||
//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
|
||||
@@ -48,6 +48,7 @@
|
||||
#include "../Headers/Problem18.hpp"
|
||||
|
||||
|
||||
//Setup the list you are trying to find a path through
|
||||
std::vector<int> Problem18::list[NUM_ROWS] =
|
||||
{{75},
|
||||
{95, 64},
|
||||
@@ -65,9 +66,7 @@ std::vector<int> Problem18::list[NUM_ROWS] =
|
||||
{63, 66, 04, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31},
|
||||
{04, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 04, 23}};
|
||||
|
||||
Problem18::Problem18() : Problem("Find the maximum total from the top to the bottom of the pyramid."), actualTotal(0){
|
||||
}
|
||||
|
||||
//This list turns every number in the vector into 100 - num
|
||||
void Problem18::invert(){
|
||||
for(unsigned int rowCnt = 0;rowCnt < NUM_ROWS;++rowCnt){
|
||||
for(unsigned int colCnt = 0;colCnt < list[rowCnt].size();++colCnt){
|
||||
@@ -76,6 +75,11 @@ void Problem18::invert(){
|
||||
}
|
||||
}
|
||||
|
||||
//Constructor
|
||||
Problem18::Problem18() : Problem("Find the maximum total from the top to the bottom of the pyramid."), actualTotal(0){
|
||||
}
|
||||
|
||||
//Solve the problem
|
||||
void Problem18::solve(){
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
@@ -135,6 +139,15 @@ void Problem18::solve(){
|
||||
solved = true;
|
||||
}
|
||||
|
||||
//Reset the problem so it can be run again
|
||||
void Problem18::reset(){
|
||||
Problem::reset();
|
||||
foundPoints.clear();
|
||||
possiblePoints.clear();
|
||||
actualTotal = 0;
|
||||
}
|
||||
|
||||
//Return a string with the solution to the problem
|
||||
std::string Problem18::getString() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -146,6 +159,7 @@ std::string Problem18::getString() const{
|
||||
return results.str();
|
||||
}
|
||||
|
||||
//Returns the pyramid that was traversed as a string
|
||||
std::string Problem18::getPyramid(){
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -162,6 +176,7 @@ std::string Problem18::getPyramid(){
|
||||
return results.str();
|
||||
}
|
||||
|
||||
//Returns the trail the algorithm took as a string
|
||||
std::string Problem18::getTrail(){
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -218,6 +233,7 @@ std::string Problem18::getTrail(){
|
||||
return results.str();
|
||||
}
|
||||
|
||||
//Returns the total that was asked for
|
||||
int Problem18::getTotal() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -225,10 +241,3 @@ int Problem18::getTotal() const{
|
||||
}
|
||||
return actualTotal;
|
||||
}
|
||||
|
||||
void Problem18::reset(){
|
||||
Problem::reset();
|
||||
foundPoints.clear();
|
||||
possiblePoints.clear();
|
||||
actualTotal = 0;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/C++/Source/Problem19.cpp
|
||||
//ProjectEuler/ProjectEulerCPP/Source/Problem19.cpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-28-18
|
||||
//Modified: 07-14-19
|
||||
//Modified: 07-09-20
|
||||
//How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
|
||||
/*
|
||||
You are given the following information, but you may prefer to do some research for yourself.
|
||||
@@ -16,7 +16,7 @@ A leap year occurs on any year evenly divisible by 4, but not on a century unles
|
||||
*/
|
||||
//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
|
||||
@@ -43,9 +43,7 @@ A leap year occurs on any year evenly divisible by 4, but not on a century unles
|
||||
unsigned int Problem19::START_YEAR = 1901; //The start year
|
||||
unsigned int Problem19::END_YEAR = 2000; //The stop year
|
||||
|
||||
Problem19::Problem19() : Problem("How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?"), totalSundays(0){
|
||||
}
|
||||
|
||||
//Return the day of the week that the date you pass into it is on
|
||||
Problem19::DAYS Problem19::getDay(unsigned int month, unsigned int day, unsigned int year){
|
||||
//Make sure the numbers are within propper bounds
|
||||
if((month < 1) || (month > 12) || (day < 1) || (day > 31) || (year < 1)){
|
||||
@@ -122,6 +120,7 @@ Problem19::DAYS Problem19::getDay(unsigned int month, unsigned int day, unsigned
|
||||
}
|
||||
}
|
||||
|
||||
//Returns true if the year passed to it is a leap year
|
||||
bool Problem19::isLeapYear(unsigned int year){
|
||||
if(year < 1){
|
||||
return false;
|
||||
@@ -141,6 +140,11 @@ bool Problem19::isLeapYear(unsigned int year){
|
||||
return false;
|
||||
}
|
||||
|
||||
//Constructor
|
||||
Problem19::Problem19() : Problem("How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?"), totalSundays(0){
|
||||
}
|
||||
|
||||
//Solve the problem
|
||||
void Problem19::solve(){
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
@@ -171,6 +175,13 @@ void Problem19::solve(){
|
||||
solved = true;
|
||||
}
|
||||
|
||||
//Reset the problem so it can be run again
|
||||
void Problem19::reset(){
|
||||
Problem::reset();
|
||||
totalSundays = 0;
|
||||
}
|
||||
|
||||
//Return a string with the solution to the problem
|
||||
std::string Problem19::getString() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -181,6 +192,7 @@ std::string Problem19::getString() const{
|
||||
return results.str();
|
||||
}
|
||||
|
||||
//Returns the total sundays that were asked for
|
||||
uint64_t Problem19::getTotalSundays() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -188,8 +200,3 @@ uint64_t Problem19::getTotalSundays() const{
|
||||
}
|
||||
return totalSundays;
|
||||
}
|
||||
|
||||
void Problem19::reset(){
|
||||
Problem::reset();
|
||||
totalSundays = 0;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
//ProjectEuler/C++/Source/Problem2.cpp
|
||||
//ProjectEuler/ProjectEulerCPP/Source/Problem2.cpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-28-18
|
||||
//Modified: 07-14-19
|
||||
//Modified: 07-09-20
|
||||
//The sum of the even Fibonacci numbers less than 4,000,000
|
||||
//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
|
||||
@@ -31,11 +31,14 @@
|
||||
#include "../Headers/Problem2.hpp"
|
||||
|
||||
|
||||
//Holds the largest number that we are looking for
|
||||
uint64_t Problem2::TOP_NUM = 4000000;
|
||||
|
||||
//Constructor
|
||||
Problem2::Problem2() : Problem("What is the sum of the even Fibonacci numbers less than 4,000,000?"), fullSum(0){
|
||||
}
|
||||
|
||||
//Solve the problem
|
||||
void Problem2::solve(){
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
@@ -62,6 +65,13 @@ void Problem2::solve(){
|
||||
solved = true;
|
||||
}
|
||||
|
||||
//Reset the problem so it can be run again
|
||||
void Problem2::reset(){
|
||||
Problem::reset();
|
||||
fullSum = 0;
|
||||
}
|
||||
|
||||
//Return a string with the solution to the problem
|
||||
std::string Problem2::getString() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -72,6 +82,7 @@ std::string Problem2::getString() const{
|
||||
return results.str();
|
||||
}
|
||||
|
||||
//Returns the requested sum
|
||||
uint64_t Problem2::getSum() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -79,8 +90,3 @@ uint64_t Problem2::getSum() const{
|
||||
}
|
||||
return fullSum;
|
||||
}
|
||||
|
||||
void Problem2::reset(){
|
||||
Problem::reset();
|
||||
fullSum = 0;
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
//ProjectEuler/C++/Source/Problem20.cpp
|
||||
//ProjectEuler/ProjectEulerCPP/Source/Problem20.cpp
|
||||
//Matthew Ellison
|
||||
// Created: 11-07-18
|
||||
//Modified: 07-14-19
|
||||
//Modified: 07-09-20
|
||||
//What is the sum of the digits of 100!?
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
|
||||
//This file contains a header from the gmp library. The library is used for large integers.
|
||||
//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
|
||||
@@ -33,11 +33,13 @@
|
||||
#include "../Headers/Problem20.hpp"
|
||||
|
||||
|
||||
//Constructor
|
||||
Problem20::Problem20() : Problem("What is the sum of the digits of 100!?"), num(1), sum(0){
|
||||
//num Starts at 1 because multiplication is performed on it
|
||||
//sum starts at 0 because addition is performed on it
|
||||
}
|
||||
|
||||
//Solve the problem
|
||||
void Problem20::solve(){
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
@@ -66,6 +68,14 @@ void Problem20::solve(){
|
||||
solved = true;
|
||||
}
|
||||
|
||||
//Reset the problem so it can be run again
|
||||
void Problem20::reset(){
|
||||
Problem::reset();
|
||||
sum = 0;
|
||||
num = 1;
|
||||
}
|
||||
|
||||
//Return a string with the solution to the problem
|
||||
std::string Problem20::getString() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -78,6 +88,7 @@ std::string Problem20::getString() const{
|
||||
return results.str();
|
||||
}
|
||||
|
||||
//Returns the number 100!
|
||||
mpz_class Problem20::getNumber() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -86,6 +97,7 @@ mpz_class Problem20::getNumber() const{
|
||||
return num;
|
||||
}
|
||||
|
||||
//Returns the number 100! in a string
|
||||
std::string Problem20::getNumberString() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -94,6 +106,7 @@ std::string Problem20::getNumberString() const{
|
||||
return num.get_str();
|
||||
}
|
||||
|
||||
//Returns the sum of the digits of 100!
|
||||
uint64_t Problem20::getSum() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -101,9 +114,3 @@ uint64_t Problem20::getSum() const{
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
void Problem20::reset(){
|
||||
Problem::reset();
|
||||
sum = 0;
|
||||
num = 1;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
//ProjectEuler/C++/Source/Problem21.cpp
|
||||
//ProjectEuler/ProjectEulerCPP/Source/Problem21.cpp
|
||||
//Matthew Ellison
|
||||
// Created: 11-08-18
|
||||
//Modified: 07-14-19
|
||||
//Modified: 07-09-20
|
||||
//Evaluate the sum of all the amicable numbers under 10000
|
||||
//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
|
||||
@@ -31,17 +31,21 @@
|
||||
#include "../Headers/Problem21.hpp"
|
||||
|
||||
|
||||
//The top number that will be evaluated
|
||||
int Problem21::LIMIT = 10000; //The top number that will be evaluated
|
||||
|
||||
Problem21::Problem21() : Problem("Evaluate the sum of all the amicable numbers under 10000"){
|
||||
reserveVectors();
|
||||
}
|
||||
|
||||
//Reserve the size of the vector to speed up insertion
|
||||
void Problem21::reserveVectors(){
|
||||
divisorSum.reserve(LIMIT); //Reserving it now makes it faster later
|
||||
divisorSum.resize(LIMIT); //Make sure there are enough spaces
|
||||
}
|
||||
|
||||
//Constructor
|
||||
Problem21::Problem21() : Problem("Evaluate the sum of all the amicable numbers under 10000"){
|
||||
reserveVectors();
|
||||
}
|
||||
|
||||
//Solve the problem
|
||||
void Problem21::solve(){
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
@@ -87,6 +91,15 @@ void Problem21::solve(){
|
||||
solved = true;
|
||||
}
|
||||
|
||||
//Reset the problem so it can be run again
|
||||
void Problem21::reset(){
|
||||
Problem::reset();
|
||||
divisorSum.clear();
|
||||
amicable.clear();
|
||||
reserveVectors();
|
||||
}
|
||||
|
||||
//Return a string with the solution to the problem
|
||||
std::string Problem21::getString() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -102,6 +115,7 @@ std::string Problem21::getString() const{
|
||||
return results.str();
|
||||
}
|
||||
|
||||
//Returns a vector with all of the amicable numbers calculated
|
||||
std::vector<uint64_t> Problem21::getAmicable() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -110,6 +124,7 @@ std::vector<uint64_t> Problem21::getAmicable() const{
|
||||
return amicable;
|
||||
}
|
||||
|
||||
//Returns the sum of all of the amicable numbers
|
||||
uint64_t Problem21::getSum() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -117,10 +132,3 @@ uint64_t Problem21::getSum() const{
|
||||
}
|
||||
return mee::getSum(amicable);
|
||||
}
|
||||
|
||||
void Problem21::reset(){
|
||||
Problem::reset();
|
||||
divisorSum.clear();
|
||||
amicable.clear();
|
||||
reserveVectors();
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
//ProjectEuler/C++/Source/Problem22.cpp
|
||||
//ProjectEuler/ProjectEulerCPP/Source/Problem22.cpp
|
||||
//Matthew Ellison
|
||||
// Created: 11-09-18
|
||||
//Modified: 07-14-19
|
||||
//Modified: 07-09-20
|
||||
//What is the total of all the name scores in the file?
|
||||
//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,6 +32,7 @@
|
||||
#include "../Headers/Problem22.hpp"
|
||||
|
||||
|
||||
//Holds the names that will be scored
|
||||
std::vector<std::string> Problem22::names = { "MARY","PATRICIA","LINDA","BARBARA","ELIZABETH","JENNIFER","MARIA","SUSAN","MARGARET","DOROTHY","LISA","NANCY","KAREN",
|
||||
"BETTY","HELEN","SANDRA","DONNA","CAROL","RUTH","SHARON","MICHELLE","LAURA","SARAH","KIMBERLY","DEBORAH","JESSICA","SHIRLEY",
|
||||
"CYNTHIA","ANGELA","MELISSA","BRENDA","AMY","ANNA","REBECCA","VIRGINIA","KATHLEEN","PAMELA","MARTHA","DEBRA","AMANDA","STEPHANIE",
|
||||
@@ -401,10 +402,7 @@ std::vector<std::string> Problem22::names = { "MARY","PATRICIA","LINDA","BARBARA
|
||||
"KENETH","JACINTO","GRAIG","FRANKLYN","EDMUNDO","SID","PORTER","LEIF","JERAMY","BUCK","WILLIAN","VINCENZO","SHON","LYNWOOD","JERE",
|
||||
"HAI","ELDEN","DORSEY","DARELL","BRODERICK","ALONSO"};
|
||||
|
||||
Problem22::Problem22() : Problem("What is the total of all the name scores in the file?"){
|
||||
reserveVectors();
|
||||
}
|
||||
|
||||
//Reserve the size of the vector to speed up insertion
|
||||
void Problem22::reserveVectors(){
|
||||
//Make sure the vector is the right size
|
||||
sums.reserve(names.size());
|
||||
@@ -414,6 +412,12 @@ void Problem22::reserveVectors(){
|
||||
prod.resize(names.size());
|
||||
}
|
||||
|
||||
//Constructor
|
||||
Problem22::Problem22() : Problem("What is the total of all the name scores in the file?"){
|
||||
reserveVectors();
|
||||
}
|
||||
|
||||
//Solve the problem
|
||||
void Problem22::solve(){
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
@@ -445,6 +449,15 @@ void Problem22::solve(){
|
||||
solved = true;
|
||||
}
|
||||
|
||||
//Reset the problem so it can be run again
|
||||
void Problem22::reset(){
|
||||
Problem::reset();
|
||||
sums.clear();
|
||||
prod.clear();
|
||||
reserveVectors();
|
||||
}
|
||||
|
||||
//Return a string with the solution to the problem
|
||||
std::string Problem22::getString() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -472,10 +485,3 @@ uint64_t Problem22::getNameScoreSum() const{
|
||||
}
|
||||
return mee::getSum(prod);
|
||||
}
|
||||
|
||||
void Problem22::reset(){
|
||||
Problem::reset();
|
||||
sums.clear();
|
||||
prod.clear();
|
||||
reserveVectors();
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
//ProjectEuler/C++/Source/Problem24.cpp
|
||||
//ProjectEuler/ProjectEulerCPP/Source/Problem24.cpp
|
||||
//Matthew Ellison
|
||||
// Created: 11-11-18
|
||||
//Modified: 07-14-19
|
||||
//Modified: 07-09-20
|
||||
//What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?
|
||||
//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
|
||||
@@ -30,13 +30,16 @@
|
||||
#include "../Headers/Problem24.hpp"
|
||||
|
||||
|
||||
int Problem24::NEEDED_PERM = 1000000; //The number of the permutation that you need
|
||||
std::string Problem24::nums = "0123456789"; //All of the characters that we need to get the permutations of
|
||||
//The number of the permutation that you need
|
||||
int Problem24::NEEDED_PERM = 1000000;
|
||||
//All of the characters that we need to get the permutations of
|
||||
std::string Problem24::nums = "0123456789";
|
||||
|
||||
//Constructor
|
||||
Problem24::Problem24() : Problem("What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?"){
|
||||
|
||||
}
|
||||
|
||||
//Solve the problem
|
||||
void Problem24::solve(){
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
@@ -56,6 +59,13 @@ void Problem24::solve(){
|
||||
solved = true;
|
||||
}
|
||||
|
||||
//Reset the problem so it can be run again
|
||||
void Problem24::reset(){
|
||||
Problem::reset();
|
||||
permutations.clear();
|
||||
}
|
||||
|
||||
//Return a string with the solution to the problem
|
||||
std::string Problem24::getString() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -84,8 +94,3 @@ std::string Problem24::getPermutation() const{
|
||||
}
|
||||
return permutations.at(NEEDED_PERM - 1);
|
||||
}
|
||||
|
||||
void Problem24::reset(){
|
||||
Problem::reset();
|
||||
permutations.clear();
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
//ProjectEuler/C++/Source/Problem25.cpp
|
||||
//ProjectEuler/ProjectEulerCPP/Source/Problem25.cpp
|
||||
//Matthew Ellison
|
||||
// Created: 11-13-18
|
||||
//Modified: 07-14-19
|
||||
//Modified: 07-09-20
|
||||
//What is the index of the first term in the Fibonacci sequence to contain 1000 digits?
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
|
||||
//This file contains a header from the gmp library. The library is used for large integers.
|
||||
//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
|
||||
@@ -33,13 +33,16 @@
|
||||
#include "../Headers/Problem25.hpp"
|
||||
|
||||
|
||||
//The number of digits to calculate up to
|
||||
unsigned int Problem25::NUM_DIGITS = 1000; //The number of digits to calculate up to
|
||||
|
||||
//Constructor
|
||||
Problem25::Problem25() : Problem("What is the index of the first term in the Fibonacci sequence to contain 1000 digits?"), number(0), index(2){
|
||||
number = 0;
|
||||
index = 0;
|
||||
}
|
||||
|
||||
//Solve the problem
|
||||
void Problem25::solve(){
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
@@ -62,6 +65,14 @@ void Problem25::solve(){
|
||||
solved = true;
|
||||
}
|
||||
|
||||
//Reset the problem so it can be run again
|
||||
void Problem25::reset(){
|
||||
Problem::reset();
|
||||
number = 0;
|
||||
index = 2;
|
||||
}
|
||||
|
||||
//Return a string with the solution to the problem
|
||||
std::string Problem25::getString() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -118,9 +129,3 @@ uint64_t Problem25::getIndexInt() const{
|
||||
}
|
||||
return index.get_ui();
|
||||
}
|
||||
|
||||
void Problem25::reset(){
|
||||
Problem::reset();
|
||||
number = 0;
|
||||
index = 2;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
//ProjectEuler/C++/Headers/Problem26.cpp
|
||||
//ProjectEuler/ProjectEulerCPP/Source/Problem26.cpp
|
||||
//Matthew Ellison
|
||||
// Created: 07-28-19
|
||||
//Modified: 07-28-19
|
||||
//Modified: 07-09-20
|
||||
//Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.
|
||||
//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
|
||||
@@ -29,13 +29,16 @@
|
||||
#include "../Headers/Problem26.hpp"
|
||||
|
||||
|
||||
//Holds the highest denominator we will check
|
||||
unsigned int Problem26::TOP_NUMBER = 999;
|
||||
|
||||
//Constructor
|
||||
Problem26::Problem26() : Problem("Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part."), longestCycle(0), longestNumber(1){
|
||||
longestCycle = 0;
|
||||
longestNumber = 0;
|
||||
}
|
||||
|
||||
//Solve the problem
|
||||
void Problem26::solve(){
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
@@ -90,6 +93,14 @@ void Problem26::solve(){
|
||||
solved = true;
|
||||
}
|
||||
|
||||
//Reset the problem so it can be run again
|
||||
void Problem26::reset(){
|
||||
Problem::reset();
|
||||
longestCycle = 0;
|
||||
longestNumber = 1;
|
||||
}
|
||||
|
||||
//Return a string with the solution to the problem
|
||||
std::string Problem26::getString() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -103,6 +114,7 @@ std::string Problem26::getString() const{
|
||||
return results.str();
|
||||
}
|
||||
|
||||
//Returns the length of the longest cycle
|
||||
unsigned int Problem26::getLongestCycle() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -111,6 +123,7 @@ unsigned int Problem26::getLongestCycle() const{
|
||||
return longestCycle;
|
||||
}
|
||||
|
||||
//Returns the denominator that starts the longest cycle
|
||||
unsigned int Problem26::getLongestNumber() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -118,9 +131,3 @@ unsigned int Problem26::getLongestNumber() const{
|
||||
}
|
||||
return longestNumber;
|
||||
}
|
||||
|
||||
void Problem26::reset(){
|
||||
Problem::reset();
|
||||
longestCycle = 0;
|
||||
longestNumber = 1;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
//ProjectEuler/C++/Source/Problem27.cpp
|
||||
//ProjectEuler/ProjectEulerCPP/Source/Problem27.cpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-14-19
|
||||
//Modified: 09-14-19
|
||||
//Modified: 07-09-20
|
||||
//Find the product of the coefficients, |a| < 1000 and |b| <= 1000, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n=0.
|
||||
//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
|
||||
@@ -28,10 +28,12 @@
|
||||
#include "../Headers/Problem27.hpp"
|
||||
|
||||
|
||||
//Constructor
|
||||
Problem27::Problem27() : Problem("Considering quadratics of the form n^2 + an + b, where |a| < 1000 and |b| <= 1000, find the product of the coefficients a and b that produce the maximum number of primes for consecutive values of n starting with n = 0."){
|
||||
topA = topB = topN = 0;
|
||||
}
|
||||
|
||||
//Solve the problem
|
||||
void Problem27::solve(){
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
@@ -72,6 +74,14 @@ void Problem27::solve(){
|
||||
solved = true;
|
||||
}
|
||||
|
||||
//Reset the problem so it can be run again
|
||||
void Problem27::reset(){
|
||||
Problem::reset();
|
||||
topA = topB = topN = 0;
|
||||
primes.clear();
|
||||
}
|
||||
|
||||
//Return a string with the solution to the problem
|
||||
std::string Problem27::getString() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -86,6 +96,7 @@ std::string Problem27::getString() const{
|
||||
return results.str();
|
||||
}
|
||||
|
||||
//Returns the top A that was generated
|
||||
int64_t Problem27::getTopA() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -94,6 +105,7 @@ int64_t Problem27::getTopA() const{
|
||||
return topA;
|
||||
}
|
||||
|
||||
//Returns the top B that was generated
|
||||
int64_t Problem27::getTopB() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -102,6 +114,7 @@ int64_t Problem27::getTopB() const{
|
||||
return topB;
|
||||
}
|
||||
|
||||
//Returns the top N that was generated
|
||||
int64_t Problem27::getTopN() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -109,9 +122,3 @@ int64_t Problem27::getTopN() const{
|
||||
}
|
||||
return topN;
|
||||
}
|
||||
|
||||
void Problem27::reset(){
|
||||
Problem::reset();
|
||||
topA = topB = topN = 0;
|
||||
primes.clear();
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
//ProjectEuler/C++/Source/Problem28.cpp
|
||||
//ProjectEuler/ProjectEulerCPP/Source/Problem28.cpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-21-19
|
||||
//Modified: 09-21-19
|
||||
//Modified: 07-09-20
|
||||
//What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed by starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral
|
||||
//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
|
||||
@@ -29,10 +29,7 @@
|
||||
#include "../Headers/Problem28.hpp"
|
||||
|
||||
|
||||
Problem28::Problem28() : Problem("What is the sum of the number on the diagonals in a 1001 x 1001 spiral formed by starting with the number 1 and moving to the right in a clockwise direction?"){
|
||||
setupGrid();
|
||||
sumOfDiagonals = 0;
|
||||
}
|
||||
//This sets up the grid to hold the correct number of variables
|
||||
void Problem28::setupGrid(){
|
||||
//Set the size of the grid to 1001 x 1001
|
||||
for(int cnt = 0;cnt < 1001;++cnt){
|
||||
@@ -43,7 +40,7 @@ void Problem28::setupGrid(){
|
||||
}
|
||||
}
|
||||
|
||||
//Sets up the grid
|
||||
//Puts all of the numbers in the grid up the grid
|
||||
void Problem28::createGrid(){
|
||||
bool finalLocation = false; //A flag to indicate if the final location to be filled has been reached
|
||||
//Set the number that is going to be put at each location
|
||||
@@ -109,6 +106,13 @@ void Problem28::findSum(){
|
||||
}
|
||||
}
|
||||
|
||||
//Constructor
|
||||
Problem28::Problem28() : Problem("What is the sum of the number on the diagonals in a 1001 x 1001 spiral formed by starting with the number 1 and moving to the right in a clockwise direction?"){
|
||||
setupGrid();
|
||||
sumOfDiagonals = 0;
|
||||
}
|
||||
|
||||
//Solve the problem
|
||||
void Problem28::solve(){
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
@@ -132,6 +136,15 @@ void Problem28::solve(){
|
||||
solved = true;
|
||||
}
|
||||
|
||||
//Reset the problem so it can be run again
|
||||
void Problem28::reset(){
|
||||
Problem::reset();
|
||||
sumOfDiagonals = 0;
|
||||
grid.clear();
|
||||
setupGrid();
|
||||
}
|
||||
|
||||
//Return a string with the solution to the problem
|
||||
std::string Problem28::getString() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -161,10 +174,3 @@ uint64_t Problem28::getSum() const{
|
||||
}
|
||||
return sumOfDiagonals;
|
||||
}
|
||||
|
||||
void Problem28::reset(){
|
||||
Problem::reset();
|
||||
sumOfDiagonals = 0;
|
||||
grid.clear();
|
||||
setupGrid();
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
//ProjectEuler/C++/Source/Problem29.cpp
|
||||
//ProjectEuler/ProjectEulerCPP/Source/Problem29.cpp
|
||||
//Matthew Ellison
|
||||
// Created: 10-06-19
|
||||
//Modified: 10-06-19
|
||||
//Modified: 07-09-20
|
||||
//How many distinct terms are in the sequence generated by a^b for 2 <= a <= 100 and 2 <= b <= 100?
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
|
||||
//This file contains a header from the gmp library. The library is used for large integers.
|
||||
//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
|
||||
@@ -34,9 +34,20 @@
|
||||
#include "Algorithms.hpp"
|
||||
|
||||
|
||||
//The lowest possible value for a
|
||||
unsigned int Problem29::BOTTOM_A = 2;
|
||||
//The highest possible value for a
|
||||
unsigned int Problem29::TOP_A = 100;
|
||||
//The lowest possible value for b
|
||||
unsigned int Problem29::BOTTOM_B = 2;
|
||||
//The highest possible value for b
|
||||
unsigned int Problem29::TOP_B = 100;
|
||||
|
||||
//Constructor
|
||||
Problem29::Problem29() : Problem("How many distict terms are in the sequence generated by a^b for 2 <= a <= 100 and 2 <= b <= 100?"){
|
||||
}
|
||||
|
||||
//Solve the problem
|
||||
void Problem29::solve(){
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
@@ -66,6 +77,13 @@ void Problem29::solve(){
|
||||
solved = true;
|
||||
}
|
||||
|
||||
//Reset the problem so it can be run again
|
||||
void Problem29::reset(){
|
||||
Problem::reset();
|
||||
unique.clear();
|
||||
}
|
||||
|
||||
//Return a string with the solution to the problem
|
||||
std::string Problem29::getString() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -78,6 +96,7 @@ std::string Problem29::getString() const{
|
||||
return results.str();
|
||||
}
|
||||
|
||||
//Returns the lowest possible value for a
|
||||
unsigned int Problem29::getBottomA() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -86,6 +105,7 @@ unsigned int Problem29::getBottomA() const{
|
||||
return BOTTOM_A;
|
||||
}
|
||||
|
||||
//Returns the highest possible value for a
|
||||
unsigned int Problem29::getTopA() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -94,6 +114,7 @@ unsigned int Problem29::getTopA() const{
|
||||
return TOP_A;
|
||||
}
|
||||
|
||||
//Returns the lowest possible value for b
|
||||
unsigned int Problem29::getBottomB() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -102,6 +123,7 @@ unsigned int Problem29::getBottomB() const{
|
||||
return BOTTOM_B;
|
||||
}
|
||||
|
||||
//Returns the highest possible value for b
|
||||
unsigned int Problem29::getTopB() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -110,6 +132,7 @@ unsigned int Problem29::getTopB() const{
|
||||
return TOP_B;
|
||||
}
|
||||
|
||||
//Returns a vector of all the unique values for a^b
|
||||
std::vector<mpz_class> Problem29::getUnique() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -117,8 +140,3 @@ std::vector<mpz_class> Problem29::getUnique() const{
|
||||
}
|
||||
return unique;
|
||||
}
|
||||
|
||||
void Problem29::reset(){
|
||||
Problem::reset();
|
||||
unique.clear();
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
//ProjectEuler/C++/Source/Problem3.cpp
|
||||
//ProjectEuler/ProjectEulerCPP/Source/Problem3.cpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-28-18
|
||||
//Modified: 07-14-19
|
||||
//Modified: 07-09-20
|
||||
//The largest prime factor of 600851475143
|
||||
//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,11 +32,14 @@
|
||||
#include "../Headers/Problem3.hpp"
|
||||
|
||||
|
||||
//The number of which you are trying to find the factors
|
||||
uint64_t Problem3::GOAL_NUMBER = 600851475143;
|
||||
|
||||
//Constructor
|
||||
Problem3::Problem3() : Problem("What is the largest prime factor of 600851475143?"){
|
||||
}
|
||||
|
||||
//Solve the problem
|
||||
void Problem3::solve(){
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
@@ -55,6 +58,13 @@ void Problem3::solve(){
|
||||
solved = true;
|
||||
}
|
||||
|
||||
//Reset the problem so it can be run again
|
||||
void Problem3::reset(){
|
||||
Problem::reset();
|
||||
factors.clear();
|
||||
}
|
||||
|
||||
//Return a string with the solution to the problem
|
||||
std::string Problem3::getString() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -65,6 +75,7 @@ std::string Problem3::getString() const{
|
||||
return results.str();
|
||||
}
|
||||
|
||||
//Returns the list of factors of the number
|
||||
std::vector<uint64_t> Problem3::getFactors() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -73,6 +84,7 @@ std::vector<uint64_t> Problem3::getFactors() const{
|
||||
return factors;
|
||||
}
|
||||
|
||||
//Returns the largest factor of the number
|
||||
uint64_t Problem3::getLargestFactor() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -82,6 +94,7 @@ uint64_t Problem3::getLargestFactor() const{
|
||||
return *factors.end();
|
||||
}
|
||||
|
||||
//Returns the number
|
||||
uint64_t Problem3::getGoalNumber() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -89,8 +102,3 @@ uint64_t Problem3::getGoalNumber() const{
|
||||
}
|
||||
return GOAL_NUMBER;
|
||||
}
|
||||
|
||||
void Problem3::reset(){
|
||||
Problem::reset();
|
||||
factors.clear();
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
//ProjectEuler/C++/Source/Problem30.cpp
|
||||
//ProjectEuler/ProjectEulerCPP/Source/Problem30.cpp
|
||||
//Matthew Ellison
|
||||
// Created: 10-27-19
|
||||
//Modified: 10-27-19
|
||||
//Modified: 07-09-20
|
||||
//Find the sum of all the numbers that can be written as the sum of the fifth powers of their digits.
|
||||
//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
|
||||
@@ -30,8 +30,12 @@
|
||||
#include "../Headers/Problem30.hpp"
|
||||
|
||||
|
||||
Problem30::Problem30() : Problem("Find the sum of all the numbers that can be written as a sum of the fifth powers of their digits"){
|
||||
}
|
||||
//This is the largest number that will be checked
|
||||
uint64_t Problem30::TOP_NUM = 1000000;
|
||||
//Start with 2 because 0 and 1 don't count
|
||||
uint64_t Problem30::BOTTOM_NUM = 2;
|
||||
//This is the power that the digits are raised to
|
||||
uint64_t Problem30::POWER_RAISED = 5;
|
||||
|
||||
//Returns a vector with the indivitual digits of the number passed into it
|
||||
std::vector<uint64_t> Problem30::getDigits(uint64_t num){
|
||||
@@ -46,6 +50,11 @@ std::vector<uint64_t> Problem30::getDigits(uint64_t num){
|
||||
return listOfDigits;
|
||||
}
|
||||
|
||||
//Constructor
|
||||
Problem30::Problem30() : Problem("Find the sum of all the numbers that can be written as a sum of the fifth powers of their digits"){
|
||||
}
|
||||
|
||||
//Solve the problem
|
||||
void Problem30::solve(){
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
@@ -78,6 +87,13 @@ void Problem30::solve(){
|
||||
solved = true;
|
||||
}
|
||||
|
||||
//Reset the problem so it can be run again
|
||||
void Problem30::reset(){
|
||||
Problem::reset();
|
||||
sumOfFifthNumbers.clear();
|
||||
}
|
||||
|
||||
//Return a string with the solution to the problem
|
||||
std::string Problem30::getString() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -124,8 +140,3 @@ uint64_t Problem30::getSumOfList() const{
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
void Problem30::reset(){
|
||||
Problem::reset();
|
||||
sumOfFifthNumbers.clear();
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/C++/Source/Problem31.cpp
|
||||
//ProjectEuler/ProjectEulerCPP/Source/Problem31.cpp
|
||||
//Matthew Ellison
|
||||
// Created: 06-19-20
|
||||
//Modified: 06-19-20
|
||||
//Modified: 07-09-20
|
||||
//How many different ways can £2 be made using any number of coins?
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
|
||||
/*
|
||||
@@ -28,12 +28,15 @@
|
||||
#include "../Headers/Problem31.hpp"
|
||||
|
||||
|
||||
//The value of coins we want
|
||||
int Problem31::desiredValue = 200;
|
||||
|
||||
//Constructor
|
||||
Problem31::Problem31() : Problem("How many different ways can 2 pounds be made using any number of coins?"){
|
||||
permutations = 0;
|
||||
}
|
||||
|
||||
//Solve the problem
|
||||
void Problem31::solve(){
|
||||
//If the problem has alread been solved do nothing and end the function
|
||||
if(solved){
|
||||
@@ -68,6 +71,13 @@ void Problem31::solve(){
|
||||
solved = true;
|
||||
}
|
||||
|
||||
//Reset the problem so it can be run again
|
||||
void Problem31::reset(){
|
||||
Problem::reset();
|
||||
permutations = 0;
|
||||
}
|
||||
|
||||
//Return a string with the solution to the problem
|
||||
std::string Problem31::getString() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -84,8 +94,3 @@ std::string Problem31::getString() const{
|
||||
int Problem31::getPermutations() const{
|
||||
return permutations;
|
||||
}
|
||||
|
||||
void Problem31::reset(){
|
||||
Problem::reset();
|
||||
permutations = 0;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
//ProjectEuler/C++/Source/Problem4.cpp
|
||||
//ProjectEuler/ProjectEulerCPP/Source/Problem4.cpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-28-18
|
||||
//Modified: 07-14-19
|
||||
//Modified: 07-09-20
|
||||
//Find the largest palindrome made from the product of two 3-digit 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
|
||||
@@ -31,12 +31,16 @@
|
||||
#include "../Headers/Problem4.hpp"
|
||||
|
||||
|
||||
//The first number to check
|
||||
int Problem4::START_NUM = 100;
|
||||
//The last number to check
|
||||
int Problem4::END_NUM = 999;
|
||||
|
||||
//Constructor
|
||||
Problem4::Problem4() : Problem("Find the largest palindrome made from the product of two 3-digit numbers."){
|
||||
}
|
||||
|
||||
//Solve the problem
|
||||
void Problem4::solve(){
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
@@ -75,6 +79,13 @@ void Problem4::solve(){
|
||||
solved = true;
|
||||
}
|
||||
|
||||
//Resets the problem so it can be run again
|
||||
void Problem4::reset(){
|
||||
Problem::reset();
|
||||
palindromes.clear();
|
||||
}
|
||||
|
||||
//Return a string with the solution to the problem
|
||||
std::string Problem4::getString() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -86,6 +97,7 @@ std::string Problem4::getString() const{
|
||||
return results.str();
|
||||
}
|
||||
|
||||
//Returns the list of all palindromes
|
||||
std::vector<uint64_t> Problem4::getPalindromes() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -94,6 +106,7 @@ std::vector<uint64_t> Problem4::getPalindromes() const{
|
||||
return palindromes;
|
||||
}
|
||||
|
||||
//Returns the largest palindrome
|
||||
uint64_t Problem4::getLargestPalindrome() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -101,8 +114,3 @@ uint64_t Problem4::getLargestPalindrome() const{
|
||||
}
|
||||
return *(palindromes.end() - 1);
|
||||
}
|
||||
|
||||
void Problem4::reset(){
|
||||
Problem::reset();
|
||||
palindromes.clear();
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
//ProjectEuler/C++/Source/Problem5.cpp
|
||||
//ProjectEuler/ProjectEulerCPP/Source/Problem5.cpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-28-18
|
||||
//Modified: 07-14-19
|
||||
//Modified: 07-09-20
|
||||
//What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
|
||||
//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
|
||||
@@ -31,9 +31,11 @@
|
||||
#include "../Headers/Problem5.hpp"
|
||||
|
||||
|
||||
//Constructor
|
||||
Problem5::Problem5() : Problem("What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?"), smallestNum(0){
|
||||
}
|
||||
|
||||
//Solve the problem
|
||||
void Problem5::solve(){
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
@@ -74,6 +76,13 @@ void Problem5::solve(){
|
||||
solved = true;
|
||||
}
|
||||
|
||||
//Reset the problem so it can be run again
|
||||
void Problem5::reset(){
|
||||
Problem::reset();
|
||||
smallestNum = 0;
|
||||
}
|
||||
|
||||
//Return a string with the solution to the problem
|
||||
std::string Problem5::getString() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -84,6 +93,7 @@ std::string Problem5::getString() const{
|
||||
return results.str();
|
||||
}
|
||||
|
||||
//Returns the requested number
|
||||
int Problem5::getNumber() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -91,8 +101,3 @@ int Problem5::getNumber() const{
|
||||
}
|
||||
return smallestNum;
|
||||
}
|
||||
|
||||
void Problem5::reset(){
|
||||
Problem::reset();
|
||||
smallestNum = 0;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
//ProjectEuler/C++/Source/Problem6.cpp
|
||||
//ProjectEuler/ProjectEulerCPP/Source/Problem6.cpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-28-18
|
||||
//Modified: 07-14-19
|
||||
//Modified: 07-09-20
|
||||
//Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
|
||||
//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
|
||||
@@ -31,12 +31,14 @@
|
||||
#include "../Headers/Problem6.hpp"
|
||||
|
||||
|
||||
int Problem6::START_NUM = 1;
|
||||
int Problem6::END_NUM = 100;
|
||||
int Problem6::START_NUM = 1; //The first number to check
|
||||
int Problem6::END_NUM = 100; //The last number to check
|
||||
|
||||
//Constructor
|
||||
Problem6::Problem6() : Problem("Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum."), sumOfSquares(0), squareOfSum(0){
|
||||
}
|
||||
|
||||
//Solve the problem
|
||||
void Problem6::solve(){
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
@@ -60,6 +62,13 @@ void Problem6::solve(){
|
||||
solved = true;
|
||||
}
|
||||
|
||||
//Reset the problem so it can be run again
|
||||
void Problem6::reset(){
|
||||
Problem::reset();
|
||||
sumOfSquares = squareOfSum = 0;
|
||||
}
|
||||
|
||||
//Return a string with the solution to the problem
|
||||
std::string Problem6::getString() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -70,6 +79,7 @@ std::string Problem6::getString() const{
|
||||
return results.str();
|
||||
}
|
||||
|
||||
//Returns the sum of all the squares
|
||||
uint64_t Problem6::getSumOfSquares() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -78,6 +88,7 @@ uint64_t Problem6::getSumOfSquares() const{
|
||||
return sumOfSquares;
|
||||
}
|
||||
|
||||
//Returns the square of all of the sums
|
||||
uint64_t Problem6::getSquareOfSum() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -86,6 +97,7 @@ uint64_t Problem6::getSquareOfSum() const{
|
||||
return squareOfSum;
|
||||
}
|
||||
|
||||
//Returns the requested difference
|
||||
uint64_t Problem6::getDifference() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -93,8 +105,3 @@ uint64_t Problem6::getDifference() const{
|
||||
}
|
||||
return abs(sumOfSquares - squareOfSum);
|
||||
}
|
||||
|
||||
void Problem6::reset(){
|
||||
Problem::reset();
|
||||
sumOfSquares = squareOfSum = 0;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/C++/Source/Problem67.cpp
|
||||
//ProjectEuler/ProjectEulerCPP/Source/Problem67.cpp
|
||||
//Matthew Ellison
|
||||
// Created: 11-02-18
|
||||
//Modified: 07-14-19
|
||||
//Modified: 07-09-20
|
||||
//The way to do this is using a breadth first search
|
||||
/*
|
||||
Find the maximum total from top to bottom
|
||||
@@ -108,7 +108,7 @@ Find the maximum total from top to bottom
|
||||
*/
|
||||
//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
|
||||
@@ -133,6 +133,7 @@ Find the maximum total from top to bottom
|
||||
#include "../Headers/Problem67.hpp"
|
||||
|
||||
|
||||
//This is the list you are trying to find a path through
|
||||
std::vector<int> Problem67::list[NUM_ROWS] = {
|
||||
{59},
|
||||
{73, 41},
|
||||
@@ -236,9 +237,7 @@ std::vector<int> Problem67::list[NUM_ROWS] = {
|
||||
{23, 33, 44, 81, 80, 92, 93, 75, 94, 88, 23, 61, 39, 76, 22, 03, 28, 94, 32, 06, 49, 65, 41, 34, 18, 23, 8, 47, 62, 60, 03, 63, 33, 13, 80, 52, 31, 54, 73, 43, 70, 26, 16, 69, 57, 87, 83, 31, 03, 93, 70, 81, 47, 95, 77, 44, 29, 68, 39, 51, 56, 59, 63, 07, 25, 70, 07, 77, 43, 53, 64, 03, 94, 42, 95, 39, 18, 01, 66, 21, 16, 97, 20, 50, 90, 16, 70, 10, 95, 69, 29, 06, 25, 61, 41, 26, 15, 59, 63, 35},
|
||||
};
|
||||
|
||||
Problem67::Problem67() : Problem("Find the maximum total from the top to the bottom of the pyramid."){
|
||||
}
|
||||
|
||||
//This function takes every number in the vector and changes it to 100 - the number
|
||||
void Problem67::invert(){
|
||||
for(unsigned int rowCnt = 0;rowCnt < NUM_ROWS;++rowCnt){
|
||||
for(unsigned int colCnt = 0;colCnt < list[rowCnt].size();++colCnt){
|
||||
@@ -247,6 +246,11 @@ void Problem67::invert(){
|
||||
}
|
||||
}
|
||||
|
||||
//Constructor
|
||||
Problem67::Problem67() : Problem("Find the maximum total from the top to the bottom of the pyramid."){
|
||||
}
|
||||
|
||||
//Solve the problem
|
||||
void Problem67::solve(){
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
@@ -311,6 +315,15 @@ void Problem67::solve(){
|
||||
solved = true;
|
||||
}
|
||||
|
||||
//Clears all of the variables so the problem can be run again
|
||||
void Problem67::reset(){
|
||||
Problem::reset();
|
||||
foundPoints.clear();
|
||||
possiblePoints.clear();
|
||||
actualTotal = 0;
|
||||
}
|
||||
|
||||
//Return a string with the solution to the problem
|
||||
std::string Problem67::getString() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -404,11 +417,3 @@ int Problem67::getTotal() const{
|
||||
}
|
||||
return actualTotal;
|
||||
}
|
||||
|
||||
//Clears all of the variables so the problem can be run again
|
||||
void Problem67::reset(){
|
||||
Problem::reset();
|
||||
foundPoints.clear();
|
||||
possiblePoints.clear();
|
||||
actualTotal = 0;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
//ProjectEuler/C++/Source/Problem7.cpp
|
||||
//ProjectEuler/ProjectEulerCPP/Source/Problem7.cpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-28-18
|
||||
//Modified: 07-14-19
|
||||
//Modified: 07-09-20
|
||||
//What is the 10001th prime number?
|
||||
//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,11 +32,14 @@
|
||||
#include "../Headers/Problem7.hpp"
|
||||
|
||||
|
||||
//The index of the prime number to find
|
||||
uint64_t Problem7::NUMBER_OF_PRIMES = 10001;
|
||||
|
||||
//Constructor
|
||||
Problem7::Problem7() : Problem("What is the 10001th prime number?"){
|
||||
}
|
||||
|
||||
//Solve the problem
|
||||
void Problem7::solve(){
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
@@ -55,6 +58,12 @@ void Problem7::solve(){
|
||||
solved = true;
|
||||
}
|
||||
|
||||
//Reset the problem so it can be run again
|
||||
void Problem7::reset(){
|
||||
Problem::reset();
|
||||
}
|
||||
|
||||
//Return a string with the solution to the problem
|
||||
std::string Problem7::getString() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -65,6 +74,7 @@ std::string Problem7::getString() const{
|
||||
return results.str();
|
||||
}
|
||||
|
||||
//Returns the requested prime number
|
||||
uint64_t Problem7::getPrime() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -72,7 +82,3 @@ uint64_t Problem7::getPrime() const{
|
||||
}
|
||||
return *primes.end();
|
||||
}
|
||||
|
||||
void Problem7::reset(){
|
||||
Problem::reset();
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/C++/Source/Problem8.cpp
|
||||
//ProjectEuler/ProjectEulerCPP/Source/Problem8.cpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-28-18
|
||||
//Modified: 07-14-19
|
||||
//Modified: 07-09-20
|
||||
//Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
|
||||
/*
|
||||
73167176531330624919225119674426574742355349194934
|
||||
@@ -27,7 +27,7 @@
|
||||
*/
|
||||
//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
|
||||
@@ -53,11 +53,14 @@
|
||||
#include "../Headers/Problem8.hpp"
|
||||
|
||||
|
||||
//The number that we are working with
|
||||
std::string Problem8::number = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
|
||||
|
||||
//Constructor
|
||||
Problem8::Problem8() : Problem("Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?"), maxProduct(0){
|
||||
}
|
||||
|
||||
//Solve the problem
|
||||
void Problem8::solve(){
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
@@ -85,6 +88,14 @@ void Problem8::solve(){
|
||||
solved = true;
|
||||
}
|
||||
|
||||
//Reset the problem so it can be run
|
||||
void Problem8::reset(){
|
||||
Problem::reset();
|
||||
maxNums = "";
|
||||
maxProduct = 0;
|
||||
}
|
||||
|
||||
//Return a string with the solution to the problem
|
||||
std::string Problem8::getString() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -96,6 +107,7 @@ std::string Problem8::getString() const{
|
||||
return results.str();
|
||||
}
|
||||
|
||||
//Returns the string of numbers that produces the largest product
|
||||
std::string Problem8::getLargestNums() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -104,6 +116,7 @@ std::string Problem8::getLargestNums() const{
|
||||
return maxNums;
|
||||
}
|
||||
|
||||
//Returns the requested product
|
||||
uint64_t Problem8::getLargestProduct() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -111,9 +124,3 @@ uint64_t Problem8::getLargestProduct() const{
|
||||
}
|
||||
return maxProduct;
|
||||
}
|
||||
|
||||
void Problem8::reset(){
|
||||
Problem::reset();
|
||||
maxNums = "";
|
||||
maxProduct = 0;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
//ProjectEuler/C++/Source/Problem9.cpp
|
||||
//ProjectEuler/ProjectEulerCPP/Source/Problem9.cpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-28-18
|
||||
//Modified: 07-14-19
|
||||
//Modified: 07-09-20
|
||||
//There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product of abc.
|
||||
//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
|
||||
@@ -30,9 +30,11 @@
|
||||
#include "../Headers/Problem9.hpp"
|
||||
|
||||
|
||||
//Constructor
|
||||
Problem9::Problem9() : Problem("There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product of abc."), a(1), b(0), c(0), found(false){
|
||||
}
|
||||
|
||||
//Solve the problem
|
||||
void Problem9::solve(){
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
@@ -68,6 +70,16 @@ void Problem9::solve(){
|
||||
solved = true;
|
||||
}
|
||||
|
||||
//Reset the problem so it can be run again
|
||||
void Problem9::reset(){
|
||||
Problem::reset();
|
||||
a = 1;
|
||||
b = 0;
|
||||
c = 0;
|
||||
found = false;
|
||||
}
|
||||
|
||||
//Return a string with the solution to the problem
|
||||
std::string Problem9::getString() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -85,6 +97,7 @@ std::string Problem9::getString() const{
|
||||
return results.str();
|
||||
}
|
||||
|
||||
//Returns the length of the first side
|
||||
int Problem9::getSideA() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -93,6 +106,7 @@ int Problem9::getSideA() const{
|
||||
return a;
|
||||
}
|
||||
|
||||
//Returns the length of the second side
|
||||
int Problem9::getSideB() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -101,6 +115,7 @@ int Problem9::getSideB() const{
|
||||
return b;
|
||||
}
|
||||
|
||||
//Returns the length of the hyp
|
||||
int Problem9::getSideC() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -109,6 +124,7 @@ int Problem9::getSideC() const{
|
||||
return (int)c;
|
||||
}
|
||||
|
||||
//Returns the product of the 3 sides
|
||||
int Problem9::getProduct() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
@@ -116,11 +132,3 @@ int Problem9::getProduct() const{
|
||||
}
|
||||
return a * b * (int)c;
|
||||
}
|
||||
|
||||
void Problem9::reset(){
|
||||
Problem::reset();
|
||||
a = 1;
|
||||
b = 0;
|
||||
c = 0;
|
||||
found = false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user