diff --git a/Algorithms.hpp b/Algorithms.hpp index 0a9b8bc..c2956dd 100644 --- a/Algorithms.hpp +++ b/Algorithms.hpp @@ -28,13 +28,6 @@ #include #include #include -//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 //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{ @@ -42,54 +35,55 @@ namespace mee{ //A list of functions in the file //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 -template +template std::vector getPrimes(T goalNumber); //This function returns a vector with a specific number of primes -template +template std::vector getNumPrimes(T numberOfPrimes); //This function returns all prime factors of a number -template +template std::vector getFactors(T goalNumber); //This is a function that gets all the divisors of num and returns a vector containing the divisors -template +template std::vector getDivisors(T num); //This is a function that returns the sum of all elements in a vector template -T getSum(std::vector numbers); +T getSum(const std::vector& numbers); //This is a function that returns the product of all elements in a vector template -T getProduct(std::vector nums); -//This is a function that searches a vecter for an element. Returns true if num is found in list -template -bool isFound(T num, std::vector list); +T getProduct(const std::vector& nums); +//This is a function that searches a vecter for an element. Returns true if the key is found in list +template +bool isFound(std::vector ary, T key); //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 can however be used with num if you want the first num characters to be stationary std::vector getPermutations(std::string master, int num = 0); //These functions return the numth Fibonacci number template -T getFib(T num); +T getFib(const T num); //This function returns a vector that includes all Fibonacci numbers <= num -template +template std::vector getAllFib(const T num); //This is a function that performs a bubble sort on a vector -template -bool bubbleSort(std::vector nums); +template +void bubbleSort(std::vector& ary); //This is a function that perfomrs a quick sort on a vector -template -bool quickSort(std::vector nums); +template +void quickSort(std::vector& 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) -template -int64_t search(std::vector nums, T num); +template +int64_t search(const std::vector& ary, T num); //This function finds the smallest element in a vector -template -T findMin(std::vector arr); +template +T findMin(const std::vector& ary); //This function finds the largest element in a vector -template -T findMax(std::vector arr); +template +T findMax(const std::vector& ary); -template +//This is a function that returns all the primes <= goalNumber and returns a vector with those prime numbers +template std::vector getPrimes(T goalNumber){ std::vector primes; bool foundFactor = false; @@ -124,7 +118,8 @@ std::vector getPrimes(T goalNumber){ return primes; } -template +//This function returns a vector with a specific number of primes +template std::vector getNumPrimes(T numberOfPrimes){ std::vector primes; primes.reserve(numberOfPrimes); //Saves cycles later @@ -165,7 +160,8 @@ std::vector getNumPrimes(T numberOfPrimes){ return primes; } -template +//This function returns all prime factors of a number +template std::vector 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) std::vector primes = getPrimes((T)ceil(sqrt(goalNumber))); //Make sure you are getting a vector of the correct type @@ -193,7 +189,8 @@ std::vector getFactors(T goalNumber){ return factors; } -template +//This is a function that gets all the divisors of num and returns a vector containing the divisors +template std::vector getDivisors(T num){ std::vector divisors; //Holds the number of divisors //Ensure the parameter is a valid number @@ -230,34 +227,36 @@ std::vector getDivisors(T num){ //This is a function that returns the sum of all elements in a vector template -T getSum(std::vector numbers){ +T getSum(const std::vector& ary){ T sum = 0; - for(unsigned int cnt = 0;cnt < numbers.size();++cnt){ - sum += numbers.at(cnt); + for(unsigned int cnt = 0;cnt < ary.size();++cnt){ + sum += ary.at(cnt); } return sum; } //This is a function that returns the product of all elmements in a vector template -T getProduct(std::vector nums){ +T getProduct(const std::vector& ary){ T prod = 1; - for(T cnt = 0;cnt < nums.size();++cnt){ - prod *= nums.at(cnt); + for(T cnt = 0;cnt < ary.size();++cnt){ + prod *= ary.at(cnt); } return prod; } -template -bool isFound(T num, std::vector list){ - for(int cnt = 0;cnt < list.size();++cnt){ - if(list.at(cnt) == num){ +//This is a function that searches a vecter for an element. Returns true if they key is found in list +template +bool isFound(std::vector ary, T key){ + for(int cnt = 0;cnt < ary.size();++cnt){ + if(ary.at(cnt) == key){ return true; } } return false; } +//This is a function that creates all permutations of a string and returns a vector of those permutations. std::vector getPermutations(std::string master, int num){ std::vector perms; //Check if the number is out of bounds @@ -289,8 +288,9 @@ std::vector getPermutations(std::string master, int num){ return perms; } +//These functions return the numth Fibonacci number template -T getFib(T num){ +T getFib(const T num){ //Make sure the number is within bounds if(num <= 2){ return 1; @@ -301,7 +301,7 @@ T getFib(T num){ tempNums[0] = tempNums[1] = 1; //Do the calculation - uint64_t cnt; + unsigned int 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]; } @@ -341,14 +341,14 @@ std::vector getAllFib(const T num){ //This is a function that performs a bubble sort on a vector template -bool bubbleSort(std::vector nums){ +void bubbleSort(std::vector& ary){ 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 //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 - if(nums.at(cnt) > nums.at(cnt + 1)){ - std::swap(nums.at(cnt), nums.at(cnt + 1)); + for(int cnt = 1;cnt < (ary.size() - numLoops);++cnt){ //use size - 1 to make sure you don't go out of bounds + if(ary.at(cnt) < ary.at(cnt - 1)){ + std::swap(ary.at(cnt), ary.at(cnt - 1)); notFinished = true; } } @@ -357,19 +357,22 @@ bool bubbleSort(std::vector nums){ //This is a function that perfomrs a quick sort on a vector template -bool quickSort(std::vector nums){ +void quickSort(std::vector& 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 template -int64_t search(std::vector nums, T num){ +int64_t search(const std::vector& ary, T num){ 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 - while(subscript < nums.size()){ - if(nums.at(subscript) == num){ + while(subscript < ary.size()){ + if(ary.at(subscript) == num){ return subscript; } + else{ + ++subscript; + } } //If you cannot find the element return -1 return -1; @@ -377,18 +380,18 @@ int64_t search(std::vector nums, T num){ //This function finds the smallest element in a vector template -T findMin(std::vector arr){ +T findMin(const std::vector& ary){ T min; //For the smallest element //Make sure the vector is not empty - if(arr.size() > 0){ + if(ary.size() > 0){ //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 - 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(arr.at(cnt) < min){ - min = arr.at(cnt); + if(ary.at(cnt) < min){ + min = ary.at(cnt); } } } @@ -399,18 +402,18 @@ T findMin(std::vector arr){ //This function finds the largest element in a vector template -T findMax(std::vector arr){ +T findMax(const std::vector& ary){ T max; //For the largest element //Make sure the vector is not empty - if(arr.size() > 0){ + if(ary.size() > 0){ //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 - 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(arr.at(cnt) > max){ - max = arr.at(cnt); + if(ary.at(cnt) > max){ + max = ary.at(cnt); } } } diff --git a/testAlgorithms.cpp b/testAlgorithms.cpp index c781693..9ea3fee 100644 --- a/testAlgorithms.cpp +++ b/testAlgorithms.cpp @@ -114,14 +114,14 @@ bool testIsFound(){ bool correctAnswer = true; std::vector testVector = mee::getPrimes((uint64_t)100); uint64_t searchFor = 79; - bool answer = mee::isFound(searchFor, testVector); + bool answer = mee::isFound(testVector, searchFor); if(answer != correctAnswer){ std::cout << "isFound() is failed at test 1" << std::endl; return false; } searchFor = 97; - answer = mee::isFound(searchFor, testVector); + answer = mee::isFound(testVector, searchFor); if(answer != correctAnswer){ std::cout << "isFound() is failed at test 2" << std::endl; return false; @@ -129,7 +129,7 @@ bool testIsFound(){ searchFor = 88; correctAnswer = false; - answer = mee::isFound(searchFor, testVector); + answer = mee::isFound(testVector, searchFor); if(answer != correctAnswer){ std::cout << "isFound() is failed at test 3" << std::endl; return false; @@ -171,19 +171,19 @@ bool testGetFib(){ //Test the gmp integer function - number = 12; + mpz_class mpzNumber = 12; mpz_class longCorrectAnswer = 144; - mpz_class longAnswer = mee::getMpzFib(number); + mpz_class longAnswer = mee::getFib(mpzNumber); 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; } - number = 4782; + mpzNumber = 4782; longCorrectAnswer = "1070066266382758936764980584457396885083683896632151665013235203375314520604694040621889147582489792657804694888177591957484336466672569959512996030461262748092482186144069433051234774442750273781753087579391666192149259186759553966422837148943113074699503439547001985432609723067290192870526447243726117715821825548491120525013201478612965931381792235559657452039506137551467837543229119602129934048260706175397706847068202895486902666185435124521900369480641357447470911707619766945691070098024393439617474103736912503231365532164773697023167755051595173518460579954919410967778373229665796581646513903488154256310184224190259846088000110186255550245493937113651657039447629584714548523425950428582425306083544435428212611008992863795048006894330309773217834864543113205765659868456288616808718693835297350643986297640660000723562917905207051164077614812491885830945940566688339109350944456576357666151619317753792891661581327159616877487983821820492520348473874384736771934512787029218636250627816"; - longAnswer = mee::getMpzFib(number); + longAnswer = mee::getFib(mpzNumber); if(longCorrectAnswer != longAnswer){ - std::cout << "getMpzFib() failed at test 4" << std::endl; + std::cout << "getFib() for mpzfailed at test 4" << std::endl; return false; } @@ -210,6 +210,7 @@ bool testBubbleSort(){ for(int cnt = 1;cnt < nums.size();++cnt){ if(nums.at(cnt) < nums.at(cnt - 1)){ return false; + std::cout << "nums.size() " << nums.size() << "\ncnt " << cnt << std::endl; } } @@ -243,7 +244,7 @@ bool testQuickSort(){ } bool testSearch(){ - bool found = false; + int64_t found = -1; //Create a vector of numbers std::vector nums {1, 20, 3, 40, 5, 60, 7, 80, 9}; //Search for one that is in the vector