home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / BK-SC1_3.DMS / in.adf / Fastlib.Lha / Examples / PI / pi-stormamiga.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-29  |  2.9 KB  |  156 lines

  1. /*
  2.  
  3. Programm für den Tröpfel-Algorithmus von PI
  4.  
  5. Algorithmus aus: Spektrum der Wissenschaft 12/1995, Mathematische Unterhaltungen
  6.  
  7. Originalversion von: Martin Knapmeyer, 03.01.1996
  8.  
  9. Umsetzung nach StormC: HAAGE & PARTNER GmbH, 16.02.1996
  10.  
  11. Mit 32 bit Integers kann man auch sehr viele Dezimalen berechnen, mit 16 bit
  12. Integers wäre 10000 Dezimalen etwa die Grenze.
  13.  
  14. Allerdings hat der Algorithmus quadratische Ordnung: doppelte Anzahl Stellen,
  15. vierfache Zeit. Schon 10000 Stellen brauchen auf einem A3000 etwa 2 Stunden.
  16.  
  17. */
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <time.h>
  22.  
  23. #include <stormamiga.h>
  24.  
  25. int n; // Anzahl der Nachkommastellen
  26. int klen;
  27.  
  28. int *result; // Zeiger auf ein Feld mit n int
  29. int *chain;  // Zeiger auf ein Feld mit klen == 10 * n / 3 int
  30. int *temp;   // Zeiger auf ein Feld mit n int
  31.  
  32. #ifndef NDEBUG
  33. void write_chain(void)
  34. {
  35.     int i;
  36.     for (i = 0; i < klen; i++)
  37.         printf__("%ld ",chain[i]);
  38.     printf__("\n");
  39. }
  40. #endif
  41.  
  42. void write_result(void)
  43. {
  44.     int i;
  45.     printf__("PI:\n");
  46.     for (i = 1; i < n; i++)
  47.     {
  48.         printf__("%ld",result[i]);
  49.         if (i % 50 == 0)
  50.             printf__("\n");
  51.     };
  52.     printf__("\n");
  53. }
  54.  
  55. void init_chain(void)
  56. {
  57.     int i;
  58.     for (i = 0; i < klen; i++)
  59.         chain[i] = 2;
  60. }
  61.  
  62. void drop(void)
  63. {
  64.     int vcnt = 0, ecnt = 0;
  65.     int i,j;
  66.     div_t qr;
  67.     for (i = 0; i < n; i++)
  68.     {
  69.         for (j = 0; j < klen; j++)
  70.             chain[j] *= 10;
  71.         for (j = klen - 1; j >= 1; j--)
  72.         {
  73.             qr = div(chain[j],(2*j + 1));
  74.             chain[j] = qr.rem;
  75.             chain[j - 1] += qr.quot*j;
  76.         };
  77.         qr = div(chain[0],10);
  78.         chain[0] = qr.rem;
  79.         if (qr.quot != 9 && qr.quot != 10)
  80.         {
  81.             for (j = 0; j <= vcnt; j++)
  82.             {
  83.                 #ifndef NDEBUG
  84.                     printf__("%ld. Stelle: %ld\n",ecnt-1,temp[j]);
  85.                 #endif
  86.                 result[ecnt] = temp[j];
  87.                 ecnt++;
  88.                 temp[j] = 0;
  89.             };
  90.             vcnt = 0;
  91.             temp[vcnt] = qr.quot;
  92.         }
  93.         else
  94.         {
  95.             if (qr.quot == 9)
  96.             {
  97.                 vcnt++;
  98.                 temp[vcnt] = qr.quot;
  99.             }
  100.             else // if (qr.quot == 10)
  101.             {
  102.                 qr.quot = 1;
  103.                 for (j = vcnt; j >= 0; j--)
  104.                 {
  105.                     temp[j] += qr.quot;
  106.                     qr = div(temp[j],10);
  107.                     temp[j] = qr.rem;
  108.                 };
  109.                 for (j = 0; j <= vcnt; j++)
  110.                 {
  111.                     #ifndef NDEBUG
  112.                         printf__("%ld. Stelle: %ld\n",ecnt-1,temp[j]);
  113.                     #endif
  114.                     result[ecnt] = temp[j];
  115.                     ecnt++;
  116.                     temp[j] = 0;
  117.                 };
  118.                 vcnt = 0;
  119.                 temp[0] = 0;
  120.             }
  121.         };
  122.     };
  123. }
  124.  
  125. void main__(void)
  126. {
  127.     clock_t timestart,timeend;
  128.     printf__("Berechnung von PI\n");
  129.     for (;;)
  130.     {
  131.         printf__("Stellenanzahl (0 für Ende):"); fflush(stdout);
  132.         scanf__("%ld",&n);
  133.         if (n < 1)
  134.             exit(0);
  135.         klen = (n * 10) / 3;
  136.         result = (int *) malloc(n*sizeof(int));
  137.         chain = (int *) malloc(klen*sizeof(int));
  138.         temp = (int *) malloc(n*sizeof(int));
  139.         if (result == NULL || chain == NULL || temp == NULL)
  140.         {
  141.             printf__("no memory, no pi.\n");
  142.             exit(100);
  143.         };
  144.         init_chain();
  145.         timestart = clock();
  146.         drop();
  147.         timeend = clock();
  148.         printf__("Zeitbedarf für %ld Stellen: %lds\n",n,
  149.             (timeend-timestart) / CLOCKS_PER_SEC);
  150.         write_result();
  151.         free(result);
  152.         free(chain);
  153.         free(temp);
  154.     };
  155. }
  156.