Time limit of a c program while calculating factorial of numbers in c I am solving a problem on calculation of factorial and the challenge is as follows!

You are asked to calculate factorials of some small positive integers.

Input

An integer t, 1&lt;=t&lt;=100, denoting the number of testcases, followed by t lines,
each containing a single integer n, 1&lt;=n&lt;=100.

Output 

For each integer n given at input, display a line with the value of n!


My Code is giving me the correct solution but Time limit is exceeded which is 2 seconds:

The code is as follows:

#include&lt;stdio.h&gt;
#include&lt;stdlib.h&gt;
#include&lt;math.h&gt;
void factorial(int N)
{
    printf("\n\n");
    int q,i,j,t,d,z;
    float p=0.0;
    for(i=2;i&lt;=N;i++)
    p=p+log10(i);
    d=(int)p+1;//No of terms in the factorial
    int *b;
    //initialization of an array
    b=(int *)malloc(d*sizeof(int));
    b[0]=1;
    for(i=1;i&lt;d;i++)
    b[i]=0;
    //calculation of factorial
    p=0.0;
    for(j=2;j&lt;=N;j++)
    {
        q=0;
        p=p+log10(j);
        z=(int)p+1;
        for(i=0;i&lt;N;i++)
        {
           t=(b[i]*j)+q;
           q=t/10;
           b[i]=t%10;   
        }
    }
    for(i=d-1;i&gt;=0;i--)
    printf("%d",b[i]);

}
int main()
{
    int n,i,j;
    scanf("%d",&amp;n);
    int *b;
    b=(int *)malloc(n*sizeof(int));
    for(i=0;i&lt;n;i++)
    {
        scanf("%d",&amp;b[i]);
    }
    for(i=0;i&lt;n;i++)
    factorial(b[i]);
    return 0;
}


How can i make my program more efficient and produce the output in the given time limit? This challenge is from HackerEarth
The best solution is only to compute the factorials you need and only compute them once.


Read all the user input find the maximum value.
Generate the factorials for all values upto the max
Note: Saving them as you go:
Print the factorial values by looking up the result you generated in 2.


So lets look at the code:

I would not bother dynamically allocating the data storage.

    b=(int *)malloc(n*sizeof(int));


The question specifically limits the maximum value of n to 100.

    int b[101]; // should be suffecient


When reading values this should work (if you assume that the input is good).

        scanf("%d",&amp;b[i]);


But the question states that the input is one value per line. Personally I would validate that there is one value per line.

Here you are calculating the factorial multiple times:

    for(i=0;i&lt;n;i++)
       factorial(b[i]);  // Factorial is O(n)
                         // Thus this loop is O(n^2)


Technically you only need to call factorial once. If you calculate factorial for 'n' you need to calculate the factorial for 'n-1' (its how it is defined). If you store the numbers then you only need to look up the values to print it once they have been calculated.

It seems like you calculate this value each time.

    float p=0.0;
    for(i=2;i&lt;=N;i++)
    p=p+log10(i);


The worst case scenario is that you need to calculate this for a value of 100. You only need to calculate this one for 100. Also you can calculate it off line and now it is a const expression.

This is then a const expression and thus you don't need to calculating it.

    b=(int *)malloc(d*sizeof(int));


But you do need 100 of them so you can store all intermediate values:

You don't need to loop over an array to initialize it:

    for(i=1;i&lt;d;i++)
        b[i]=0;


You can initialize it already zero'd out. For dynamic memory use calloc() for automatic arrays use = {0};

So the above two can be combined into;

    const int d = &lt;pre-calculated value&gt;;
    int b[100][d] = {0};
    b[0][0] = 1;
    b[1][0] = 1;