1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
// global digits - start with a small sample to verify code
int digits = 10;
int term = 3;
int hold, sum;
int i = digits - 1;
int past[10], present[10],future[10]; // three arrays to create fib. seq.
// global functions, declared post main
void addFibonacci(int*, int*, int*);
void resetSequence(int*, int*, int*);
void showTime(int*);
int main()
{
clock_t runtime = clock();
// set the "F1" and "F2" terms equal to 1 to start the process
past[i] = present[i] = 1;
addFibonacci(past, present, future);
showTime(future);
cout << endl;
// the term itself will be to large to print easily
cout << "Term with " << digits << " digits is: " << term << endl;
cout << "Runtime: " << (clock()-runtime)/CLOCKS_PER_SEC;
}
//recursive function (I think I labled it pretty well with variable names)
void addFibonacci (int* pastArray, int* presentArray, int* futureArray)
{
while ( i >= 0)
{
if (presentArray[0] == 1)
{
i = -1;
}
else
{
hold = pastArray[i] + presentArray[i];
sum = hold;
if (hold >= 10)
{
if (i < 0) break;
sum -= 10;
futureArray[i] = sum;
pastArray[i-1] += 1;
i--;
if (i < 0) break;
addFibonacci(pastArray, presentArray, futureArray);
}
if (hold > 0)
{
if (i < 0) break;
futureArray[i] = hold;
i--;
if (i < 0) break;
addFibonacci(pastArray, presentArray, futureArray);
}
if (hold == 0)
{
if (i < 0) break;
term++;
resetSequence(pastArray, presentArray, futureArray);
showTime(presentArray);
if (i < 0) break;
}
}
}
}
// after finding a number, start over and reset the fib number positions
void resetSequence (int* pastArray, int* presentArray, int* futureArray)
{
i = digits - 1;
for (int j = 0; j < digits; j++)
{
pastArray[j] = presentArray[j];
presentArray[j] = futureArray[j];
}
}
void showTime (int* presentArray)
{
for (int k = 0; k < digits; k++)
{
cout << presentArray[k];
}
cout << endl;
}
|