/*
* Modified C source code
* 4. Analyze and optimize the C program for calculating the factorial below. The report must be written and the measurement data must be analyzed for support. At the same time, the methods and tools mentioned in the class should be used.
*
* The array method is used to solve the problem that the factorial results of large numbers and huge numbers are out of bounds. Specific algorithms have the simplest method of multiplication.
**/
/* Header contains the header file */
# Include <stdio. h>
# Include <stdlib. h>/* which function uses this library? */
# Define m limit 00000l/* definition */
# Define n 60000/* define the array size: Make sure that the array storing the final calculation result is large enough */
/* Funtion function declaration */
Int multiply (int n, unsigned int prod [N], int highest );
Void print (unsigned int prod [N], int highest );
/* Main Program */
Int main (INT argc, char * argv []) {
Int I, n = 0, highest = 0;/* integer n is the factorial size. Highest is the number of digits (but why not initialize to 1? Highest should be less than N )*/
Int prod [N] = {1, 0};/* initialize the result to 1 first. That is to say, if no parameter is input on the keyboard, the default input parameter is 0 and the result is 1 */
If (argc> 1) n = atoi (argv [1]);/* command line parameter: receives the factorial size N from the keyboard. */
For (I = 2; I <= N; I ++)/* Start the factorial, And the factorial element starts to appear in sequence from 2 "*/
Highest = multiply (I, prod, highest);/* call the factorial subprogram */
Print (prod, highest );
Return 0;
}
/*
* Subprograms used to calculate factorial
*
**/
Int multiply (int n,/* n: Number of factorial required */
Unsigned int prod [N],/* array for storing the final calculation result */
Int highest)/* number of digits of the factorial result */
{
/* TMP is the product of any element of the factorial and a temporary result */
Unsigned long TMP;/* If your compiler supports c99 */
/* Unsigned _ int64 TMP; * // * if you use visual c or Borland C */
Int carrier = 0;/* carry */
Int I;
/* Multiply each of the temporary results by a factorial element based on the basic multiplication algorithm */
For (I = 0; I <= highest; I ++ ){
/* One of the corresponding factorial is multiplied by one of the current temporary results (plus carry )*/
TMP = N;
TMP * = prod [I];
TMP + = carrier;
/* The above 3 formula is equivalent to TMP = prod [J-1] * I + carrier; j indicates the temporary accumulators I here, And I indicates the outer accumulators I */
Prod [I] = TMP % m;/* update the bit information of the temporary result */
Carrier = tmp/m;/* check whether there is an advance */
}
If (carrier)/* If the input space exists */
Prod [++ highest] = carrier;/* Add a new digit to add information. Increase the number of digits by 1 */
Carrier/= 10;/* check whether it can be carried */
Return highest;
}
/*
* Output result
*
**/
Void print (unsigned int prod [N], int highest ){
Printf ("% u", prod [highest]);/* ISO c99 Standard Support * // * display result */
While (highest> 0)
Printf ("% 09u", prod [-- highest]);
Return;
}