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;
}