Re-implement the recursive factorial function to use dynamic programming as we did for the Fibonacci function.
What will be an ideal response?
```
also with the instructor’s code
// Chapter 8 Design and Implement Problem 4
// storage for factorial values with first two initialized
static int[] factorialValues = {
1, 1, 0, 0, 0, 0, 0, 0, 0, 0};
static int lastComputedFactorial = 1;
// original recursive solution for comparision
static long fact( int n ) {
if ( n == 1 ) return 1;
return n * fact( n - 1 );
}
// dynamic programming solution for factorial function
static long factorial(int n) {
int i;
// see if there is room to store the computed values
if (n > factorialValues.length) {
return -1;
}
// compute the factorials up to n and store the results
lastComputedFactorial++;
for (; lastComputedFactorial <= n; lastComputedFactorial++) {
factorialValues[lastComputedFactorial] = factorialValues[
lastComputedFactorial - 1] * lastComputedFactorial;
}
// undo the last ++ from the last iteration of the for loop
lastComputedFactorial--;
return factorialValues[n];
}
```
You might also like to view...
Compressing a file decreases the file size.
Answer the following statement true (T) or false (F)
A flashing vertical line in the document window that indicates where text will appear when you type is known as the ____ point.
A. starting B. insertion C. editing D. typing