0

I am writing a Fibonacci program for a homework assignment. We have been using recursion and memoization in class to improve the run-time for finding the Fibonacci numbers up to element of 1000. My program, however, only works at finding the Fibonacci number up to element 93 and anything else after that produces the wrong answer. I've tried many solutions, including changing the data type to "long long" and "unsigned long long". Anyone know how to fix this problem?

Here is the code I have so far:

#include <iostream>
#include <vector>

using namespace std;


unsigned long long Fibonacci(int aInput, vector<unsigned long long>& aVec)
{
     unsigned long long aAnswer;

if (aVec[aInput] == 0)
{
    if (aInput <= 0)
    {
        aVec[0] = 0;
        return 0;
    }
    else if (aInput == 1)
    {
        aVec[1] = 1;
        return 1;
    }
    else
    {
        aAnswer = Fibonacci(aInput - 1, aVec) + Fibonacci(aInput - 2, aVec);
        aVec[aInput] = aAnswer;
        return aAnswer;
    }
 }
else
{
    return aVec[aInput];
}
}


int main()
{
bool flag = true;
unsigned long long answer;
int num;
char choice;
vector<unsigned long long> MyVec(1000,0);

while (flag == true)
{
    cout << "Enter a Number: ";
    cin >> num;

    answer = Fibonacci(num, MyVec);
    cout << "Fibonacci at Index " << num << " is: " << answer << endl;

    cout << endl << "Enter Another Number? (Y/N): ";
    cin >> choice;

    if (choice == 'Y' || choice == 'y')
    {
        flag = true;
    }
    else
    {
        flag = false;
    }
}


cout << endl;
system("pause");
return 0;
}
bpreiss12
  • 7
  • 2
  • There is no need to use recursion or any kind of loop to find the Nth Fibonacci number. Use [Binet's Formula](https://mathworld.wolfram.com/BinetsFibonacciNumberFormula.html). Also, the value of the 93rd number will overflow the integer variables -- store the result in a double. – PaulMcKenzie May 10 '20 at 16:18
  • 1
    The 94th fibonacci number is larger than 2^64. You can't store it in an `unsigned long long`. You need another type, maybe from one of the big-integer libraries available on the internet. – Nelfeal May 10 '20 at 16:26
  • 1
    The 93rd number, 12200160415121876738, is the last one that fits into a 64-bit unsigned integer. The next one would overflow that. The 1000th number is enormous. Exponential growth will do this to you. – Igor Tandetnik May 10 '20 at 16:27
  • The solution requires us to use recursion. What are some of the bigger integer libraries available? – bpreiss12 May 10 '20 at 16:28
  • [Boost.Multiprecision](https://www.boost.org/doc/libs/1_73_0/libs/multiprecision/doc/html/index.html) – Evg May 10 '20 at 16:29
  • *The solution requires us to use recursion* -- Your current solution has an issue, regardless if you used a big number library. What if you run out of stack going that deep into the recursion? – PaulMcKenzie May 10 '20 at 16:38
  • 2
    I'm not sure I understand what you're describing. You're saying that you were given a homework assignment that is impossible to implement in C++ itself, and requires the use of additional libraries that you never learned how to use, in class? The only way this makes any kind of logical sense is if your "real" homework assignment is to implement ***your own*** multiprecision numerical library, and its basic operations (addition, subtraction, multiplication, division, etc...) and then use that to solve this problem. If you hand in something that uses boost, you will obviously fail it. – Sam Varshavchik May 10 '20 at 16:42
  • 1
    To what Sam stated, all you really need is a Big int class that does addition (probably using a `std::string` as the "int"), since that's the only operation you're doing in the code. That takes less than one day, maybe an hour or 2 (if you are a good student) to complete. Once you have that class, then you should literally be able to replace `long long int` with that type. – PaulMcKenzie May 10 '20 at 16:45
  • Responding to Sam's comment, yes I've never heard of libraries such as "BigInt" until today. The point of our homework was to take a recursive Fibonacci function and use memoization (which I forgot to include in the description) to make it run more efficiently and make it able to find very large number in the Fibonacci sequence. I assumed I was getting an overflow error with my arithmetic but I was not sure how to fix it. – bpreiss12 May 10 '20 at 16:52
  • @bpreiss12 There is no fix for the overflow error -- it is what it is. Maybe creating such a class was what the assignment was really geared towards. – PaulMcKenzie May 10 '20 at 16:59
  • @bpreiss12 -- Also, if you were to try 1000, you will see that access to the `vector` in `Fibonacci` will be out-of-bounds. Your vector size is too small. – PaulMcKenzie May 10 '20 at 17:23
  • If the point of your homework is just to "take a recursive Fibonacci function and use memoization" then the goal is sufficiently met by making your homework asssignment work for reasonable number of Fibonnaci numbers, up to the limit natively supported on your 32 or 64 bit OS, and there is no point to using a dedicated library for that purpose. – Sam Varshavchik May 10 '20 at 18:05

0 Answers0