100以内的阶乘

void jieCheng(int  N)
{
int a[64];
int n, i, c, p;
int len = 1;
a[0] = 1;
//计算
for (n=N; n>1; n--) {
c=0;
for (i=0; i<len; i++) {
p=a[i]*n+c;//计算
a[i]= p%10000;
c = p/10000;
}
//存储并标记长度
a[i]=c;
if (c>0) {
len++;
}
}
//打印
printf("%d,",a[len-1]);
for (i=len-2; i>=0; i--) {
i==0 ? printf("%04d",a[i]) : printf("%04d,",a[i]);
}
printf("\n");
}

10000以内的阶乘

10,0000!大概有50,0000位数,计算基本上达到目前个人电脑的极限,我在MacBook Pro (Retina, 15-inch, Mid 2015)环境下Time to do 100000! is 60.233349 seconds。

void jieCheng(int  N)
{
long a[100000];//100000以下的阶乘
long n, i, c, p;
int len = 1;
a[0] = 1;
//计算
for (n=N; n>1; n--) {
c=0;
for (i=0; i<len; i++) {
p=a[i]*n+c;//计算
a[i]= p%100000000;//每个数组元素,存储亿为单位
c = p/100000000;
}
//存储并标记长度
a[i]=c;
if (c>0) {
len++;
}
}
//打印
printf("len=%d:%ld,",len, a[len-1]);
for (i=len-2; i>=0; i--) {
i==0 ? printf("%09ld",a[i]) : printf("%09ld,",a[i]);
}
printf("\n");
}