SHARE
TWEET

Untitled

a guest Feb 24th, 2020 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<bits/stdc++.h> //4.9 DM
  2. using namespace std;
  3.  
  4. double leibniz()
  5. {
  6.      double pi=0,x=1,y;
  7.  
  8.     for(double i=0;i<=1000000;i++)
  9.     {
  10.         int l=i;
  11.         if(l%2==0)x=1;
  12.         else x=-1;
  13.         double val= (2*i+1);
  14.         pi+=(x/val);
  15.     }
  16.     return 4*pi;
  17. }
  18.  
  19. double better_leibniz()
  20. {
  21.      double pi=0,x=1.0,y,val,val1,z;
  22.  
  23.     for(double i=0;i<=1000000;i++)
  24.     {
  25.         val=(4.0*i+1.0);
  26.         val1=(4.0*i+3.0);
  27.         y=(1.0/val);
  28.         z=(1.0/val1);
  29.         pi+=z*y;
  30.  
  31.     }
  32.     return 8.0*pi;
  33. }
  34.  
  35.  
  36. double marchins()
  37. {
  38.      double pi=0,x,y,val,val1,z;
  39.      x=atan(1.0/5.0);
  40.      y=atan(1.0/239.0);
  41.      pi=16.0*x;
  42.      x=4.0*y;
  43.      pi=pi-x;
  44.     return pi;
  45. }
  46.  
  47.  
  48. double david()
  49. {
  50.      double pi=0,x=1,y,val,val1,z,a,b,c,d;
  51.  
  52.     for(double i=0;i<=27;i++)
  53.     {
  54.         x*=16;
  55.         a=4.0/(8.0*i+1.0);
  56.         b=2.0/(8.0*i+4.0);
  57.         c=1.0/(8.0*i+5.0);
  58.         c=1.0/(8.0*i+6.0);
  59.         z=(1.0/val1);
  60.         y=a-b-c-d;
  61.         pi+=(y/x*16.0);
  62.  
  63.     }
  64.     return pi;
  65. }
  66.  
  67.  
  68.  
  69.  
  70. int main()
  71. {
  72.     double pi,x,y,z,w,av;
  73.     pi=3.14159265358979323846264338327950288419716939937510;
  74.     w=leibniz();
  75.     x=better_leibniz();
  76.     y=marchins();
  77.     z=david();
  78.     printf("normal leibniz pi= %.51f\n",w);
  79.     printf("better leibniz pi= %.51f\n",x);
  80.     printf("marchins pi= %.51f\n",y);
  81.     printf("david pi= %.51f\n\n",z);
  82.     printf("original pi= %.51f\n\n",pi);
  83.     double p=1e9,index=1;
  84.  
  85.         if(abs(pi-w)<p) p=abs(pi-w),index=1,av=w;
  86.         if(abs(pi-x)<p) p=abs(pi-x),index=2,av=x;
  87.         if(abs(pi-y)<p) p=abs(pi-y),index=3,av=y;
  88.         if(abs(pi-z)<p) p=abs(pi-z),index=4,av=z;
  89.         if(index==1) printf("leibniz is better, pi= %.51f\n",w);
  90.         if(index==2) printf("better leibniz is better, pi= %.51f\n",x);
  91.         if(index==3) printf("marchins is better, pi= %.51f\n",y);
  92.         if(index==4) printf("david is better, pi= %.51f\n",z);
  93.  
  94.  
  95.  
  96. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top