-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathlargefactorials.c
68 lines (63 loc) · 1.27 KB
/
largefactorials.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<stdio.h>
int a[20000]; //this program can only compute factorials
//with 20,000 or less digits but the range
int main() //can be increased by changing 20,000 with
{ //a larger number less than 10,000,000
int T,n,j,g,f;
scanf("%d",&T); //input no. of test cases
for(int i=0;i<T;i++)
{
for(j=0;j<20000;j++) a[j]=0;
f=0;
a[1]=1;
g=1;
scanf("%d",&n); //input the no. whose factorial
for(j=1;j<=8;j++) //is required
{
a[1]*=j;
if(j==n) break;
}
if(j==9)
{
for(f=0;a[g+f]/10>1;)
{
f++;
a[g+f]=a[g+f-1]/10;
a[g+f-1]=a[g+f-1]%10;
}
}
g+=f;
for(j=9;j<=n;j++)
{
for(int k=1;k<=g;k++)
{
a[k]=a[k]*j;
if(k-3>0)
{
a[k]+=a[k-3]/1000;
a[k-3]=a[k-3]%1000;
a[k-1]+=a[k-3]/100;
a[k-3]=a[k-3]%100;
a[k-2]+=a[k-3]/10;
a[k-3]=a[k-3]%10;
}
}
a[g]+=a[g-2]/100;
a[g-2]=a[g-2]%100;
a[g-1]+=a[g-2]/10;
a[g-2]=a[g-2]%10;
a[g]+=a[g-1]/10;
a[g-1]=a[g-1]%10;
for(f=0;a[g+f]/10>=1;)
{
f++;
a[g+f]=a[g+f-1]/10;
a[g+f-1]=a[g+f-1]%10;
}
g=g+f;
}
for(j=g;j>0;j--) printf("%d",a[j]); //printing the output
printf("\n"); //in the form of an array
}
return 0;
}