Updated comments and made sure style was consistent

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

View File

@@ -1,8 +1,25 @@
//ProjectEuler/C++/Headers/Problem.hpp
//ProjectEuler/ProjectEulerCPP/Headers/Problem.hpp
//Matthew Ellison
// Created: 07-04-19
//Modified: 07-10-19
//Modified: 07-09-20
//This is an abstract base class to allow polymorphism for the individual problems
/*
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
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef PROBLEM_HPP
#define PROBLEM_HPP
@@ -25,9 +42,11 @@ public:
Problem(std::string description) : description(description), solved(false){
}
//Destructor
virtual ~Problem(){
}
//Gets
virtual std::string getDescription() const{
return description;
}
@@ -37,12 +56,14 @@ public:
virtual mee::Stopwatch getTimer(){
return timer;
}
virtual void solve() = 0;
virtual std::string getString() const = 0;
//Reset the problem so it can be run again
virtual void reset(){
timer.reset();
solved = false;
}
//Pure virtual functions
virtual void solve() = 0; //Solve the problem
virtual std::string getString() const = 0; //Return a string with the solution to the problem
};
#endif //PROBLEM_HPP

View File

@@ -1,11 +1,11 @@
//ProjectEuler/C++/Headers/Problem1.hpp
//ProjectEuler/ProjectEulerCPP/Headers/Problem1.hpp
//Matthew Ellison
// Created: 09-28-18
//Modified: 07-14-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
@@ -33,21 +33,26 @@
class Problem1: public Problem{
private:
static uint64_t MAX_NUMBER;
//Variables
//Static variables
static uint64_t MAX_NUMBER; //The highest number to be tested
//Instance variables
uint64_t fullSum; //For the sum of all the numbers
std::vector<uint64_t> numbers; //Holds all the numbers
public:
//Constructor
Problem1();
virtual void solve();
virtual std::string getString() const;
virtual void reset();
//Returns the requested sum
uint64_t getSum() const;
//Operational functions
virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again
//Gets
virtual std::string getString() const; //Return a string with the solution to the problem
uint64_t getSum() const; //Returns the requested sum
};
/* Results:
The sum of all the numbers < 1000 that are divisible by 3 or 5 is 233168
It took 4.500 microseconds to solve this problem.
It took an average of 957.000 nanoseconds to run this problem over 100 iterations
*/
#endif //PROBLEM1_HPP

View File

@@ -1,11 +1,11 @@
//ProjectEuler/C++/Headers/Problem10.hpp
//ProjectEuler/ProjectEulerCPP/Headers/Problem10.hpp
//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
@@ -32,20 +32,25 @@
class Problem10 : public Problem{
private:
//Variables
//Static variables
static uint64_t GOAL_NUMBER;
//Instance variables
uint64_t sum;
public:
//Constructor
Problem10();
virtual void solve();
virtual std::string getString() const;
virtual void reset();
//Returns the sum that was requested
uint64_t getSum() const;
//Operational functions
virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again
//Gets
virtual std::string getString() const; //Return a string with the solution to the problem
uint64_t getSum() const; //Returns the sum that was requested
};
/* Results:
The sum of all the primes less than 2000000 is 142913828922
It took 162.240 milliseconds to solve this problem.
It took an average of 171.141 milliseconds to run this problem over 100 iterations
*/
#endif //PROBLEM10_HPP

View File

@@ -1,7 +1,7 @@
//ProjectEuler/C++/Headers/Problem11.hpp
//MatthewEllison
//ProjectEuler/ProjectEulerCPP/Headers/Problem11.hpp
//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
@@ -54,24 +54,27 @@
class Problem11 : public Problem{
private:
//This is the grid of number that we will be working with
static std::vector<int> grid[20];
//Variables
//Static variables
static std::vector<int> grid[20]; //This is the grid of number that we will be working with
//Instance variables
std::vector<int> greatestProduct; //This is a vector containing the largest product we have found so far
public:
//Constructor
Problem11();
virtual void solve();
virtual std::string getString() const;
virtual void reset();
//Returns the numbers that were being searched
std::vector<int> getNumbers() const;
//Returns the product that was requested
int getProduct() const;
//Operational functions
virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again
//Gets
virtual std::string getString() const; //Return a string with the solution to the problem
std::vector<int> getNumbers() const; //Returns the numbers that were being searched
int getProduct() const; //Returns the product that was requested
};
/* Results:
The greatest product of 4 number in a line is 70600674
The numbers are 89 94 97 87
It took 12.900 microseconds to solve this problem
It took an average of 11.201 microseconds to run this problem over 100 iterations
*/
#endif //PROBLEM11_HPP

View File

@@ -1,10 +1,10 @@
//ProjectEuler/C++/Headers/Problem12.hpp
//ProjectEuler/ProjectEulerCPP/Headers/Problem12.hpp
//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
@@ -32,29 +32,31 @@
class Problem12 : public Problem{
private:
//Variables
//Static variables
static uint64_t GOAL_DIVISORS; //The number of divisors that you want
//Instance variables
int64_t sum; //The sum of the numbers up to counter
int64_t counter; //The next number to be added to sum
std::vector<int64_t> divisors; //Holds the divisors of the triangular number sum
public:
//Constructor
Problem12();
virtual void solve();
virtual std::string getString() const;
virtual void reset();
//Returns the triangular number
int64_t getTriangularNumber() const;
//Get the final number that was added to the triangular number
int64_t getLastNumberAdded() const;
//Returns the list of divisors of the requested number
std::vector<int64_t> getDivisorsOfTriangularNumber() const;
//Returns the number of divisors of the requested number
size_t getNumberOfDivisors() const;
//Operational functions
virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again
//Gets
virtual std::string getString() const; //Return a string with the solution to the problem
int64_t getTriangularNumber() const; //Returns the triangular number
int64_t getLastNumberAdded() const; //Get the final number that was added to the triangular number
std::vector<int64_t> getDivisorsOfTriangularNumber() const; //Returns the list of divisors of the requested number
size_t getNumberOfDivisors() const; //Returns the number of divisors of the requested number
};
/* Results:
The triangular number 76576500 is a sum of all numbers >= 12375 and has 576 divisors
It took 262.765 milliseconds to solve this problem.
It took an average of 280.536 milliseconds to run this problem over 100 iterations
*/
#endif //PROBLEM12_HPP

View File

@@ -1,7 +1,7 @@
//ProjectEuler/C++/Headers/Problem13.hpp
//ProjectEuler/ProjectEulerCPP/Headers/Problem13.hpp
//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
@@ -139,27 +139,31 @@
class Problem13 : public Problem{
private:
//A vector to hold all of the numbers
std::vector<mpz_class> nums;
mpz_class sum;
void setNums();
void reserveVectors();
//Variables
//Instance variables
std::vector<mpz_class> nums; //A vector to hold all of the numbers
mpz_class sum; //The sum of all the numbers
//Functions
void setNums(); //A function to set the nums vector
void reserveVectors(); //Reserve the size of the vector to speed up insertion
public:
//Constructor
Problem13();
virtual void solve();
virtual std::string getString() const;
virtual void reset();
//Returns the list 50-digit numbers
std::vector<mpz_class> getNumbers() const;
//Returns the sum of the 50-digit numbers
mpz_class getSum() const;
//Operational functions
virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again
//Gets
virtual std::string getString() const; //Return a string with the solution to the problem
std::vector<mpz_class> getNumbers() const; //Returns the list 50-digit numbers
mpz_class getSum() const; //Returns the sum of the 50-digit numbers
};
/* Results:
The sum of all 100 numbers is 5537376230390876637302048746832985971773659831892672
The first 10 digits of the sum of the numbers is 5537376230
It took 0.000 nanoseconds to solve this problem.
It took an average of 13.270 microseconds to run this problem over 100 iterations
*/
#endif //PROBLEM13_HPP

View File

@@ -1,7 +1,7 @@
//ProjectEuler/C++/Headers/Problem14.hpp
//ProjectEuler/ProjectEulerCPP/Headers/Problem14.hpp
//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
@@ -37,25 +37,30 @@ Which starting number, under one million, produces the longest chain?
class Problem14 : public Problem{
private:
//This function follows the rules of the sequence and returns its length
uint64_t checkSeries(uint64_t num);
//Variables
//Static variables
static uint64_t MAX_NUM; //This is the top number that you will be checking against the series
//Instance variables
uint64_t maxLength; //This is the length of the longest chain
uint64_t maxNum; //This is the starting number of the longest chain
//Function
uint64_t checkSeries(uint64_t num); //This function follows the rules of the sequence and returns its length
public:
//Constructor
Problem14();
virtual void solve();
virtual std::string getString() const;
virtual void reset();
//Returns the length of the requested chain
uint64_t getLength() const;
//Returns the starting number of the requested chain
uint64_t getStartingNumber() const;
//Operational functions
virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again
//Gets
virtual std::string getString() const; //Return a string with the solution to the problem
uint64_t getLength() const; //Returns the length of the requested chain
uint64_t getStartingNumber() const; //Returns the starting number of the requested chain
};
/* Results:
The number 837799 produced a chain of 525 steps
It took 153.999 milliseconds to solve this problem.
It took an average of 197.008 milliseconds to run this problem over 100 iterations
*/
#endif //PROBLEM14_HPP

View File

@@ -1,11 +1,11 @@
//ProjectEuler/C++/Headers/Problem15.hpp
//ProjectEuler/ProjectEulerCPP/Headers/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
@@ -32,23 +32,30 @@
class Problem15 : public Problem{
private:
//Variables
//Static variables
static int WIDTH; //The width of the grid
static int LENGTH; //The length of the grid
//Instance variables
uint64_t numOfRoutes; //The number of routes from 0, 0 to 20, 20
//Functions
//This function acts as a handler for moving the position on the grid and counting the distance
//If moves right first, then down
//It moves right first, then down
void move(int currentX, int currentY, uint64_t& numOfRoutes);
public:
//Constructor
Problem15();
virtual void solve();
virtual std::string getString() const;
virtual void reset();
//Returns the number of routes found
uint64_t getNumberOfRoutes() const;
//Operational functions
virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again
//Gets
virtual std::string getString() const; //Return a string with the solution to the problem
uint64_t getNumberOfRoutes() const; //Returns the number of routes found
};
/* Results:
The number of routes is 137846528820
It took 10.914 minutes to solve this problem.
It took an average of 18.010 minutes to run this problem through 10 iterations
*/
#endif //PROBLEM15_HPP

View File

@@ -1,14 +1,14 @@
//ProjectEuler/C++/Headers/Problem16.hpp
//ProjectEuler/ProjectEulerCPP/Headers/Problem16.hpp
//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,25 +35,29 @@
class Problem16 : public Problem{
private:
//Variables
//Static variables
static int NUM_TO_POWER; //The number that is going to be raised to a power
static int POWER; //The power that the number is going to be raised to
//Instance variables
mpz_class num; //The number to be calculated
int sumOfElements; //The sum of all digits in the number
public:
//Constructors
Problem16();
virtual void solve();
virtual std::string getString() const;
virtual void reset();
//Returns the number that was calculated
mpz_class getNumber() const;
//Return the sum of the digits of the number
int getSum() const;
//Operational functions
virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again
//Gets
virtual std::string getString() const; //Return a string with the solution to the problem
mpz_class getNumber() const; //Returns the number that was calculated
int getSum() const; //Return the sum of the digits of the number
};
/* Results:
2^1000 = 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376
The sum of the elements is 1366
It took 0.000 nanoseconds to solve this problem.
It took an average of 4.806 microseconds to run this problem over 100 iterations
*/
#endif //PROBLEM16_HPP

View File

@@ -1,11 +1,11 @@
//ProjectEuler/C++/Headers/Problem17.hpp
//ProjectEuler/ProjectEulerCPP/Headers/Problem17.hpp
//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
@@ -32,23 +32,29 @@
class Problem17 : public Problem{
private:
std::string makeWord(int num);
std::string wordHelper(int num);
uint64_t countLetters(std::string str);
//Variables
//Static variables
static int TOP_NUM; //This is the largest number to get the words of
//Instance variables
uint64_t letterCount; //This is the cumulative number of letters in the words of the numbers
//Functions
std::string makeWord(int num); //This function makes a word out of the number passed into it
std::string wordHelper(int num); //This function helps makeWord() by returning the words for the numbers 1-9
uint64_t countLetters(std::string str); //This counts the number of letters in the string that is passed in (ignoring numbers and punctuation)
public:
//Constructor
Problem17();
virtual void solve();
virtual std::string getString() const;
virtual void reset();
//Returns the number of letters asked for
uint64_t getLetterCount() const;
//Operational functions
virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again
//Gets
virtual std::string getString() const; //Return a string with the solution to the problem
uint64_t getLetterCount() const; //Returns the number of letters asked for
};
/* Results:
The number of letters is 21124
It took 217.500 microseconds to solve this problem.
It took an average of 220.126 microseconds to run this problem over 100 iterations
*/
#endif //Problem17_HPP

View File

@@ -1,7 +1,7 @@
//ProjectEuler/C++/Headers/Problem18.hpp
//ProjectEuler/ProjectEulerCPP/Headers/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
@@ -51,6 +51,8 @@
class Problem18 : public Problem{
private:
//Structures
//A structure to hold the location data for a point on the map
struct location{
int xLocation;
int yLocation;
@@ -58,30 +60,34 @@ private:
bool fromRight;
location(int x, int y, int t, bool r) : xLocation(x), yLocation(y), total(t), fromRight(r){ }
};
//This list turns every number in the vector into 100 - num
void invert();
//Variables
//Static variables
static const int NUM_ROWS = 15; //The number of rows in the array
//Setup the list you are trying to find a path through
static std::vector<int> list[NUM_ROWS];
static std::vector<int> list[NUM_ROWS]; //Setup the list you are trying to find a path through
//Instance variables
std::list<location> foundPoints; //For the points that I have already found the shortest distance to
std::list<location> possiblePoints; //For the locations you are checking this round
int actualTotal; //The true total of the path from the top to the bottom
//Functions
void invert(); //This list turns every number in the vector into 100 - num
public:
//Constructor
Problem18();
virtual void solve();
virtual std::string getString() const;
virtual void reset();
//Returns the pyramid that was traversed as a string
std::string getPyramid();
//Returns the trail the algorithm took as a string
std::string getTrail();
//Returns the total that was asked for
int getTotal() const;
//Operational functions
virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again
//Gets
virtual std::string getString() const; //Return a string with the solution to the problem
std::string getPyramid(); //Returns the pyramid that was traversed as a string
std::string getTrail(); //Returns the trail the algorithm took as a string
int getTotal() const; //Returns the total that was asked for
};
/* Results:
The value of the longest path is 1074
It took 16.500 microseconds to solve this problem.
It took an average of 9.925 microseconds to run this problem over 100 iterations
*/
#endif //PROBLEM18_HPP

View File

@@ -1,7 +1,7 @@
//ProjectEuler/C++/Headers/Problem19.hpp
//ProjectEuler/ProjectEulerCPP/Headers/Problem19.hpp
//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,28 +43,31 @@ A leap year occurs on any year evenly divisible by 4, but not on a century unles
class Problem19 : public Problem{
private:
//Variables
//Variables
//Staic variables
//An easier way to return the days
enum DAYS {SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, NUMBER_OF_DAYS, ERROR};
static unsigned int START_YEAR; //The start year
static unsigned int END_YEAR; //The stop year
//Instance variables
uint64_t totalSundays; //Keep track of the number of sundays
//Functions
//Return the day of the week that the date you pass into it is on
DAYS getDay(unsigned int month, unsigned int day, unsigned int year);
//Returns true if the year passed to it is a leap year
bool isLeapYear(unsigned int year);
//Functions
DAYS getDay(unsigned int month, unsigned int day, unsigned int year); //Return the day of the week that the date you pass into it is on
bool isLeapYear(unsigned int year); //Returns true if the year passed to it is a leap year
public:
//Constructors
Problem19();
virtual void solve();
virtual std::string getString() const;
virtual void reset();
//Returns the total sundays that were asked for
uint64_t getTotalSundays() const;
//Operational functions
virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again
//Gets
virtual std::string getString() const; //Return a string with the solution to the problem
uint64_t getTotalSundays() const; //Returns the total sundays that were asked for
};
/* Results
There are 171 Sundays that landed on the first of the months from 1901 to 2000
It took 4.579 milliseconds to solve this problem.
It took an average of 4.749 milliseconds to run this problem over 100 iterations
*/
#endif //PROBLEM19_HPP

View File

@@ -1,11 +1,11 @@
//ProjectEuler/C++/Headers/Problem2.hpp
//ProjectEuler/ProjectEulerCPP/Headers/Problem2.hpp
//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
@@ -32,20 +32,25 @@
class Problem2 : public Problem{
private:
//Variables
//Static variables
static uint64_t TOP_NUM; //Holds the largest number that we are looking for
//Instance variables
uint64_t fullSum; //Holds the sum of all the numbers
public:
//Constructor
Problem2();
virtual void solve();
virtual std::string getString() const;
virtual void reset();
//Returns the requested sum
uint64_t getSum() const;
//Operational functions
virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again
//Gets
virtual std::string getString() const; //Return a string with the solution to the problem
uint64_t getSum() const; //Returns the requested sum
};
/* Results:
The sum of the even Fibonacci numbers less than 4,000,000 is 4613732
It took 2.200 microseconds to solve this problem.
It took an average of 324.000 nanoseconds to run this problem over 100 iterations
*/
#endif //PROBLEM2_HPP

View File

@@ -1,14 +1,14 @@
//ProjectEuler/C++/Headers/Problem20.hpp
//ProjectEuler/ProjectEulerCPP/Headers/Problem20.hpp
//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
@@ -36,25 +36,27 @@
class Problem20 : public Problem{
private:
//Variables
//Instance variables
mpz_class num; //Holds the number 100!
uint64_t sum; //The sum of the digits of num
public:
//Constructor
Problem20();
virtual void solve();
virtual std::string getString() const;
virtual void reset();
//Returns the number 100!
mpz_class getNumber() const;
//Returns the number 100! in a string
std::string getNumberString() const;
//Returns the sum of the digits of 100!
uint64_t getSum() const;
//Operational functions
virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again
//Gets
virtual std::string getString() const; //Return a string with the solution to the problem
mpz_class getNumber() const; //Returns the number 100!
std::string getNumberString() const; //Returns the number 100! in a string
uint64_t getSum() const; //Returns the sum of the digits of 100!
};
/* Results:
100! = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
The sum of the digits is: 648
It took 8.300 microseconds to solve this problem.
It took an average of 4.094 microseconds to run this problem over 100 iterations
*/
#endif //PROBLEM20_HPP

View File

@@ -1,11 +1,11 @@
//ProjectEuler/C++/Headers/Problem21.hpp
//ProjectEuler/ProjectEulerCPP/Headers/Problem21.hpp
//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
@@ -33,19 +33,25 @@
class Problem21 : public Problem{
private:
//Variables
//Static variables
static int LIMIT; //The top number that will be evaluated
//Instance variables
std::vector<uint64_t> divisorSum; //Holds the sum of the divisors of the subscript number
std::vector<uint64_t> amicable; //Holds all amicable numbers
void reserveVectors();
//Functions
void reserveVectors(); //Reserve the size of the vector to speed up insertion
public:
//Constructor
Problem21();
virtual void solve();
virtual std::string getString() const;
virtual void reset();
//Returns a vector with all of the amicable numbers calculated
std::vector<uint64_t> getAmicable() const;
//Returns the sum of all of the amicable numbers
uint64_t getSum() const;
//Operational functions
virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again
//Gets
virtual std::string getString() const; //Return a string with the solution to the problem
std::vector<uint64_t> getAmicable() const; //Returns a vector with all of the amicable numbers calculated
uint64_t getSum() const; //Returns the sum of all of the amicable numbers
};
@@ -62,7 +68,7 @@ All amicable numbers less than 10000 are
6232
6368
The sum of all of these amicable numbers is 31626
It took 4.083 milliseconds to solve this problem.
It took an average of 4.310 milliseconds to run this problem over 100 iterations
*/
#endif //PROBLEM21_HPP

View File

@@ -1,11 +1,11 @@
//ProjectEuler/C++/Headers/Problem22.hpp
//ProjectEuler/ProjectEulerCPP/Headers/Problem22.hpp
//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
@@ -33,24 +33,30 @@
class Problem22 : public Problem{
private:
//Variables
//Static variables
static std::vector<std::string> names; //Holds the names that will be scored
//Instance variables
std::vector<uint64_t> sums; //Holds the score based on the sum of the characters in the name
std::vector<uint64_t> prod; //Holds the score based on the sum of the characters and the location in alphabetical order
static std::vector<std::string> names; //Holds the names that will be scored
void reserveVectors();
//Functions
void reserveVectors(); //Reserve the size of the vector to speed up insertion
public:
//Constructor
Problem22();
virtual void solve();
virtual std::string getString() const;
virtual void reset();
//Returns the vector of the names being scored
std::vector<std::string> getNames() const;
//Returns the sum of the names scores
uint64_t getNameScoreSum() const;
//Operational functions
virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again
//Gets
virtual std::string getString() const; //Return a string with the solution to the problem
std::vector<std::string> getNames() const; //Returns the vector of the names being scored
uint64_t getNameScoreSum() const; //Returns the sum of the names scores
};
/* Results:
The answer to the question is 871198282
It took 638.400 microseconds to solve this problem.
It took an average of 436.559 microseconds to run this problem over 100 iterations
*/
#endif //Problem22

View File

@@ -1,11 +1,11 @@
//ProjectEuler/C++/Headers/Problem23.hpp
//ProjectEuler/ProjectEulerCPP/Headers/Problem23.hpp
//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
@@ -33,24 +33,30 @@
class Problem23 : public Problem{
private:
static int MAX_NUM;
//Variable
//Static variables
static int MAX_NUM; //The largest possible number that can not be written as the sum of two abundant numbers
//Instance variables
std::vector<uint64_t> divisorSums; //This gives the sum of the divisors at subscripts
uint64_t sum; //The sum of all the numbers we are looking for
//A function that returns true if num can be created by adding two elements from abund and false if it cannot
bool isSum(const std::vector<int>& abund, int num);
void reserveVectors();
//Functions
bool isSum(const std::vector<int>& abund, int num); //A function that returns true if num can be created by adding two elements from abund and false if it cannot
void reserveVectors(); //Reserve the size of the vector to speed up insertion
public:
//Constructor
Problem23();
virtual void solve();
virtual std::string getString() const;
virtual void reset();
//Returns the sum of the numbers asked for
uint64_t getSum() const;
//Operational functions
virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again
//Gets
virtual std::string getString() const; //Return a string with the solution to the problem
uint64_t getSum() const; //Returns the sum of the numbers asked for
};
/* Results:
The answer is 4179871
It took 4.888 seconds to solve this problem.
It took an average of 5.902 seconds to run this problem over 100 iterations
*/
#endif //PROBLEM23_HPP

View File

@@ -1,11 +1,11 @@
//ProjectEuler/C++/Headers/Problem24.hpp
//ProjectEuler/ProjectEulerCPP/Headers/Problem24.hpp
//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
@@ -32,23 +32,26 @@
class Problem24 : public Problem{
private:
//Variables
//Static variables
static int NEEDED_PERM; //The number of the permutation that you need
static std::string nums; //All of the characters that we need to get the permutations of
//Instance variables
std::vector<std::string> permutations; //Holds all of the permutations of the string nums
public:
//Constructor
Problem24();
virtual void solve();
virtual std::string getString() const;
virtual void reset();
//Returns a vector with all of the permutations
std::vector<std::string> getPermutationsList() const;
//Returns the specific permutations you are looking for
std::string getPermutation() const;
virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again
//Gets
virtual std::string getString() const; //Return a string with the solution to the problem
std::vector<std::string> getPermutationsList() const; //Returns a vector with all of the permutations
std::string getPermutation() const; //Returns the specific permutations you are looking for
};
/* Results
The 1 millionth permutation is 2783915460
It took 1.166 seconds to solve this problem.
It took an average of 1.157 seconds to run this problem over 100 iterations
*/
#endif //PROBLEM24_HPP

View File

@@ -1,14 +1,14 @@
//ProjectEuler/C++/Headers/Problem25.hpp
//ProjectEuler/ProjectEulerCPP/Headers/Problem25.hpp
//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
@@ -35,14 +35,20 @@
class Problem25 : public Problem{
private:
//Variables
//Static variables
static unsigned int NUM_DIGITS; //The number of digits to calculate up to
//Instance variables
mpz_class number; //The current Fibonacci number
mpz_class index; //The index of the current Fibonacci number just calculated
public:
//Constructor
Problem25();
virtual void solve();
virtual std::string getString() const;
virtual void reset();
//Operational functions
virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again
//Gets
virtual std::string getString() const; //Return a string with the solution to the problem
mpz_class getNumber() const; //Returns the Fibonacci number asked for
std::string getNumberString() const; //Returns the Fibonacci number asked for as a string
mpz_class getIndex() const; //Returns the index of the requested Fibonacci number
@@ -53,7 +59,7 @@ public:
/* Results:
The first Fibonacci number with 1000 digits is 1070066266382758936764980584457396885083683896632151665013235203375314520604694040621889147582489792657804694888177591957484336466672569959512996030461262748092482186144069433051234774442750273781753087579391666192149259186759553966422837148943113074699503439547001985432609723067290192870526447243726117715821825548491120525013201478612965931381792235559657452039506137551467837543229119602129934048260706175397706847068202895486902666185435124521900369480641357447470911707619766945691070098024393439617474103736912503231365532164773697023167755051595173518460579954919410967778373229665796581646513903488154256310184224190259846088000110186255550245493937113651657039447629584714548523425950428582425306083544435428212611008992863795048006894330309773217834864543113205765659868456288616808718693835297350643986297640660000723562917905207051164077614812491885830945940566688339109350944456576357666151619317753792891661581327159616877487983821820492520348473874384736771934512787029218636250627816
Its index is 4782
It took 188.246 milliseconds to solve this problem.
It took an average of 241.017 milliseconds to run this problem over 100 iterations
*/
#endif //PROBLEM25_HPP

View File

@@ -1,11 +1,11 @@
//ProjectEuler/C++/Headers/Problem26.hpp
//ProjectEuler/ProjectEulerCPP/Headers/Problem26.hpp
//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
@@ -32,14 +32,20 @@
class Problem26 : public Problem{
private:
//Variables
//Static variables
static unsigned int TOP_NUMBER; //Holds the highest denominator we will check
//Instance variables
unsigned int longestCycle; //The length of the longest cycle
unsigned int longestNumber; //The starting denominator of the longest cycle
public:
//Constructor
Problem26();
virtual void solve();
virtual std::string getString() const;
virtual void reset();
//Operational functions
virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again
//Gets
virtual std::string getString() const; //Return a string with the solution to the problem
unsigned int getLongestCycle() const; //Returns the length of the longest cycle
unsigned int getLongestNumber() const; //Returns the denominator that starts the longest cycle
};
@@ -48,7 +54,7 @@ public:
/* Results:
The longest cycle is 982 digits long
It is started with the number 983
It took 6.865 milliseconds to solve this problem.
It took an average of 9.989 milliseconds to run this problem over 100 iterations
*/
#endif //PROBLEM26_HPP

View File

@@ -1,11 +1,11 @@
//ProjectEuler/C++/Headers/Problem27.hpp
//ProjectEuler/ProjectEulerCPP/Headers/Problem27.hpp
//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
@@ -33,25 +33,30 @@
class Problem27 : public Problem{
private:
//Variables
//Instance variables
int64_t topA; //The A for the most n's generated
int64_t topB; //The B for the most n's generated
int64_t topN; //The most n's generated
std::vector<int64_t> primes; //A list of all primes that could possibly be generated with this formula
public:
//Constructor
Problem27();
virtual void solve();
virtual std::string getString() const;
virtual void reset();
int64_t getTopA() const;
int64_t getTopB() const;
int64_t getTopN() const;
//Operational functions
virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again
//Gets
virtual std::string getString() const; //Return a string with the solution to the problem
int64_t getTopA() const; //Returns the top A that was generated
int64_t getTopB() const; //Returns the top B that was generated
int64_t getTopN() const; //Returns the top N that was generated
};
/* Results:
The greatest number of primes found is 70
It was found with A = -61, B = 971
The product of A and B is -59231
It took 2.076 seconds to solve this problem.
It took an average of 2.176 seconds to run this problem over 100 iterations
*/
#endif //PROBLEM27_HPP

View File

@@ -1,11 +1,11 @@
//ProjectEuler/C++/Headers/Problem28.hpp
//ProjectEuler/ProjectEulerCPP/Headers/Problem28.hpp
//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
@@ -34,23 +34,30 @@
class Problem28 : public Problem{
private:
std::vector<std::vector<int>> grid; //Holds the grid that we will be filling and searching
//Variables
//Instance variables
std::vector<std::vector<int>> grid; //Holds the grid that we will be filling and searching
uint64_t sumOfDiagonals; //Holds the sum of the diagonals of the grid
//Functions
void setupGrid(); //This sets up the grid to hold the correct number of variables
void createGrid(); //Puts all of the numbers in the grid up the grid
void findSum(); //Finds the sum of the diagonals in the grid
public:
//Constructor
Problem28();
virtual void solve();
virtual std::string getString() const;
virtual void reset();
std::vector<std::vector<int>> getGrid() const; //Returns the grid
//Operational functions
virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again
//Gets
virtual std::string getString() const; //Return a string with the solution to the problem
std::vector<std::vector<int>> getGrid() const; //Returns the grid
uint64_t getSum() const; //Returns the sum of the diagonals
};
/* Results:
The sum of the diagonals in the given grid is 669171001
It took 913.800 microseconds to solve this problem.
It took an average of 1.254 milliseconds to run this problem over 100 iterations
*/
#endif //PROBLEM28_HPP

View File

@@ -1,14 +1,14 @@
//ProjectEuler/C++/Headers/Problem29.hpp
//ProjectEuler/ProjectEulerCPP/Headers/Problem29.hpp
//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
@@ -38,26 +38,32 @@
class Problem29 : public Problem{
private:
static const unsigned int BOTTOM_A = 2; //The lowest possible value for a
static const unsigned int TOP_A = 100; //The highest possible value for a
static const unsigned int BOTTOM_B = 2; //The lowest possible value for b
static const unsigned int TOP_B = 100; //The highest possible value for b
//Variables
//Static variables
static unsigned int BOTTOM_A; //The lowest possible value for a
static unsigned int TOP_A; //The highest possible value for a
static unsigned int BOTTOM_B; //The lowest possible value for b
static unsigned int TOP_B; //The highest possible value for b
//Instance variables
std::vector<mpz_class> unique; //Holds all values in powers, except repeats
public:
//Constructor
Problem29();
virtual void solve();
virtual std::string getString() const;
virtual void reset();
unsigned int getBottomA() const;
unsigned int getTopA() const;
unsigned int getBottomB() const;
unsigned int getTopB() const;
std::vector<mpz_class> getUnique() const;
//Operational functions
virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again
//Gets
virtual std::string getString() const; //Return a string with the solution to the problem
unsigned int getBottomA() const; //Returns the lowest possible value for a
unsigned int getTopA() const; //Returns the highest possible value for a
unsigned int getBottomB() const; //Returns the lowest possible value for b
unsigned int getTopB() const; //Returns the highest possible value for b
std::vector<mpz_class> getUnique() const; //Returns a vector of all the unique values for a^b
};
/* Results:
The number of unique values generated by a^b for 2 <= a <= 100 and 2 <= b <= 100 is 9183
It took 1.464 seconds to solve this problem.
It took an average of 1.651 seconds to run this problem over 100 iterations
*/
#endif //PROBLEM29_HPP

View File

@@ -1,11 +1,11 @@
//ProjectEuler/C++/Headers/Problem3.hpp
//ProjectEuler/ProjectEulerCPP/Headers/Problem3.hpp
//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
@@ -33,24 +33,27 @@
class Problem3 : public Problem{
private:
static uint64_t GOAL_NUMBER; //The number you are trying to find the factors of
//Variables
//Static variables
static uint64_t GOAL_NUMBER; //The number of which you are trying to find the factors
//Instance variables
std::vector<uint64_t> factors; //Holds the factors of goalNumber
public:
//Constructor
Problem3();
virtual void solve();
virtual std::string getString() const;
virtual void reset();
//Returns the list of factors of the number
std::vector<uint64_t> getFactors() const;
//Returns the largest factor of the number
uint64_t getLargestFactor() const;
//Returns the number
uint64_t getGoalNumber() const;
//Operational functions
virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again
//Gets
virtual std::string getString() const; //Return a string with the solution to the problem
std::vector<uint64_t> getFactors() const; //Returns the list of factors of the number
uint64_t getLargestFactor() const; //Returns the largest factor of the number
uint64_t getGoalNumber() const; //Returns the number
};
/* Results:
The largest factor of the number 600851475143 is 6857
It took 46.738 milliseconds to solve this problem.
It took an average of 50.300 milliseconds to run this problem over 100 iterations
*/
#endif //PROBLEM3_HPP

View File

@@ -1,11 +1,11 @@
//ProjectEuler/C++/Headers/Problem30.hpp
//ProjectEuler/ProjectEulerCPP/Headers/Problem30.hpp
//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
@@ -33,16 +33,24 @@
class Problem30 : public Problem{
private:
static const uint64_t TOP_NUM = 1000000; //This is the largest number that will be checked
static const uint64_t BOTTOM_NUM = 2; //Start with 2 because 0 and 1 don't count
static const uint64_t POWER_RAISED = 5; //This is the power that the digits are raised to
//Variables
//Static variables
static uint64_t TOP_NUM; //This is the largest number that will be checked
static uint64_t BOTTOM_NUM; //Start with 2 because 0 and 1 don't count
static uint64_t POWER_RAISED; //This is the power that the digits are raised to
//Instance variables
std::vector<uint64_t> sumOfFifthNumbers; //This is a vector of the numbers that are the sum of the fifth power of their digits
//Functions
std::vector<uint64_t> getDigits(uint64_t num); //Returns a vector with the indivitual digits of the number passed into it
public:
//Constructor
Problem30();
virtual void solve();
virtual std::string getString() const;
virtual void reset();
//Operational functions
virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again
//Gets
virtual std::string getString() const; //Return a string with the solution to the problem
uint64_t getTopNum() const; //This returns the top number to be checked
std::vector<uint64_t> getListOfSumOfFifths() const; //This returns a copy of the vector holding all the numbers that are the sum of the fifth power of their digits
uint64_t getSumOfList() const; //This returns the sum of all entries in sumOfFifthNumbers
@@ -51,7 +59,7 @@ public:
/* Results:
The sum of all the numbers that can be written as the sum of the fifth powers of their digits is 443839
It took 280.300 milliseconds to solve this problem.
It took an average of 306.944 milliseconds to run this problem over 100 iterations
*/

View File

@@ -1,7 +1,7 @@
//ProjectEuler/C++/Headers/Problem31.hpp
//ProjectEuler/ProjectEulerCPP/Headers/Problem31.hpp
//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
/*
@@ -32,21 +32,26 @@
class Problem31 : public Problem{
private:
static int desiredValue;
int permutations;
//Variables
//Static variables
static int desiredValue; //The value of coins we want
//Instance variables
int permutations; //The number of permutations that are found
public:
//Constructor
Problem31();
virtual void solve();
virtual std::string getString() const;
virtual void reset();
//Returns the number of correct permutations of the coins
int getPermutations() const;
//Operational functions
virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again
//Gets
virtual std::string getString() const; //Return a string with the solution to the problem
int getPermutations() const; //Returns the number of correct permutations of the coins
};
/* Results:
There are 73682 ways to make 2 pounds with the given denominations of coins
It took 0.000 nanoseconds to solve this problem.
It took an average of 1.916 microseconds to run this problem over 100 iterations
*/

View File

@@ -1,11 +1,11 @@
//ProjectEuler/C++/Headers/Problem4.hpp
//ProjectEuler/ProjectEulerCPP/Headers/Problem4.hpp
//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
@@ -32,24 +32,28 @@
class Problem4 : public Problem{
private:
static int START_NUM;
static int END_NUM;
//Variables
//Static variables
static int START_NUM; //The first number to check
static int END_NUM; //The last number to check
//Instance variables
std::vector<uint64_t> palindromes; //Holds all numbers that turn out to be palindromes
public:
//Constructor
Problem4();
virtual void solve();
virtual std::string getString() const;
virtual void reset();
//Returns the list of all palindromes
std::vector<uint64_t> getPalindromes() const;
//Returns the largest palindrome
uint64_t getLargestPalindrome() const;
//Operational functions
virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again
//Gets
virtual std::string getString() const; //Return a string with the solution to the problem
std::vector<uint64_t> getPalindromes() const; //Returns the list of all palindromes
uint64_t getLargestPalindrome() const; //Returns the largest palindrome
};
/* Results:
The largest palindrome is 906609
It took 31.904 milliseconds to solve this problem.
It took an average of 36.525 milliseconds to run this problem over 100 iterations
*/
#endif //PROBLEM4_HPP

View File

@@ -1,11 +1,11 @@
//ProjectEuler/C++/Headers/Problem5.hpp
//ProjectEuler/ProjectEulerCPP/Headers/Problem5.hpp
//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
@@ -33,19 +33,23 @@
class Problem5 : public Problem{
private:
int smallestNum;
//Variables
//Instance variables
int smallestNum; //The smallest number that is found
public:
//Constructor
Problem5();
virtual void solve();
virtual std::string getString() const;
virtual void reset();
//Returns the requested number
int getNumber() const;
//Operational functions
virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again
//Gets
virtual std::string getString() const; //Return a string with the solution to the problem
int getNumber() const; //Returns the requested number
};
/* Results:
The smallest positive number evenly divisible by all numbers 1-20 is 232792560
It took 748.245 milliseconds to run this algorithm
It took an average of 1.928 seconds to run this problem over 100 iterations
*/
#endif //PROBLEM5_HPP

View File

@@ -1,11 +1,11 @@
//ProjectEuler/C++/Headers/Problem6.hpp
//ProjectEuler/ProjectEulerCPP/Headers/Problem6.hpp
//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
@@ -32,26 +32,29 @@
class Problem6 : public Problem{
private:
static int START_NUM;
static int END_NUM;
//Variables
//Static variables
static int START_NUM; //The first number to check
static int END_NUM; //The last number to check
//Instance variables
uint64_t sumOfSquares; //Holds the sum of the squares of all the numbers
uint64_t squareOfSum; //Holds the square of the sum of all the numbers
public:
//Constructor
Problem6();
virtual void solve();
virtual std::string getString() const;
virtual void reset();
//Returns the sum of all the squares
uint64_t getSumOfSquares() const;
//Returns the square of all of the sums
uint64_t getSquareOfSum() const;
//Returns the requested difference
uint64_t getDifference() const;
//Operational functinos
virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again
//Gets
virtual std::string getString() const; //Return a string with the solution to the problem
uint64_t getSumOfSquares() const; //Returns the sum of all the squares
uint64_t getSquareOfSum() const; //Returns the square of all of the sums
uint64_t getDifference() const; //Returns the requested difference
};
/* Result
The difference between the sum of the squares and the square of the sum of all numbers from 1-100 is 25164150
It took 1.000 microseconds to solve this problem.
It took an average of 76.000 nanoseconds to run this problem over 100 iterations
*/
#endif //PROBLEM6_HPP

View File

@@ -1,7 +1,7 @@
//ProjectEuler/C++/Headers/Problem67.hpp
//ProjectEuler/ProjectEulerCPP/Headers/Problem67.hpp
//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
@@ -136,6 +136,7 @@ Find the maximum total from top to bottom
class Problem67 : public Problem{
private:
//Structures
struct location{
int xLocation;
int yLocation;
@@ -143,18 +144,25 @@ private:
bool fromRight;
location(int x, int y, int t, bool r) : xLocation(x), yLocation(y), total(t), fromRight(r){ }
};
void invert(); //This function takes every number in the vector and changes it to 100 - the number
//Variables
//Static variables
static const int NUM_ROWS = 100; //The number of rows in the list of numbers
static std::vector<int> list[NUM_ROWS]; //This is the list you are trying to find a path through
//Instance variables
std::list<location> foundPoints; //For the points that I have already found the shortest distance to
std::list<location> possiblePoints; //For the locations you are checking this round
int actualTotal; //The true total of the path from the top to the bottom
//This is the list you are trying to find a path through
static std::vector<int> list[NUM_ROWS];
//Functions
void invert(); //This function takes every number in the vector and changes it to 100 - the number
public:
//Constructor
Problem67();
virtual void solve();
virtual std::string getString() const;
virtual void reset();
//Operational functions
virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again
//Gets
virtual std::string getString() const; //Return a string with the solution to the problem
std::string getPyramid() const; //Returns the pyramid that was traversed as a string
std::string getTrail(); //Returns the trail the algorithm took as a string
int getTotal() const; //Returns the total that was asked for
@@ -162,7 +170,7 @@ public:
/* Results:
The value of the longest path is 7273
It took 208.728 milliseconds to solve this problem.
It took an average of 366.373 milliseconds to run this problem over 100 iterations
*/
#endif //PROBLEM67_HPP

View File

@@ -1,11 +1,11 @@
//ProjectEuler/C++/Headers/Problem7.hpp
//ProjectEuler/ProjectEulerCPP/Headers/Problem7.hpp
//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
@@ -33,20 +33,25 @@
class Problem7: public Problem{
private:
static uint64_t NUMBER_OF_PRIMES;
//Variables
//Static variables
static uint64_t NUMBER_OF_PRIMES; //The index of the prime number to find
//Instance variables
std::vector<uint64_t> primes; //Holds the prime numbers
public:
//Constructor
Problem7();
virtual void solve();
virtual std::string getString() const;
virtual void reset();
//Returns the requested prime number
uint64_t getPrime() const;
//Operational functions
virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again
//Gets
virtual std::string getString() const; //Return a string with the solution to the problem
uint64_t getPrime() const; //Returns the requested prime number
};
/* Results
The 10001th prime number is 104743
It took 3.551 milliseconds to solve this problem.
It took an average of 3.864 milliseconds to run this problem over 100 iterations
*/
#endif //PROBLEM7_HPP

View File

@@ -1,7 +1,7 @@
//ProjectEuler/C++/Headers/Problem8.hpp
//ProjectEuler/ProjectEulerCPP/Headers/Problem8.hpp
//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
@@ -55,25 +55,29 @@
class Problem8 : public Problem{
private:
static std::string number;
//Variables
//Static variables
static std::string number; //The number that we are working with
//Instance variables
std::string maxNums; //Holds the string of the largest product
uint64_t maxProduct; //Holds the largest product of 13 numbers
public:
//Constructor
Problem8();
virtual void solve();
virtual std::string getString() const;
virtual void reset();
//Returns the string of numbers that produces the largest product
std::string getLargestNums() const;
//Returns the requested product
uint64_t getLargestProduct() const;
//Operational functions
virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again
//Gets
virtual std::string getString() const; //Return a string with the solution to the problem
std::string getLargestNums() const; //Returns the string of numbers that produces the largest product
uint64_t getLargestProduct() const; //Returns the requested product
};
/* Results
The greatest product is 23514624000
The numbers are 5576689664895
It took 124.900 microseconds to solve this problem.
It took an average of 129.024 microseconds to run this problem over 100 iterations
*/
#endif //PROBLEM8_HPP

View File

@@ -1,11 +1,11 @@
//ProjectEuler/C++/Headers/Problem9.hpp
//ProjectEuler/ProjectEulerCPP/Headers/Problem9.hpp
//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
@@ -32,29 +32,30 @@
class Problem9 : public Problem{
private:
int a; //Holds the position on the first side
int b; //Holds the position on the second side
//Variables
//Instance variables
int a; //Holds the position on the first side
int b; //Holds the position on the second side
double c; //Holds the hyp
bool found; //A flag to determine if we have found the solution yet
public:
//Constructor
Problem9();
virtual void solve();
virtual std::string getString() const;
virtual void reset();
//Returns the length of the first side
int getSideA() const;
//Returns the length of the second side
int getSideB() const;
//Returns the length of the hyp
int getSideC() const;
//Returns the product of the 3 sides
int getProduct() const;
//Operational functions
virtual void solve(); //Solve the problem
virtual void reset(); //Reset the problem so it can be run again
//Gets
virtual std::string getString() const; //Return a string with the solution to the problem
int getSideA() const; //Returns the length of the first side
int getSideB() const; //Returns the length of the second side
int getSideC() const; //Returns the length of the hyp
int getProduct() const; //Returns the product of the 3 sides
};
/* Results:
The Pythagorean triplet is 200 375 425
The numbers' product is 31875000
It took 0.000 nanoseconds to solve this problem.
It took an average of 154.595 microseconds to run this problem over 100 iterations
*/
#endif //PROBLEM9_HPP

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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();
}

View File

@@ -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;
}

View File

@@ -1,7 +1,7 @@
//ProjectEuler/C++/Source/Problem13.cpp
//ProjectEuler/ProjectEulerCPP/Source/Problem13.cpp
//Matthew Ellison
// Created: 09-29-18
//Modified: 07-14-19
//Modified: 07-09-20
//Work out the first ten digits of the sum of the following one-hundred 50-digit numbers
/*
37107287533902102798797998220837590246510135740250
@@ -110,7 +110,7 @@
//You can find more information about them at https://gmplib.org/
//When compiling this file you need to have the gmp library installed as well as linking the libraries to your executable using the -lgmpxx and -lgmp flags
/*
Copyright (C) 2019 Matthew Ellison
Copyright (C) 2020 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -138,16 +138,7 @@
#include "../Headers/Problem13.hpp"
Problem13::Problem13() : Problem("Work out the first ten digits of the sum of the one-hundred 50-digit numbers"), sum(0){
reserveVectors();
}
void Problem13::reserveVectors(){
//Make sure the vector is the correct size
nums.reserve(100);
nums.resize(100);
}
//A function to set the nums vector
void Problem13::setNums(){
//Set the numbers
nums[0] = "37107287533902102798797998220837590246510135740250";
@@ -252,6 +243,19 @@ void Problem13::setNums(){
nums[99] = "53503534226472524250874054075591789781264330331690";
}
//Reserve the size of the vector to speed up insertion
void Problem13::reserveVectors(){
//Make sure the vector is the correct size
nums.reserve(100);
nums.resize(100);
}
//Constructor
Problem13::Problem13() : Problem("Work out the first ten digits of the sum of the one-hundred 50-digit numbers"), sum(0){
reserveVectors();
}
//Solve the problem
void Problem13::solve(){
//If the problem has already been solved do nothing and end the function
if(solved){
@@ -274,6 +278,15 @@ void Problem13::solve(){
solved = true;
}
//Reset the problem so it can be run again
void Problem13::reset(){
Problem::reset();
sum = 0;
nums.clear();
reserveVectors();
}
//Return a string with the solution to the problem
std::string Problem13::getString() const{
//If the problem hasn't been solved throw an exception
if(!solved){
@@ -286,6 +299,7 @@ std::string Problem13::getString() const{
return results.str();
}
//Returns the list 50-digit numbers
std::vector<mpz_class> Problem13::getNumbers() const{
//If the problem hasn't been solved throw an exception
if(!solved){
@@ -294,6 +308,7 @@ std::vector<mpz_class> Problem13::getNumbers() const{
return nums;
}
//Returns the sum of the 50-digit numbers
mpz_class Problem13::getSum() const{
//If the problem hasn't been solved throw an exception
if(!solved){
@@ -301,10 +316,3 @@ mpz_class Problem13::getSum() const{
}
return sum;
}
void Problem13::reset(){
Problem::reset();
sum = 0;
nums.clear();
reserveVectors();
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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();
}

View File

@@ -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();
}

View File

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

View File

@@ -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();
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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();
}

View File

@@ -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();
}

View File

@@ -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();
}

View File

@@ -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();
}

View File

@@ -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();
}

View File

@@ -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;
}

View File

@@ -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();
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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();
}

View File

@@ -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;
}

View File

@@ -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;
}