mirror of
https://bitbucket.org/Mattrixwv/projecteulercpp.git
synced 2025-12-06 17:13:59 -05:00
Updated comments and made sure style was consistent
This commit is contained in:
@@ -1,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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user