/* Pi Spigot from April '95 Math Monthly ---------------------- The program basically evaluates n terms of a series for pi by starting with the last term and multiplying by 10 and carrying the remainder to the next term, reading off the ones digit, and repeating until all numerators are zeroed. Check out the issue for a more detailed explanation. Improved output by Robert Simms, of Salsibury, MD Language: C (manually converted from Pascal) */ #include #include digitout(int count, int digit) { printf("%d", digit); if (count%5==0) printf(" "); if (count%20==0) printf("\n"); if (count%100==0) printf("\n"); } int main(int argc, char *argv[]) { register int count, i, j, k, len, n, nines, predigit, q, x; int *a; count=-2; predigit=0; nines=0; if (--argc) n=atoi(argv[1])+2; else { fprintf(stderr, "Digits of pi -- usage: %s integer\n returns the requested number of decimal digits of pi, give or take one\n", argv[0]); return 1; } len=10*n/3; a=(int *) malloc(len * sizeof(int)); for(j=0; j=0; i--) { x=10**(a+i)+q*(i+1); *(a+i)=x%(2*i+1); q=x/(2*i+1); } *a=q%10; q=q/10; switch(q) { case 10: digitout(++count, predigit +1); for(k=0; k