用计算机来计算圆周率

用计算机来计算圆周率

  目前PC机上流行的最快的圆周率计算程序是PiFast。它除了计算圆周率,还可以计算e和sqrt(2)。PiFast可以利用磁盘缓存,突破物理内存的限制进行超高精度的计算,最高计算位数可达240亿位,并提供基于Fabrice Bellard公式的验算功能。
最高记录:12,884,901,372位
时间:2000年10月10日
记录创造者:Shigeru Kondo
所用程序:PiFast ver3.3
机器配置:Pentium III 1G, 1792M RAM,WindowsNT4.0,40GBx2(IDE,FastTrak66)
计算时间:1,884,375秒 (21.8天)
验算时间:29小时

·G++编译器中的运算程序微机WindowsXP中Dev-cpp中的运算程序(30000位)(C++)

 1 #include  < iostream >
 2 #include  < fstream >
 3
 4 #define  N 30010
 5
 6 using   namespace  std;
 7
 8 void  mult ( int   * a, int  b, int   * s)
 9 {
10    for (int i=N,c=0;i>=0;i--)
11    {
12        int y=(*(a+i))*b+c;
13        c=y/10;
14        *(s+i)=y%10;
15    }

16}

17
18 void  divi ( int   * a, int  b, int   * s)
19 {
20    for (int i=0,c=0;i<=N;i++)
21    {
22        int y=(*(a+i))+c*10;
23        c=y%b;
24        *(s+i)=y/b;
25    }

26}

27
28 void  incr( int   * a, int   * b, int   * s)
29 {
30    for (int i=N,c=0;i>=0;i--)
31    {
32        int y=(*(a+i))+(*(b+i))+c;
33        c=y/10;
34        *(s+i)=y%10;
35    }

36}

37
38 bool  eqs( int   * a, int   * b)
39 {
40    int i=0;
41    while (((*(a+i))==(*(b+i)))&&(i<=N)) i++;
42    return i>N;
43}

44
45 int  main( int  argc,  char   * argv[])
46 {
47    int lpi[N+1],lls[N+1],lsl[N+1],lp[N+1];
48    int *pi=lpi,*ls=lls,*sl=lsl,*p=lp;
49    for (int i=0;i<=N;i++)*(pi+i)=*(ls+i)=*(sl+i)=*(p+i)=0;
50    memset(pi,0,sizeof(pi));
51    memset(ls,0,sizeof(ls));
52    memset(sl,0,sizeof(sl));
53    memset(p,0,sizeof(p));
54    *pi=*ls=*sl=1;
55    for (int i=1;true;i++)
56    {
57        mult(ls,i,sl);
58        divi(sl,2*i+1,ls);
59        incr(pi,ls,p);
60        if (eqs(pi,p)) break;
61        int *t;
62        t=p;
63        p=pi;
64        pi=t;
65        if (i%50==0) cout << i << "   ";
66    }

67    cout << endl;
68    mult(p,2,pi);
69    ofstream fout("pi.txt");
70    fout << *pi << ".";
71    for (int i=1;i<=N;i++)
72    {
73        fout << *(pi+i);
74        if (i%10==0) fout << " ";
75        if (i%80==0) fout << endl;
76    }

77    return 0;
78}


注:①运行时会有数据弹出,那是无关紧要的,只为了加快了感觉速度;
   ②最后的txt文本里有30010位,其中最后10位可能是错的。

你可能感兴趣的:(用计算机来计算圆周率)