博客园  :: 首页  :: 新随笔  :: 联系 :: 管理

计算圆周率

Posted on 2010-07-13 20:00  codingsilence  阅读(245)  评论(0编辑  收藏  举报

一、

int a=10000,b,c=2800,d,e,f[2801],g;
main(){for(;b-c;)f[b++]=a/5;
for(;d=0,g=c*2;c-=14,printf("%.4d",e+d/a),e=d%a)
for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b);}

 

二、

1 #include <stdio.h>
2   
3 int main()
4 {
5   int f[8401], a, b, c = sizeof(f) / sizeof(f[0]) - 1, d, e, g;
6   for (a = 10000, b = e = 0; b != c; ) f[b++] = a / 5;
7   for (; g = c * 2; c -= 14, printf("%.4d", e + d / a), e = d % a)
8     for (d = 0, b = c; d += f[b] * a, f[b] = d % --g, --b; d *= b) d /= g--;
9 }

 

 三、

 

01 // 计算圆周率到小数点后 digits 位
02 int [] Comput(int digits)
03 {
04   pi = 0;
05   foreach (Term term in list)
06   {
07     // c * arctan(1/x) = c/x - c/(3*x^3) + c/(5*x^5) - c/(7*x^7) + ...
08     tmp = c;
09     pi += (tmp /= x);
10     for (int step = 3; tmp > 0; step += 2)
11     {
12       tmp /= (x * x);
13       pi +=或-= tmp / step;
14     }
15   }
16 }

 

四、

 

01:  #include <stdio.h>
02:  #include <stdlib.h>
03:  
04:  const int DIGITS = 21987;
05:  
06:  void pi2()
07:  {
08:    static char s[] = ")[c?jEia#44#34R......f%Zf"; // 2,762 chars => 5,000 digits
09:    for (int i = 0; i < sizeof(s) - 1; i++) {
10:      if (s[i] == '#') printf("0%c", s[++i]);
11:      else {
12:        int v = s[i];
13:        if (v == '$') v = '//';
14:        printf("%02d", v - '%' + 10);
15:      }
16:    }
17:  }
18:  
19:  int main()
20:  {
21:    int t0[] = {176, 28, 48, 96}, k0[] = {1, 1, 0, 1}, n0[] = {57, 239, 682, 12943};
22:    int m, n, r, s, i, j, k, p, d = DIGITS, z = sizeof(t0) / sizeof(t0[0]);
23:    int* t = (int *)calloc((d += 5) + 1, sizeof(int));
24:    int* pi = (int *)calloc(d + 1, sizeof(int));
25:    for (i = d; i >= 0; i--) pi[i] = 0;
26:    for (p = 0; p < z; p++) {
27:      for (k=k0[p], n=n0[p], t[i=j=d]=t0[p], i--; i >= 0; i--) t[i] = 0;
28:      for (r = 0, i = j; i >= 0; i--) {
29:        r = (m = 10 * r + t[i]) % n;
30:        t[i] = m / n;
31:        k ? (pi[i] += t[i]) : (pi[i] -= t[i]);
32:      }
33:      while (j > 0 && t[j] == 0) j--;
34:      for (k = !k, s = 3, n *= n; j > 0; k = !k, s += 2) {
35:        for (r = 0, i = j; i >= 0; i--) {
36:          r = (m = 10 * r + t[i]) % n;
37:          t[i] = m / n;
38:        }
39:        while (j > 0 && t[j] == 0) j--;
40:        for (r = 0, i = j; i >= 0; i--) {
41:          r = (m = 10 * r + t[i]) % s;
42:          m /= s;
43:          k ? (pi[i] += m) : (pi[i] -= m);
44:        }
45:      }
46:    }
47:    for (n = i = 0; i <= d; pi[i++] = r) {
48:      n = (m = pi[i] + n) / 10;
49:      if ((r = m % 10) < 0) r += 10, n--;
50:    }
51:    printf("3."); 
52:    for (i = d - 1; i >= 5; i--) putchar((int)pi[i] + '0');
53:    pi2();
54:    return 0;
55:  }