mirror of
https://github.com/pkivolowitz/asm_book.git
synced 2026-06-21 02:26:59 +08:00
begun precomputation chapter
This commit is contained in:
parent
17f0773d7c
commit
32fe3b3b94
2 changed files with 128 additions and 0 deletions
25
section_3/precomputation/README.md
Normal file
25
section_3/precomputation/README.md
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
# Section 3 - Pre-computation
|
||||||
|
|
||||||
|
Time versus space.
|
||||||
|
|
||||||
|
This is the essential battle that programmers face. In order to go
|
||||||
|
faster, more memory is used. In order to economize on memory, more
|
||||||
|
computation is needed.
|
||||||
|
|
||||||
|
This duality is demonstrated in no better way than comparing a
|
||||||
|
calculated method (economizing space at the expense of time) versus an
|
||||||
|
entirely precomputed method (sacrificing space to reduce time).
|
||||||
|
|
||||||
|
In this section, we will demonstrate three methods of calculating
|
||||||
|
factorials from 0 to 15.
|
||||||
|
|
||||||
|
* Iteratively
|
||||||
|
|
||||||
|
* Recursively
|
||||||
|
|
||||||
|
* By pre-computation
|
||||||
|
|
||||||
|
Certainly, for the purposes of this demonstration, it is not necessary
|
||||||
|
to implement both iterative and recursive methods. We do so for fun and
|
||||||
|
for any lessons the reader can glean.
|
||||||
|
|
||||||
103
section_3/precomputation/main.c
Normal file
103
section_3/precomputation/main.c
Normal file
|
|
@ -0,0 +1,103 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#define MIN 1
|
||||||
|
#define MAX 15
|
||||||
|
|
||||||
|
long Iterative(long n);
|
||||||
|
long Recursive(long n);
|
||||||
|
long Precomputed(long n);
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
long (*func[])(long) = {
|
||||||
|
Iterative,
|
||||||
|
Recursive,
|
||||||
|
Precomputed,
|
||||||
|
NULL
|
||||||
|
};
|
||||||
|
|
||||||
|
printf("%3s %13s %13s %13s\n",
|
||||||
|
"N", "Iterative", "Recursive", "Precomputed");
|
||||||
|
|
||||||
|
for (long n = MIN; n <= MAX; n++) {
|
||||||
|
printf("%3ld ", n);
|
||||||
|
int i = 0;
|
||||||
|
while (func[i] != NULL) {
|
||||||
|
printf("%13ld ", (*func[i])(n));
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Iterative() - this function computes the factorial of a long integer
|
||||||
|
by counting down, multiplying each time the value computed in the
|
||||||
|
previous iteration.
|
||||||
|
|
||||||
|
The maximum value of the parameter is NOT enforced here, having been
|
||||||
|
checked by the calling function.
|
||||||
|
*/
|
||||||
|
|
||||||
|
long Iterative(long n) {
|
||||||
|
long retval = 1;
|
||||||
|
for (long i = 1; i <= n; i++) {
|
||||||
|
retval *= i;
|
||||||
|
}
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Recursive() - this function computes the factorial of a long integer
|
||||||
|
recursively. At each point, the current value of n in multiplied by
|
||||||
|
the return value of calling the function with its parameter being
|
||||||
|
reduced by b1.
|
||||||
|
|
||||||
|
The maximum value of the parameter is NOT enforced here, having been
|
||||||
|
checked by the calling function.
|
||||||
|
*/
|
||||||
|
|
||||||
|
long Recursive(long n) {
|
||||||
|
long retval;
|
||||||
|
if (n <= 1)
|
||||||
|
retval = 1;
|
||||||
|
else
|
||||||
|
retval = n * Recursive(n - 1);
|
||||||
|
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
|
||||||
|
long v[] = {
|
||||||
|
1,
|
||||||
|
1,
|
||||||
|
2,
|
||||||
|
6,
|
||||||
|
24,
|
||||||
|
120,
|
||||||
|
720,
|
||||||
|
5040,
|
||||||
|
40320,
|
||||||
|
362880,
|
||||||
|
3628800,
|
||||||
|
39916800,
|
||||||
|
479001600,
|
||||||
|
6227020800,
|
||||||
|
87178291200,
|
||||||
|
1307674368000
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Precomputed() - this function computes the factorial of a long
|
||||||
|
integer using pre-computation. The values of the parameter over a
|
||||||
|
predefined range are computed prior to the first call of the
|
||||||
|
factorial function. These values are stored in a table and fetched
|
||||||
|
using the parameter as an index.
|
||||||
|
|
||||||
|
The maximum value of the parameter is NOT enforced here, having been
|
||||||
|
checked by the calling function.
|
||||||
|
*/
|
||||||
|
|
||||||
|
long Precomputed(long n) {
|
||||||
|
if (n < 0 || n > 15)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
return v[n];
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue