Esempio n. 1
0
int main(int argc, char **argv)
{
int      ii, dplaces;
char     *buffer;
MAPM     pi_mapm;               /* declare the MAPM variable ... */


if (argc < 2)
  {
   fprintf(stdout,"\nUsage: pi_2  digits\t\t\t\t[Version 1.1]\n");
   fprintf(stdout,
      "       Compute PI to \'digits\' decimal places of accuracy\n");

   exit(4);
  }

dplaces = atoi(argv[1]);

if (dplaces < 2)
  {
   fprintf(stdout,"\nUsage: pi-2  digits \n");
   exit(6);
  }


fprintf(stderr, "\nstart Borwein-Quartic PI calculation \n");

pi_mapm = compute_PI(dplaces);

fprintf(stderr, "Borwein-Quartic PI calculation complete \n\n");

/*
 *   find out how many digits so we can
 *   convert the entire value into a string.
 *   (plus some pad for decimal point, exponent, etc)
 */

ii = pi_mapm.significant_digits() + 12;

if ((buffer = (char *)malloc(ii)) == NULL)
  {
   fprintf(stdout,"PI demo: Out of memory \n");
   exit(8);
  }

/* now convert the MAPM number into a string */

pi_mapm.toString(buffer, dplaces);
printf("Borwein PI = [%s] \n",buffer);

free(buffer);

exit(0);
}
Esempio n. 2
0
void kmp(char *T, char *P) {
    int m, k;
    int *PI;
    long int n, i, c;

    n = strlen(T);
    m = strlen(P);
    k = 0;
    c = 0;
    PI = (int *)malloc(sizeof(int)*(m+1));
    compute_PI(PI, P, m);

    for ( i=0; i<n; i++ ) {
        while ( k>0 && P[k] != T[i] )
            k = PI[k];
        if ( P[k] == T[i] )
            k++;
        if ( k == m ) {
            c++;   
            k = PI[m];
        }
    }
    printf("%ld\n", c); 
}