zxp学长告诉我两种计算pi值得办法,第一种是pi/4=1-1/3+1/5-1/7……(课本上的传统方法)

第二种方法是

计算pi的方法

这种方法的证明

 

计算pi的方法计算pi的方法

(以上由zxp学长找的资料给我的……)

用C++写出这两种方法求解pi的过程,然后比较这两种方法收敛的速度

 1 #include<iostream>
 2 #include<iomanip>
 3 #include<math.h>
 4 using namespace std;
 5 int main(){
 6     double pi1=0;
 7     double pi2;
 8     for(double i=0;i<1500000;i++){//the method in text book should use more than a million times 
 9         pi1=pi1+(1/(2*i+1))*((int)i%2?-1:(1));
10     }
11     pi1=pi1*4;
12     double t=sqrt(2);
13     double d=2;
14     double m=t;
15     for(int i=0;i<100;++i){
16         pi2=(d)/m;
17         t=sqrt(2+t);
18         m*=t;
19         d*=2;
20     }//the method zxp give me use less than a thousand times;
21     cout<<setiosflags(ios::fixed)<<setprecision(10)<<pi1<<endl;
22     cout<<setiosflags(ios::fixed)<<setprecision(10)<<pi2*2<<endl;
23     return 0;
24 }
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-17
  • 2021-10-12
  • 2022-12-23
  • 2021-07-27
  • 2021-10-25
  • 2021-09-15
猜你喜欢
  • 2021-09-22
  • 2022-02-10
  • 2021-08-14
  • 2021-05-23
  • 2021-08-05
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案