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...
Repeating a set of instructions a specific number of times is called ______________ iteration.
Fill in the blank(s) with the appropriate word(s).
An array that uses two indices is referred to as a(n) ____________ array.
What will be an ideal response?
Use the ____________________ if only minor adjustments are needed for the macro you have recorded.
Fill in the blank(s) with the appropriate word(s).
What is a cryptographic key?
What will be an ideal response?