Updated to be more standardized

This commit is contained in:
2019-02-27 18:09:16 -05:00
parent 84cbf6d5af
commit bb8810326f
2 changed files with 79 additions and 75 deletions

View File

@@ -28,13 +28,6 @@
#include <algorithm> #include <algorithm>
#include <string> #include <string>
#include <cmath> #include <cmath>
//This library is licensed under lgplv3
//You can find more information at gmplib.org
//In order to use this functionality you must use the -DNEEDGMP flag in your compiler
//You must also link both libgmpxx and libgmp
#ifdef NEEDGMP
#include <gmpxx.h> //This is necessary for the getGmpFib function for numbers larger than a normal int can hold. It can be commented out if needed
#endif //NEEDGMP
namespace mee{ namespace mee{
@@ -42,54 +35,55 @@ namespace mee{
//A list of functions in the file //A list of functions in the file
//Also works as prototypes with general information //Also works as prototypes with general information
//This is a function that returns all the primes <= goalNumber and returns a vector with those prime numbers //This is a function that returns all the primes <= goalNumber and returns a vector with those prime numbers
template<class T> template <class T>
std::vector<T> getPrimes(T goalNumber); std::vector<T> getPrimes(T goalNumber);
//This function returns a vector with a specific number of primes //This function returns a vector with a specific number of primes
template<class T> template <class T>
std::vector<T> getNumPrimes(T numberOfPrimes); std::vector<T> getNumPrimes(T numberOfPrimes);
//This function returns all prime factors of a number //This function returns all prime factors of a number
template<class T> template <class T>
std::vector<T> getFactors(T goalNumber); std::vector<T> getFactors(T goalNumber);
//This is a function that gets all the divisors of num and returns a vector containing the divisors //This is a function that gets all the divisors of num and returns a vector containing the divisors
template<class T> template <class T>
std::vector<T> getDivisors(T num); std::vector<T> getDivisors(T num);
//This is a function that returns the sum of all elements in a vector //This is a function that returns the sum of all elements in a vector
template <class T> template <class T>
T getSum(std::vector<T> numbers); T getSum(const std::vector<T>& numbers);
//This is a function that returns the product of all elements in a vector //This is a function that returns the product of all elements in a vector
template <class T> template <class T>
T getProduct(std::vector<T> nums); T getProduct(const std::vector<T>& nums);
//This is a function that searches a vecter for an element. Returns true if num is found in list //This is a function that searches a vecter for an element. Returns true if the key is found in list
template<class T> template <class T>
bool isFound(T num, std::vector<T> list); bool isFound(std::vector<T> ary, T key);
//This is a function that creates all permutations of a string and returns a vector of those permutations. //This is a function that creates all permutations of a string and returns a vector of those permutations.
//It is meant to have only the string passed into it from the calling function. num is used for recursion purposes //It is meant to have only the string passed into it from the calling function. num is used for recursion purposes
//It can however be used with num if you want the first num characters to be stationary //It can however be used with num if you want the first num characters to be stationary
std::vector<std::string> getPermutations(std::string master, int num = 0); std::vector<std::string> getPermutations(std::string master, int num = 0);
//These functions return the numth Fibonacci number //These functions return the numth Fibonacci number
template <class T> template <class T>
T getFib(T num); T getFib(const T num);
//This function returns a vector that includes all Fibonacci numbers <= num //This function returns a vector that includes all Fibonacci numbers <= num
template<class T> template <class T>
std::vector<T> getAllFib(const T num); std::vector<T> getAllFib(const T num);
//This is a function that performs a bubble sort on a vector //This is a function that performs a bubble sort on a vector
template<class T> template <class T>
bool bubbleSort(std::vector<T> nums); void bubbleSort(std::vector<T>& ary);
//This is a function that perfomrs a quick sort on a vector //This is a function that perfomrs a quick sort on a vector
template<class T> template <class T>
bool quickSort(std::vector<T> nums); void quickSort(std::vector<T>& ary, int bottom = -1, int top = -1);
//This is a function that performs a search on a vector and returns the subscript of the item being searched for (-1 if not found) //This is a function that performs a search on a vector and returns the subscript of the item being searched for (-1 if not found)
template<class T> template <class T>
int64_t search(std::vector<T> nums, T num); int64_t search(const std::vector<T>& ary, T num);
//This function finds the smallest element in a vector //This function finds the smallest element in a vector
template<class T> template <class T>
T findMin(std::vector<T> arr); T findMin(const std::vector<T>& ary);
//This function finds the largest element in a vector //This function finds the largest element in a vector
template<class T> template <class T>
T findMax(std::vector<T> arr); T findMax(const std::vector<T>& ary);
template<class T> //This is a function that returns all the primes <= goalNumber and returns a vector with those prime numbers
template <class T>
std::vector<T> getPrimes(T goalNumber){ std::vector<T> getPrimes(T goalNumber){
std::vector<T> primes; std::vector<T> primes;
bool foundFactor = false; bool foundFactor = false;
@@ -124,7 +118,8 @@ std::vector<T> getPrimes(T goalNumber){
return primes; return primes;
} }
template<class T> //This function returns a vector with a specific number of primes
template <class T>
std::vector<T> getNumPrimes(T numberOfPrimes){ std::vector<T> getNumPrimes(T numberOfPrimes){
std::vector<T> primes; std::vector<T> primes;
primes.reserve(numberOfPrimes); //Saves cycles later primes.reserve(numberOfPrimes); //Saves cycles later
@@ -165,7 +160,8 @@ std::vector<T> getNumPrimes(T numberOfPrimes){
return primes; return primes;
} }
template<class T> //This function returns all prime factors of a number
template <class T>
std::vector<T> getFactors(T goalNumber){ std::vector<T> getFactors(T goalNumber){
//Get all the prime numbers up to sqrt(number). If there is a prime < goalNumber it will have to be <= sqrt(goalNumber) //Get all the prime numbers up to sqrt(number). If there is a prime < goalNumber it will have to be <= sqrt(goalNumber)
std::vector<T> primes = getPrimes((T)ceil(sqrt(goalNumber))); //Make sure you are getting a vector of the correct type std::vector<T> primes = getPrimes((T)ceil(sqrt(goalNumber))); //Make sure you are getting a vector of the correct type
@@ -193,7 +189,8 @@ std::vector<T> getFactors(T goalNumber){
return factors; return factors;
} }
template<class T> //This is a function that gets all the divisors of num and returns a vector containing the divisors
template <class T>
std::vector<T> getDivisors(T num){ std::vector<T> getDivisors(T num){
std::vector<T> divisors; //Holds the number of divisors std::vector<T> divisors; //Holds the number of divisors
//Ensure the parameter is a valid number //Ensure the parameter is a valid number
@@ -230,34 +227,36 @@ std::vector<T> getDivisors(T num){
//This is a function that returns the sum of all elements in a vector //This is a function that returns the sum of all elements in a vector
template <class T> template <class T>
T getSum(std::vector<T> numbers){ T getSum(const std::vector<T>& ary){
T sum = 0; T sum = 0;
for(unsigned int cnt = 0;cnt < numbers.size();++cnt){ for(unsigned int cnt = 0;cnt < ary.size();++cnt){
sum += numbers.at(cnt); sum += ary.at(cnt);
} }
return sum; return sum;
} }
//This is a function that returns the product of all elmements in a vector //This is a function that returns the product of all elmements in a vector
template <class T> template <class T>
T getProduct(std::vector<T> nums){ T getProduct(const std::vector<T>& ary){
T prod = 1; T prod = 1;
for(T cnt = 0;cnt < nums.size();++cnt){ for(T cnt = 0;cnt < ary.size();++cnt){
prod *= nums.at(cnt); prod *= ary.at(cnt);
} }
return prod; return prod;
} }
template<class T> //This is a function that searches a vecter for an element. Returns true if they key is found in list
bool isFound(T num, std::vector<T> list){ template <class T>
for(int cnt = 0;cnt < list.size();++cnt){ bool isFound(std::vector<T> ary, T key){
if(list.at(cnt) == num){ for(int cnt = 0;cnt < ary.size();++cnt){
if(ary.at(cnt) == key){
return true; return true;
} }
} }
return false; return false;
} }
//This is a function that creates all permutations of a string and returns a vector of those permutations.
std::vector<std::string> getPermutations(std::string master, int num){ std::vector<std::string> getPermutations(std::string master, int num){
std::vector<std::string> perms; std::vector<std::string> perms;
//Check if the number is out of bounds //Check if the number is out of bounds
@@ -289,8 +288,9 @@ std::vector<std::string> getPermutations(std::string master, int num){
return perms; return perms;
} }
//These functions return the numth Fibonacci number
template <class T> template <class T>
T getFib(T num){ T getFib(const T num){
//Make sure the number is within bounds //Make sure the number is within bounds
if(num <= 2){ if(num <= 2){
return 1; return 1;
@@ -301,7 +301,7 @@ T getFib(T num){
tempNums[0] = tempNums[1] = 1; tempNums[0] = tempNums[1] = 1;
//Do the calculation //Do the calculation
uint64_t cnt; unsigned int cnt;
for(cnt = 2;(cnt < num) && (tempNums[(cnt - 1) % 3] >= tempNums[(cnt - 2) % 3]);++cnt){ for(cnt = 2;(cnt < num) && (tempNums[(cnt - 1) % 3] >= tempNums[(cnt - 2) % 3]);++cnt){
tempNums[cnt % 3] = tempNums[(cnt + 1) % 3] + tempNums[(cnt + 2) % 3]; tempNums[cnt % 3] = tempNums[(cnt + 1) % 3] + tempNums[(cnt + 2) % 3];
} }
@@ -341,14 +341,14 @@ std::vector<T> getAllFib(const T num){
//This is a function that performs a bubble sort on a vector //This is a function that performs a bubble sort on a vector
template<class T> template<class T>
bool bubbleSort(std::vector<T> nums){ void bubbleSort(std::vector<T>& ary){
bool notFinished = true; //A flag to determine if the loop is finished bool notFinished = true; //A flag to determine if the loop is finished
for(int numLoops = 0;notFinished;++numLoops){ //Loop until you finish for(int numLoops = 0;numLoops < ary.size();++numLoops){ //Loop until you finish
notFinished = false; //Assume you are finished until you find an element out of order notFinished = false; //Assume you are finished until you find an element out of order
//Loop through every element in the vector, moving the largest one to the end //Loop through every element in the vector, moving the largest one to the end
for(int cnt = 0;cnt < ((nums.size() - 1) - numLoops);++cnt){ //use size - 1 to make sure you don't go out of bounds for(int cnt = 1;cnt < (ary.size() - numLoops);++cnt){ //use size - 1 to make sure you don't go out of bounds
if(nums.at(cnt) > nums.at(cnt + 1)){ if(ary.at(cnt) < ary.at(cnt - 1)){
std::swap(nums.at(cnt), nums.at(cnt + 1)); std::swap(ary.at(cnt), ary.at(cnt - 1));
notFinished = true; notFinished = true;
} }
} }
@@ -357,19 +357,22 @@ bool bubbleSort(std::vector<T> nums){
//This is a function that perfomrs a quick sort on a vector //This is a function that perfomrs a quick sort on a vector
template<class T> template<class T>
bool quickSort(std::vector<T> nums){ void quickSort(std::vector<T>& nums, int bottom, int top){
} }
//This is a function that performs a search on a vector and returns the subscript of the item being searched for //This is a function that performs a search on a vector and returns the subscript of the item being searched for
template<class T> template<class T>
int64_t search(std::vector<T> nums, T num){ int64_t search(const std::vector<T>& ary, T num){
int64_t subscript = 0; //Start with the subscript at 0 int64_t subscript = 0; //Start with the subscript at 0
//Step through every element in the vector and return the subscript if you find the correct element //Step through every element in the vector and return the subscript if you find the correct element
while(subscript < nums.size()){ while(subscript < ary.size()){
if(nums.at(subscript) == num){ if(ary.at(subscript) == num){
return subscript; return subscript;
} }
else{
++subscript;
}
} }
//If you cannot find the element return -1 //If you cannot find the element return -1
return -1; return -1;
@@ -377,18 +380,18 @@ int64_t search(std::vector<T> nums, T num){
//This function finds the smallest element in a vector //This function finds the smallest element in a vector
template<class T> template<class T>
T findMin(std::vector<T> arr){ T findMin(const std::vector<T>& ary){
T min; //For the smallest element T min; //For the smallest element
//Make sure the vector is not empty //Make sure the vector is not empty
if(arr.size() > 0){ if(ary.size() > 0){
//Use the first element as the smallest element //Use the first element as the smallest element
min = arr.at(0); min = ary.at(0);
//Run through every element in the vector, checking it against the current minimum //Run through every element in the vector, checking it against the current minimum
for(int cnt = 1;cnt < arr.size();++cnt){ for(int cnt = 1;cnt < ary.size();++cnt){
//If the current element is smaller than the minimum, make it the new minimum //If the current element is smaller than the minimum, make it the new minimum
if(arr.at(cnt) < min){ if(ary.at(cnt) < min){
min = arr.at(cnt); min = ary.at(cnt);
} }
} }
} }
@@ -399,18 +402,18 @@ T findMin(std::vector<T> arr){
//This function finds the largest element in a vector //This function finds the largest element in a vector
template<class T> template<class T>
T findMax(std::vector<T> arr){ T findMax(const std::vector<T>& ary){
T max; //For the largest element T max; //For the largest element
//Make sure the vector is not empty //Make sure the vector is not empty
if(arr.size() > 0){ if(ary.size() > 0){
//Use the first element as the largest element //Use the first element as the largest element
max = arr.at(0); max = ary.at(0);
//Run through every element in the vector, checking it against the current minimum //Run through every element in the vector, checking it against the current minimum
for(int cnt = 1;cnt < arr.size();++cnt){ for(int cnt = 1;cnt < ary.size();++cnt){
//If the current element is larger than the maximum, make it the new maximum //If the current element is larger than the maximum, make it the new maximum
if(arr.at(cnt) > max){ if(ary.at(cnt) > max){
max = arr.at(cnt); max = ary.at(cnt);
} }
} }
} }

View File

@@ -114,14 +114,14 @@ bool testIsFound(){
bool correctAnswer = true; bool correctAnswer = true;
std::vector<uint64_t> testVector = mee::getPrimes((uint64_t)100); std::vector<uint64_t> testVector = mee::getPrimes((uint64_t)100);
uint64_t searchFor = 79; uint64_t searchFor = 79;
bool answer = mee::isFound(searchFor, testVector); bool answer = mee::isFound(testVector, searchFor);
if(answer != correctAnswer){ if(answer != correctAnswer){
std::cout << "isFound() is failed at test 1" << std::endl; std::cout << "isFound() is failed at test 1" << std::endl;
return false; return false;
} }
searchFor = 97; searchFor = 97;
answer = mee::isFound(searchFor, testVector); answer = mee::isFound(testVector, searchFor);
if(answer != correctAnswer){ if(answer != correctAnswer){
std::cout << "isFound() is failed at test 2" << std::endl; std::cout << "isFound() is failed at test 2" << std::endl;
return false; return false;
@@ -129,7 +129,7 @@ bool testIsFound(){
searchFor = 88; searchFor = 88;
correctAnswer = false; correctAnswer = false;
answer = mee::isFound(searchFor, testVector); answer = mee::isFound(testVector, searchFor);
if(answer != correctAnswer){ if(answer != correctAnswer){
std::cout << "isFound() is failed at test 3" << std::endl; std::cout << "isFound() is failed at test 3" << std::endl;
return false; return false;
@@ -171,19 +171,19 @@ bool testGetFib(){
//Test the gmp integer function //Test the gmp integer function
number = 12; mpz_class mpzNumber = 12;
mpz_class longCorrectAnswer = 144; mpz_class longCorrectAnswer = 144;
mpz_class longAnswer = mee::getMpzFib(number); mpz_class longAnswer = mee::getFib(mpzNumber);
if(longCorrectAnswer != longAnswer){ if(longCorrectAnswer != longAnswer){
std::cout << "getMpzFib() failed at test 3" << std::endl; std::cout << "getFib() for mpz failed at test 3" << std::endl;
return false; return false;
} }
number = 4782; mpzNumber = 4782;
longCorrectAnswer = "1070066266382758936764980584457396885083683896632151665013235203375314520604694040621889147582489792657804694888177591957484336466672569959512996030461262748092482186144069433051234774442750273781753087579391666192149259186759553966422837148943113074699503439547001985432609723067290192870526447243726117715821825548491120525013201478612965931381792235559657452039506137551467837543229119602129934048260706175397706847068202895486902666185435124521900369480641357447470911707619766945691070098024393439617474103736912503231365532164773697023167755051595173518460579954919410967778373229665796581646513903488154256310184224190259846088000110186255550245493937113651657039447629584714548523425950428582425306083544435428212611008992863795048006894330309773217834864543113205765659868456288616808718693835297350643986297640660000723562917905207051164077614812491885830945940566688339109350944456576357666151619317753792891661581327159616877487983821820492520348473874384736771934512787029218636250627816"; longCorrectAnswer = "1070066266382758936764980584457396885083683896632151665013235203375314520604694040621889147582489792657804694888177591957484336466672569959512996030461262748092482186144069433051234774442750273781753087579391666192149259186759553966422837148943113074699503439547001985432609723067290192870526447243726117715821825548491120525013201478612965931381792235559657452039506137551467837543229119602129934048260706175397706847068202895486902666185435124521900369480641357447470911707619766945691070098024393439617474103736912503231365532164773697023167755051595173518460579954919410967778373229665796581646513903488154256310184224190259846088000110186255550245493937113651657039447629584714548523425950428582425306083544435428212611008992863795048006894330309773217834864543113205765659868456288616808718693835297350643986297640660000723562917905207051164077614812491885830945940566688339109350944456576357666151619317753792891661581327159616877487983821820492520348473874384736771934512787029218636250627816";
longAnswer = mee::getMpzFib(number); longAnswer = mee::getFib(mpzNumber);
if(longCorrectAnswer != longAnswer){ if(longCorrectAnswer != longAnswer){
std::cout << "getMpzFib() failed at test 4" << std::endl; std::cout << "getFib() for mpzfailed at test 4" << std::endl;
return false; return false;
} }
@@ -210,6 +210,7 @@ bool testBubbleSort(){
for(int cnt = 1;cnt < nums.size();++cnt){ for(int cnt = 1;cnt < nums.size();++cnt){
if(nums.at(cnt) < nums.at(cnt - 1)){ if(nums.at(cnt) < nums.at(cnt - 1)){
return false; return false;
std::cout << "nums.size() " << nums.size() << "\ncnt " << cnt << std::endl;
} }
} }
@@ -243,7 +244,7 @@ bool testQuickSort(){
} }
bool testSearch(){ bool testSearch(){
bool found = false; int64_t found = -1;
//Create a vector of numbers //Create a vector of numbers
std::vector<int> nums {1, 20, 3, 40, 5, 60, 7, 80, 9}; std::vector<int> nums {1, 20, 3, 40, 5, 60, 7, 80, 9};
//Search for one that is in the vector //Search for one that is in the vector