# 梅欽類公式

${\displaystyle {\frac {\pi }{4}}=4\arctan {\frac {1}{5}}-\arctan {\frac {1}{239}}}$

${\displaystyle c_{0}{\frac {\pi }{4}}=\sum _{n=1}^{N}c_{n}\arctan {\frac {a_{n}}{b_{n}}}}$

(1)

${\displaystyle \arctan x=\sum _{n=0}^{\infty }{\frac {(-1)^{n}}{2n+1}}x^{2n+1}=x-{\frac {x^{3}}{3}}+{\frac {x^{5}}{5}}-{\frac {x^{7}}{7}}+...}$

(4)

## 导出

${\displaystyle \sin(\alpha +\beta )=\sin \alpha \cos \beta +\cos \alpha \sin \beta }$
${\displaystyle \cos(\alpha +\beta )=\cos \alpha \cos \beta -\sin \alpha \sin \beta }$

${\displaystyle -{\frac {\pi }{2}}<\arctan {\frac {a_{1}}{b_{1}}}+\arctan {\frac {a_{2}}{b_{2}}}<{\frac {\pi }{2}}.}$

${\displaystyle \arctan {\frac {a_{1}}{b_{1}}}+\arctan {\frac {a_{2}}{b_{2}}}=\arctan {\frac {a_{1}b_{2}+a_{2}b_{1}}{b_{1}b_{2}-a_{1}a_{2}}},}$

(2)

${\displaystyle 2\arctan {\frac {1}{5}}}$
${\displaystyle =\arctan {\frac {1}{5}}+\arctan {\frac {1}{5}}}$
${\displaystyle =\arctan {\frac {1\times 5+1\times 5}{5\times 5-1\times 1}}}$
${\displaystyle =\arctan {\frac {10}{24}}}$
${\displaystyle =\arctan {\frac {5}{12}}}$
${\displaystyle 4\arctan {\frac {1}{5}}}$
${\displaystyle =2\arctan {\frac {1}{5}}+2\arctan {\frac {1}{5}}}$
${\displaystyle =\arctan {\frac {5}{12}}+\arctan {\frac {5}{12}}}$
${\displaystyle =\arctan {\frac {5\times 12+5\times 12}{12\times 12-5\times 5}}}$
${\displaystyle =\arctan {\frac {120}{119}}}$
${\displaystyle 4\arctan {\frac {1}{5}}-{\frac {\pi }{4}}}$
${\displaystyle =4\arctan {\frac {1}{5}}-\arctan {\frac {1}{1}}}$
${\displaystyle =4\arctan {\frac {1}{5}}+\arctan {\frac {-1}{1}}}$
${\displaystyle =\arctan {\frac {120}{119}}+\arctan {\frac {-1}{1}}}$
${\displaystyle =\arctan {\frac {120\times 1+(-1)\times 119}{119\times 1-120\times (-1)}}}$
${\displaystyle =\arctan {\frac {1}{239}}}$
${\displaystyle {\frac {\pi }{4}}=4\arctan {\frac {1}{5}}-\arctan {\frac {1}{239}}}$

## 用梅钦公式编程计算圆周率(C++)

#include<stdio.h>
#include<iostream>
using namespace std;
int main(void)
{   //本程序每四位数输出，如果请求计算的位数不是4的整数倍，最后输出可能会少1~3位数
long a[2]={956,80},b[2]={57121,25},i=0,j,k,p,q,r,s=2,t,u,v,N,M=10000;
cin>>N,N=N/4+3;
long *pi=new long[N],*e=new long[N];
while(i<N)pi[i++]=0;
while(--s+1)
{
for(*e=a[k=s],i=N;--i;)e[i]=0;
for(q=1;j=i-1,i<N;e[i]?0:++i,q+=2,k=!k)
for(r=v=0;++j<N;pi[j]+=k?u:-u)u=(t=v*M+(e[j]=(p=r*M+e[j])/b[s]))/q,r=p%b[s],v=t%q;
}
while(--i)(pi[i]=(t=pi[i]+s)%M)<0?pi[i]+=M,s=t/M-1:s=t/M;
for(cout<<"3.";++i<N-2;)printf("%04ld",pi[i]);
delete []pi,delete []e,cin.ignore(),cin.ignore();
return 0;
}