Updated getFib so you don't need a special function for the gmp library

This commit is contained in:
Matthew Ellison
2019-02-09 00:49:25 -05:00
parent 649fe3a66e
commit 84cbf6d5af

View File

@@ -69,11 +69,6 @@ std::vector<std::string> getPermutations(std::string master, int num = 0);
//These functions return the numth Fibonacci number
template <class T>
T getFib(T num);
//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
mpz_class getMpzFib(uint64_t num);
#endif //NEEDGMP
//This function returns a vector that includes all Fibonacci numbers <= num
template<class T>
std::vector<T> getAllFib(const T num);
@@ -306,40 +301,15 @@ T getFib(T num){
tempNums[0] = tempNums[1] = 1;
//Do the calculation
for(T cnt = 2;(cnt < num) && (tempNums[(cnt - 1) % 3] >= tempNums[(cnt - 2) % 3]);++cnt){
uint64_t 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];
}
fib = tempNums[(num - 1) % 3]; //Transfer the answer to permanent variable. -1 to account for the offset of starting at 0
fib = tempNums[(cnt - 1) % 3]; //Transfer the answer to permanent variable. -1 to account for the offset of starting at 0
return fib;
}
//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
mpz_class getMpzFib(uint64_t num){
//Make sure the number is within bounds
if(num <= 0){
return 0;
}
else if(num <= 2){
return 1;
}
mpz_class fibNum = 0;
mpz_class tempNums[3];
//Initialize the variables
tempNums[0] = 1;
tempNums[1] = 1;
//Do the calculation
for(uint64_t cnt = 2;cnt < num;++cnt){
tempNums[cnt % 3] = tempNums[(cnt + 1) % 3] + tempNums[(cnt + 2) % 3];
}
return tempNums[(num - 1) % 3]; //Return the answer
}
#endif //NEEDGMP
//This function returns a vector that includes all Fibonacci numbers <= num
template<class T>
std::vector<T> getAllFib(const T num){