From 84cbf6d5affcfa77ebee56f96773efd3334c56fd Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Sat, 9 Feb 2019 00:49:25 -0500 Subject: [PATCH] Updated getFib so you don't need a special function for the gmp library --- Algorithms.hpp | 36 +++--------------------------------- 1 file changed, 3 insertions(+), 33 deletions(-) diff --git a/Algorithms.hpp b/Algorithms.hpp index 519125d..0a9b8bc 100644 --- a/Algorithms.hpp +++ b/Algorithms.hpp @@ -69,11 +69,6 @@ std::vector getPermutations(std::string master, int num = 0); //These functions return the numth Fibonacci number template 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 std::vector 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 std::vector getAllFib(const T num){