**梅钦类公式**（英语：**Machin-like formula**）是数学中计算圆周率的一个常用技巧，它是梅欽公式的推广，梅钦公式的形式为

{\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**)** |

其中， a\_{n} 和 b\_{n} 为正 整数，且 {\displaystyle a\_{n}<b\_{n}}， c\_n 为非零整数，且c\_{0} 为正整数。

梅钦类公式的应用可结合反正切函数的泰勒级数展开：


|  |  |  |  |  |  |  |
| --- | --- | --- | --- | --- | --- | --- |
| {\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;
 printf("%9cMachin%6cpi=16arctan(1/5)-4arctan(1/239)\nPlease input a number.\n",32,32);
 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;
}

```

參考文獻
----

* 埃里克·韦斯坦因.. MathWorld.
* The constant π （页面存档备份，存于）
* Machin's Merit （页面存档备份，存于） at MathPages
* Archimedes' constant pi - Machin's formula gives a proof for the John Machin`s formula
* GUIDORIZZI, Hamilton Luiz. *Um Curso de Cálculo*, vol. 4. 3ª edição. Rio de Janeiro: Livros Técnicos e Científicos Editora, 1999.