Calculate the factorial of 400
-
C/C ++ code
-
#define N 400long a[8916]={1,0},n,i,c,len; int main(void) { n=N; for ( len=1;n>1; n--) { for (c=0,i=0; i<len;i++ ) { a[i]= ( c+= a[i]*n ) % 10000; c/=10000; } ((a[i]=c)>0)?len++:0; } for( len--,printf("%d",a[len--]);len>=0; len--) printf("%04d",a[len]); return 0; }
[Explanation]
For (LEN = 1; n> 1; n --) // set the length of Len to 1, because an element in the array already has a [0] = 1.
{
For (C = 0, I = 0; I <Len; I ++) // The initial value of C is 0. It is an inner loop, len in array
// All elements must be involved in the operation and must be * n, which is the reason for a [I] * N below.
{
A [I] = (C + = A [I] * n) % 10; C/= 10; // C is a carry, needless to say, c + = A [I] * n. Each element of a corresponds to n
// To multiply values, you must consider whether the low position has a forward digit.
}
(A [I] = c)> 0 )? Len ++: 0; // does the last element have an increment? If yes, the position in the next array of the current element is directly equal to the carry value, and
// Add 1 to the element value of the array. If there is no element, nothing is done. If there is no element, nothing is done.
}