size_a, pa - points to the valid end of array a
ma - maximum capacity of a, must be greater than na
n=12- -find the order of n
p - the current multiplier when finding the factorial
*/
#include<stdio.h>
#define Ma 10000
int pa;/* points to the valid end of array a
int p=2;
int memory_over=0;
union data
{ unsigned long int b;
struct
{unsigned l:16;
unsigned h:16;
}m;
}a[Ma];
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Algorithm note 1: Considering the result is rather long, I use a[Ma].b to store the result of n!, each a[pa].b can store 4 decimal digits.
Because the array I defined is static, Ma should be large enough.
ps:In fact, it is enough to define a unsigned long int b[Ma]; (directly use b[pa] instead of a[pa].b), but I consider that I may access the high 16 bits (a[pa].m.h) and the low 16 bits (a[pa].m.l) of each node b[pa], but I consider it is redundant! No need to define such a complex **** with bodies like I did!
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
unsigned int cashe;
unsigned int carry;
unsignified int carry;
void
void main()
{
unsigned int n;/* find the order of n*/
void facto(unsigned int n);
printf("Input n:");
scanf("%u",& amp;n);
/*================= start factoring! =============*/
a[0].b=1;/*initialize**
facto(n);
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Algorithm note 2: The above sentence calls facto(n) directly to find n!
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
/*======================== The following shows the final result =============== =====================*/
if(memory_over==0)
{printf("the result include %dNO:\n",pa+1);
printf("%u",a[pa--].m.l);
for(;pa>=0;pa--)
printf("%04u",a[pa].m.l);
printf("\n");
}
getch();
}
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Algorithm description 2: Description of the facto(n) function for order:
This function calls multiple() over and over again. Each time it is called it makes a[pa].b multiply by the order p until n has been multiplied!
{multiple();
p++;/* multiply by order p every round*/
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++* /
void facto(unsigned int n)
{void multiple();
pa=0;
while(pa<Ma-1&&p<=n)/*capacity limit**
{ multiple();
p++;/* multiply by one order p per round*/
}
if(p<=n)
{printf("memory out!\n");memory_over=1;}//*If the current array a[Ma] storing the result is not enough! Ma should be raised
}
/*==============================================================================
Algorithm description 3: Multiplication function multiple() Description: Responsible for multiplying a[pa].b with order p.
a[pa].b has many nodes, a[0].b, a[1].b, a[2].b, a[3].b, a[4].b,.
Of course it starts at the low node a[0].b and keeps on multiplying with p. The resulting "progressions" are added to the high a[1].b until a[pa].b*p!
As the result increases in value, the a[].b of the pa node may not be able to hold the result, so if a[pa].b is multiplied by p and there is still a "rounding" carry, expand pa and put the carry into the newly added node:
if(carry>)
If(carry> 0)
All the nodes in a[1].b[1].b*p) have a "rounding" carry, the a[1].b*p is added to the higher a[1].b*p! 0)
a[++pa].b=carry;
===================================================================================*/
void multiple()
{int i=0;
carry=0;
while(i<=pa)/*i points to the currently processed element a[i], multiply by order p with one bit per round*/
{a[i].b=a[i].b*p+carry;/*Calculate result carry=a[i].b/10000;/*calculate carry**/
a[i].b=a[i].b%10000;/*calculate remainder**
i++;
}
if(carry>0)
a [++pa].b=carry;
}