Sunday, 17 November 2013

C PROGRAM TO CALCULATE FACTORIAL OF 16000



In my previous post I have told you about program which can calculate factorial up to 1000.If you have not read it please read it first .Now I have posted a program which can calculate factorial up to 16000, this is the maximum number upto which I was able to calculate factorial in Turbo C 3.0.It takes approximately 4 minute 30 seconds to calculate factorial of 16000 and 1 minute 31 seconds to calculate factorial of 10000.




 SOURCE CODE OUTPUT EXPLANATION DOWNLOAD


#include<conio.h>
#include<stdio.h>
void main()
{
  FILE *fp;
  char choice;
  int z,ccha[8],i,a,b=0,g,j,d,h,f=0;
  long unsigned int ch[7651],cha[7651],y=0,e=100000000;
  clrscr();
  for(j=0;j<=7650;j++)
  {
    ch[j]=0;
    cha[j]=0;
  }
  printf("\n\n\n\n\n\t\t\t     MOHAMMED UJJAIN WALA\n\n\t\t\t\t  PRESENTS");
  printf("\n\n\n\t\t\t\t  FACTORIAL ");
  printf("\n\n\n\npress any key to continue");
  getch();
  do
  {
    fp=fopen("factor.txt","w");
    clrscr();
    printf("Enter the no. between 0-16000 whose factorial to be found ") ;
    scanf("%d",&a);
    if(a<1||a>16000)
    {
      printf("\nFactorial cannot be calculated");
      getch();
      exit(0);
    }
    if(a>2000)
    printf("Please wait ................ ") ;
    d=a-1;
    g=d;
    ch[7580]=a;
    while(d!=0)
    {
      b=0;
      while(d!=0)
      {
if(b>0)
{
  for(j=y;j<=7580;j++)
  {
    ch[j]*=10;
    f=ch[j]/e;
    ch[j]=ch[j]%e;
    ch[j-1]+=f;
  }
}
h=d%10;
if(h!=0)
{
  for(j=7580;j>y;j--)
  ch[j]=ch[j]*h;
  for(j=7580;j>y;j--)
  {
    f=ch[j]/e;
    ch[j]=ch[j]%e;
    ch[j-1]+=f;
  }
  for(j=7580;j>y;j--)
  cha[j]=ch[j]+cha[j];
  for(j=y;j<=7580;j++)
  {
    z=ch[j]%h;
    ch[j]=ch[j]/h;
    ch[j+1]=(z*e)+ch[j+1];
  }
}
b=1;
d=d/10;
      }
      for(j=7580;j>y;j--)
      ch[j]=cha[j];
      for(j=7580;j>y;j--)
      {
f=ch[j]/e;
ch[j]=ch[j]%e;
ch[j-1]+=f;
      }
      for(j=7580;j>y;j--)
      cha[j]=0;
      g=g-1;
      d=g;
      y=0;
      for(j=0;j<=7580;j++)
      {
if(ch[j]!=0)
break;
else
y++;
      }
      y=y-10;
    }
    y=0;
    for(j=0;j<=7580;j++)
    {
      if(ch[j]!=0)
      break;
      else
      y++;
    }
    for(j=y;j<=7580;j++)
    {
      for(i=0;i<8;i++)
      {
ccha[i]=ch[j]%10;
ch[j]=ch[j]/10;
      }
      for(i=7;i>=0;i--)
      {
printf("%d",ccha[i]);
fprintf(fp,"%d",ccha[i]);
      }
    }
    fclose(fp);
    printf("\nWant to enter more no. whose factorial to be calculated(Y/N)");
    choice=getch();
  }while(choice=='y'||choice=='Y');
}


SCREENSHOTS

Previous                                                                 Next
As we move to larger numbers like 16000 its factorial becomes so large that we have not enough space to store that number .In turbo C if we try to declare int ch[32768] compiler will give an error i.e. array size is to large and if we declare we will declare 2d arrays ch[2][30000]  than compiler will also generate error.The reason behind the error is simple , we cannot allocate static memory greater than 64 KB .Now if we declare two different array say ch[30000] and cha[30000] than compiler will not generate but we are declaring static memory greater than 64 KB so why compiler did not generate error ?.It is because what ever we declare we have only 64 KB of memory, now at execution time when ch[30000] gets allocated on RAM and the program has not enough memory space to allocate memory for cha[30000] than it will override the memory block which was allocated for ch[30000] and we get unexpected results ,conclusion is we cannot use memory greater than 64 KB .Thus total integer type (i.e. which will occupy 2 bytes of memory) of variable should  be less than 32768.We can also declare unsigned int than we can declare ch[65534] but this will not help us much .Now there are 35664 digits in factorial of 10000 thus we have not enough space in integer type array therefore we will declare two array ( ch[7651] cha[7651])of data type long unsigned int
Note : here total long usigned int type(i.e. 4bytes ) of variable should be less than 16384 .In our case total variables are 7651+7651=15302.Thus it is still less than 64 KB of memory.
We can store 10 digits in single array of type long unsigned int ,so we will store up to 8 digits in a single array variable and rest of the two digits are for carry ,therefore total size of array required to store factorial of 10000 is 35564/8=4445.5 or 4446 .Now we have enough space  to store factorial of 10000.From my calculations we can store factorial up to 16000 in array of size 7651.
This all concepts are difficult two understand .I have told above concepts to tell you why and what changes I have make in my program and also why we cannot calculate factorial of 100000 in turbo C because we need approximately two arrays of size 70000.
I have declare e=100000000 so that I can store 8 digits in single array.
The working of the program is same as program to calculate factorial of 100 .The only difference is we are adding partial product simultaneously with multiplication of numbers, thus we need only two arrays this increases CPU works but we really require less amount of memory. Our CPU also uses this approach to multiply two numbers.
Any questions regarding to program please write in comments.


RELATED POST:-

No comments:

Post a Comment