home *** CD-ROM | disk | FTP | other *** search
/ The Programmer Disk / The Programmer Disk (Microforum).iso / xpro / c3 / pro13 / f.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-01-21  |  2.3 KB  |  94 lines

  1. /*
  2.  * Sample program of ways for computing with numbers larger than normal
  3.  * written in Turbo C.  This program computes the factorial of a number
  4.  * for vaules of 'N' upto 3000. (i.e. K! for values of K from 0 to 3000+
  5.  */
  6. #include <time.h>
  7. #include <dos.h>
  8.  
  9.   typedef int          number[10000];
  10.  
  11.  number       arr;
  12.  float        t1, t2, tt;
  13.  int          x, i, place, N, tens, ones;
  14.  char         bell;
  15.  struct time  start, end, elapsed;
  16.  
  17. void         Roundoff()
  18. {
  19.   for (i = 0; i <= place - 1; i++)
  20.   {
  21.     while (((arr[i] / tens) > 0))
  22.     {
  23.       arr[i + ones] = arr[i + ones] + ((arr[i] / tens) % 10);
  24.       ones = ones + 1;
  25.       tens = tens * 10;
  26.     }
  27.     arr[i] = arr[i] % 10;
  28.     ones = 1;
  29.     tens = 10;
  30.   }
  31. }
  32.  
  33. void         Multiply()
  34. {
  35.   for (i = 0; i <= place; i++)
  36.   arr[i] = arr[i] * x;
  37. }
  38.  
  39. void         Setplace()
  40. {
  41.   if ((arr[place - 1] != 0) && (place < 10000)) place = place + (x * arr[place - 1] / 10);
  42. }
  43.  
  44.  
  45. main(argc,argv)
  46. int argc;
  47. char *argv[];
  48. { if (argc > 1)
  49.      N = atoi(argv[1]);
  50.   else
  51.   { printf("\n");
  52.   printf("Program to Calculate N! for up to 10000 digits in final answer\n");
  53.   printf("\n");
  54.   printf("Please enter the number to calculate N!\n");
  55.   printf("N = ? ");
  56.   scanf("%d",&N); }
  57.   for (i = 0; i <= 10000; i++) arr[i] = 0;
  58.   place = 4;
  59.   tens = 10;
  60.   ones = 1;
  61.   bell = '\007';
  62.   printf("%c\n",bell);
  63.   arr[0] = 1;
  64.   gettime(&start);
  65.   for (x = 1; x <= N; x++)
  66.   {
  67.     Multiply();
  68.     Roundoff();
  69.     Setplace();
  70.   }
  71.   gettime(&end);
  72.   t1 = ((((start.ti_hour * 60) + start.ti_min) * 60) + start.ti_sec)*100 +
  73.        start.ti_hund;
  74.   t2 = ((((end.ti_hour * 60) + end.ti_min) * 60) + end.ti_sec)*100 +
  75.        end.ti_hund;
  76.   tt = (t2 - t1)/100;
  77.   printf("Start time was  - %02d:%02d:%02d.%02d\n", start.ti_hour,
  78.      start.ti_min, start.ti_sec, start.ti_hund);
  79.   printf("Finish time was - %02d:%02d:%02d.%02d\n", end.ti_hour,
  80.      end.ti_min, end.ti_sec, end.ti_hund);
  81.   printf("Total elapsed time was %6.2f seconds\n",tt);
  82.   printf("\n");
  83.   printf("%d ! =  \n",N);
  84.   while (arr[place] == 0) place = place - 1;
  85.   for (i = 0; i <= place; i++)
  86.   {
  87.     printf("%d",arr[place - i]);
  88.     if (((place - i) % 3 == 0) && (i < place)) printf(",");
  89.     if ((place - i) % 54 == 0) printf("\n");
  90.   }
  91.   printf("\n");
  92.   printf("%d digits%c\n",place + 1,bell);
  93. }
  94.