# Machin-like formula

In mathematics, Machin-like formulas are a popular technique for computing π (the ratio of the circumference to the diameter of a circle) to a large number of digits. They are generalizations of John Machin's formula from 1706:
    ${\frac {\pi }{4}}=4\arctan {\frac {1}{5}}-\arctan {\frac {1}{239}}$
which he used to compute π to 100 decimal places.
Machin-like formulas have the form
where $c_{0}$ is a positive integer, $c_{n}$ are signed non-zero integers, and $a_{n}$ and $b_{n}$ are positive integers such that $a_{n}<b_{n}$.
These formulas are used in conjunction with Gregory's series, the Taylor series expansion for arctangent:

## Derivation

The angle addition formula for arctangent asserts that
if $-{\frac {\pi }{2}}<\arctan {\frac {a_{1}}{b_{1}}}+\arctan {\frac {a_{2}}{b_{2}}}<{\frac {\pi }{2}}.$ All of the Machin-like formulas can be derived by repeated application of equation 3. As an example, we show the derivation of Machin's original formula one has: ${\begin{aligned}2\arctan {\frac {1}{5}}&=\arctan {\frac {1}{5}}+\arctan {\frac {1}{5}}\\&=\arctan {\frac {1\cdot 5+1\cdot 5}{5\cdot 5-1\cdot 1}}\\&=\arctan {\frac {10}{24}}\\&=\arctan {\frac {5}{12}},\end{aligned}}$ and consequently ${\begin{aligned}4\arctan {\frac {1}{5}}&=2\arctan {\frac {1}{5}}+2\arctan {\frac {1}{5}}\\&=\arctan {\frac {5}{12}}+\arctan {\frac {5}{12}}\\&=\arctan {\frac {5\cdot 12+5\cdot 12}{12\cdot 12-5\cdot 5}}\\&=\arctan {\frac {120}{119}}.\end{aligned}}$ Therefore also ${\begin{aligned}4\arctan {\frac {1}{5}}-{\frac {\pi }{4}}&=4\arctan {\frac {1}{5}}-\arctan {\frac {1}{1}}\\&=4\arctan {\frac {1}{5}}+\arctan {\frac {-1}{1}}\\&=\arctan {\frac {120}{119}}+\arctan {\frac {-1}{1}}\\&=\arctan {\frac {120\cdot 1+(-1)\cdot 119}{119\cdot 1-120\cdot (-1)}}\\&=\arctan {\frac {1}{239}},\end{aligned}}$ and so finally ${\frac {\pi }{4}}=4\arctan {\frac {1}{5}}-\arctan {\frac {1}{239}}.$
An insightful way to visualize equation 3 is to picture what happens when two complex numbers are multiplied together:
    $(b_{1}+a_{1}\mathrm {i} )\cdot (b_{2}+a_{2}\mathrm {i} )$
       $=b_{1}b_{2}+a_{2}b_{1}\mathrm {i} +a_{1}b_{2}\mathrm {i} -a_{1}a_{2}$
The angle associated with a complex number $(b_{n}+a_{n}\mathrm {i} )$ is given by:
    $\arctan {\frac {a_{n}}{b_{n}}}$
Thus, in equation 4, the angle associated with the product is:
    $\arctan {\frac {a_{1}b_{2}+a_{2}b_{1}}{b_{1}b_{2}-a_{1}a_{2}}}$
Note that this is the same expression as occurs in equation 3. Thus equation 3 can be interpreted as saying that multiplying two complex numbers means adding their associated angles (see multiplication of complex numbers).
The expression:
    $c_{n}\arctan {\frac {a_{n}}{b_{n}}}$
is the angle associated with:
    $(b_{n}+a_{n}\mathrm {i} )^{c_{n}}$
Equation 1 can be re-written as:
    $k\cdot (1+\mathrm {i} )^{c_{0}}=\prod _{n=1}^{N}(b_{n}+a_{n}\mathrm {i} )^{c_{n}}$
Here $k$ is an arbitrary constant that accounts for the difference in magnitude between the vectors on the two sides of the equation. The magnitudes can be ignored, only the angles are significant.

### Using complex numbers

Other formulas may be generated using complex numbers. For example, the angle of a complex number (a+bi){\textstyle (a+b\mathrm {i} )} is given by arctan⁡ b/a {\textstyle \arctan {\frac {b}{a}}} and, when one multiplies complex numbers, one adds their angles. If a=b{\textstyle a=b} then arctan⁡ b/a {\textstyle \arctan {\frac {b}{a}}} is 45 degrees or π/4 {\textstyle {\frac {\pi }{4}}} radians. This means that if the real part and complex part are equal then the arctangent will equal π/4 {\textstyle {\frac {\pi }{4}}}. Since the arctangent of one has a very slow convergence rate if we find two complex numbers that when multiplied will result in the same real and imaginary part we will have a Machin-like formula. An example is (2+i){\textstyle (2+\mathrm {i} )} and (3+i){\textstyle (3+\mathrm {i} )}. If we multiply these out we will get (5+5i){\textstyle (5+5\mathrm {i} )}. Therefore, arctan⁡ 1/2+arctan⁡ 1/3=π/4 {\textstyle \arctan {\frac {1}{2}}+\arctan {\frac {1}{3}}={\frac {\pi }{4}}}.
If you want to use complex numbers to show that π/4=4arctan⁡ 1/5−arctan⁡ 1/239 {\textstyle {\frac {\pi }{4}}=4\arctan {\frac {1}{5}}-\arctan {\frac {1}{239}}}, you first must know that raising a complex number to a real power $k$ implies multiplying its anomaly (angle) by $k$, and that the anomaly of the product of two complex numbers is equal to the sum of their anomalies. Since it can by shown, by doing the calculation, that (5+i)$^4$(239−i)=(1+i)⋅2$^2$⋅13$^4${\textstyle (5+\mathrm {i} )^{4}(239-\mathrm {i} )=(1+\mathrm {i} )\cdot 2^{2}\cdot 13^{4}}, i.e. that the real and imaginary parts of both sides are equal, and since that equality is equivalent to: 4arctan⁡ 1/5−arctan⁡ 1/239=π/4 {\textstyle 4\arctan {\frac {1}{5}}-\arctan {\frac {1}{239}}={\frac {\pi }{4}}}, the latter equality is also demonstrated.

## Lehmer's measure

One of the most important parameters that characterize computational efficiency of a Machin-like formula is the Lehmer's measure, defined as
    ${\it {\lambda }}=\sum _{n=1}^{N}{\frac {1}{\log _{10}(b_{n}/a_{n})}}$.
In order to obtain the Lehmer's measure as small as possible, it is necessary to decrease the ratio of positive integers $a_{n}/b_{n}$ in the arctangent arguments and to minimize the number of the terms in the Machin-like formula. Nowadays at $a_{n}=1$ the smallest known Lehmer's measure is $\lambda \approx 1.51244$ due to H. Chien-Lih (1997), whose Machin-like formula is shown below. It is very common in the Machin-like formulas when all numerators $a_{n}=1~.$

## Two-term formulas

In the special case where the numerator $a_{n}=1$, there are exactly four solutions having only two terms. All four were found by John Machin in 1705–1706, but only one of them became widely known when it was published in William Jones's book Synopsis Palmariorum Matheseos, so the other three are often attributed to other mathematicians. These are
Euler's 1737 (known to Machin 1706):
    ${\frac {\pi }{4}}=\arctan {\frac {1}{2}}+\arctan {\frac {1}{3}}$
Hermann's 1706 (known to Machin 1706):
    ${\frac {\pi }{4}}=2\arctan {\frac {1}{2}}-\arctan {\frac {1}{7}}$
Hutton's or Vega's (known to Machin 1706):
    ${\frac {\pi }{4}}=2\arctan {\frac {1}{3}}+\arctan {\frac {1}{7}}$
and Machin's 1706:
    ${\frac {\pi }{4}}=4\arctan {\frac {1}{5}}-\arctan {\frac {1}{239}}$ .
In the general case, where the value of a numerator $a_{n}$ is not restricted, there are infinitely many other solutions. For example:
    ${\frac {\pi }{4}}=22\arctan {\frac {1}{28}}+\arctan {\frac {1744507482180328366854565127}{98646395734210062276153190241239}}$
or

### Example

The adjacent diagram demonstrates the relationship between the arctangents and their areas. From the diagram, we have the following:
    ${\begin{array}{ll}{\rm {area}}(PON)&={\rm {area}}(MOF)=\pi \times {\frac {\angle MOF}{2\pi }}=\angle MEF=\arctan {1 \over 2}\\{\rm {area}}(POM)&={\rm {area}}(NOF)=\arctan {1 \over 3}\\{\rm {area}}(POF)&={\pi \over 4}={\rm {area}}(PON)+{\rm {area}}(NOF)=\arctan {1 \over 2}+\arctan {1 \over 3}\\{\rm {area}}(MON)&=\arctan {1 \over 7}\\{\rm {area}}(PON)=\arctan {1 \over 2}&={\rm {area}}(POM)+{\rm {area}}(MON)=\arctan {1 \over 3}+\arctan {1 \over 7},\end{array}}$
a relation which can also be found by means of the following calculation within the complex numbers
    $(3+\mathrm {i} )(7+\mathrm {i} )=21-1+(3+7)\mathrm {i} =10\cdot (2+\mathrm {i} ).$

## More terms

The 2002 record for digits of π, 1,241,100,000,000, was obtained by Yasumasa Kanada of Tokyo University. The calculation was performed on a 64-node Hitachi supercomputer with 1 terabyte of main memory, performing 2 trillion operations per second. The following two equations were both used:
    ${\frac {\pi }{4}}=12\arctan {\frac {1}{49}}+32\arctan {\frac {1}{57}}-5\arctan {\frac {1}{239}}+12\arctan {\frac {1}{110443}}$
    Kikuo Takano (1982).
    ${\frac {\pi }{4}}=44\arctan {\frac {1}{57}}+7\arctan {\frac {1}{239}}-12\arctan {\frac {1}{682}}+24\arctan {\frac {1}{12943}}$
    F. C. M. Størmer (1896).
Two equations are used so that one can check they both give the same result; it is helpful if the equations used to cross-check the result reuse some of the arctangent arguments (note the reuse of 57 and 239 above), so that the process can be simplified by only computing them once, but not all of them, in order to preserve their independence.
Machin-like formulas for π can be constructed by finding a set of $m$ integers $b_{n},n=1..m$, where all the prime factorisations of ⁠$b_{n}^{2}+1$⁠, taken together, use a number of distinct primes $\leq m$, and then using either linear algebra or the LLL basis-reduction algorithm to construct linear combinations of arctangents of ${\frac {1}{b_{n}}}$. For example, in the Størmer formula above, we have
    $57^{2}+1=2\cdot 5^{3}\cdot 13$
    $239^{2}+1=2\cdot 13^{4}$
    $682^{2}+1=5^{3}\cdot 61^{2}$
    $12943^{2}+1=2\cdot 5^{4}\cdot 13^{3}\cdot 61$
so four expressions whose factors are powers of only the four primes 2, 5, 13 and 61.
In 1993 Jörg Uwe Arndt found the 11-term formula:
    ${\begin{aligned}{\frac {\pi }{4}}=&\;36462\arctan {\frac {1}{390112}}+135908\arctan {\frac {1}{485298}}+274509\arctan {\frac {1}{683982}}\\&-39581\arctan {\frac {1}{1984933}}+178477\arctan {\frac {1}{2478328}}-114569\arctan {\frac {1}{3449051}}\\&-146571\arctan {\frac {1}{18975991}}+61914\arctan {\frac {1}{22709274}}-69044\arctan {\frac {1}{24208144}}\\&-89431\arctan {\frac {1}{201229582}}-43938\arctan {\frac {1}{2189376182}}\\\end{aligned}}$
using the set of 11 primes $\{2,5,13,17,29,37,53,61,89,97,101\}.$
Another formula where 10 of the $\arctan$-arguments are the same as above has been discovered by Hwang Chien-Lih (黃見利) (2004), so it is easier to check they both give the same result:
    ${\begin{aligned}{\frac {\pi }{4}}=&\;36462\arctan {\frac {1}{51387}}+26522\arctan {\frac {1}{485298}}+19275\arctan {\frac {1}{683982}}\\&-3119\arctan {\frac {1}{1984933}}-3833\arctan {\frac {1}{2478328}}-5183\arctan {\frac {1}{3449051}}\\&-37185\arctan {\frac {1}{18975991}}-11010\arctan {\frac {1}{22709274}}+3880\arctan {\frac {1}{24208144}}\\&-16507\arctan {\frac {1}{201229582}}-7476\arctan {\frac {1}{2189376182}}\\\end{aligned}}$
You will note that these formulas reuse all the same arctangents after the first one. They are constructed by looking for numbers where ⁠$b^{2}+1$⁠ is divisible only by primes less than 102.
The most efficient currently known Machin-like formula for computing π is:
    ${\begin{aligned}{\frac {\pi }{4}}=&\;183\arctan {\frac {1}{239}}+32\arctan {\frac {1}{1023}}-68\arctan {\frac {1}{5832}}\\&+12\arctan {\frac {1}{110443}}-12\arctan {\frac {1}{4841182}}-100\arctan {\frac {1}{6826318}}\\\end{aligned}}$
    (Hwang Chien-Lih, 1997)
where the set of primes is $\{2,5,13,229,457,1201\}.$
A further refinement is to use "Todd's Process", as described in; this leads to results such as
    ${\begin{aligned}{\frac {\pi }{4}}=&\;183\arctan {\frac {1}{239}}+32\arctan {\frac {1}{1023}}-68\arctan {\frac {1}{5832}}\\&+12\arctan {\frac {1}{113021}}-100\arctan {\frac {1}{6826318}}\\&-12\arctan {\frac {1}{33366019650}}+12\arctan {\frac {1}{43599522992503626068}}\\\end{aligned}}$
    (Hwang Chien-Lih, 2003)
where the large prime 834312889110521 divides the ⁠$b_{n}^{2}+1$⁠ of the last two indices. M. Wetherfield found 2004
    ${\begin{aligned}{\frac {\pi }{4}}=&\;83\arctan {\frac {1}{107}}+17\arctan {\frac {1}{1710}}-22\arctan {\frac {1}{103697}}\\&-24\arctan {\frac {1}{2513489}}-44\arctan {\frac {1}{18280007883}}\\&+12\arctan {\frac {1}{7939642926390344818}}\\&+22\arctan {\frac {1}{3054211727257704725384731479018}}.\\\end{aligned}}$
In Pi Day 2024, Matt Parker along with 400 volunteers used the following formula to hand calculate $\pi$:
${\begin{aligned}{\frac {\pi }{4}}=&\;1587\arctan {\frac {1}{2852}}+295\arctan {\frac {1}{4193}}+593\arctan {\frac {1}{4246}}\\&+359\arctan {\frac {1}{39307}}+481\arctan {\frac {1}{55603}}+625\arctan {\frac {1}{211050}}\\&-708\arctan {\frac {1}{390112}}\end{aligned}}$
It was the biggest hand calculation of $\pi$ in a century.

## More methods

There are further methods to derive Machin-like formulas for $\pi$ with reciprocals of integers. One is given by the following formula:
    ${\frac {\pi }{4}}=2^{k-1}\cdot \arctan {\frac {1}{A_{k}}}+\sum \limits _{m=1}^{M}\arctan {\frac {1}{\left\lfloor B_{k,m}\right\rfloor }}+\arctan {\frac {1}{B_{k,M+1}}},$
where
    $a_{0}:=0$
and recursively
    $a_{k}:={\sqrt {2+a_{k-1}}},\;A_{k}:=\left\lfloor {\frac {a_{k}}{\sqrt {2-a_{k-1}}}}\right\rfloor$
and
    $B_{k,1}:={\frac {2}{\left({\dfrac {A_{k}+\mathrm {i} }{A_{k}-\mathrm {i} }}\right)^{2^{k-1}}-\mathrm {i} }}-\mathrm {i}$
and recursively
    $B_{k,m}:={\frac {1+\left\lfloor B_{k,m-1}\right\rfloor B_{k,m-1}}{\left\lfloor B_{k,m-1}\right\rfloor -B_{k,m-1}}}~.$
E.g., for $k=4$ and $M=5$ we get:
    ${\begin{aligned}{\frac {\pi }{4}}=&\;8\arctan {\frac {1}{10}}-\arctan {\frac {1}{84}}-\arctan {\frac {1}{21342}}\\&-\arctan {\frac {1}{991268848}}-\arctan {\frac {1}{193018008592515208050}}\\&-\arctan {\frac {1}{197967899896401851763240424238758988350338}}\\&-\arctan {\frac {1}{117573868168175352930277752844194126767991915008537018836932014293678271636885792397}}\end{aligned}}$
This is verified by the following MuPAD code:
meaning
    ${\begin{aligned}z:=&\,(10+\mathrm {i} )^{8}\cdot (84-\mathrm {i} )\cdot (21342-\mathrm {i} )\cdot (991268848-\mathrm {i} )\cdot (193018008592515208050-\mathrm {i} )\\&\cdot (197967899896401851763240424238758988350338-\mathrm {i} )\\&\cdot (117573868168175352930277752844194126767991915008537018836932014293678271636885792397-\mathrm {i} )\\\;=&\,(1+\mathrm {i} )\cdot \Re (z)~.\end{aligned}}$

## Efficiency

For large computations of π, the binary splitting algorithm can be used to compute the arctangents much, much more quickly than by adding the terms in the Taylor series naively one at a time. In practical implementations such as y-cruncher, there is a relatively large constant overhead per term plus a time proportional to $1/\log b_{n}$, and a point of diminishing returns appears beyond three or four arctangent terms in the sum; this is why the supercomputer calculation above used only a four-term version.
It is not the goal of this section to estimate the actual run time of any given algorithm. Instead, the intention is merely to devise a relative metric by which two algorithms can be compared against each other.
Let $N_{d}$ be the number of digits to which π is to be calculated.
Let $N_{t}$ be the number of terms in the Taylor series (see equation 2).
Let $u_{n}$ be the amount of time spent on each digit (for each term in the Taylor series).
The Taylor series will converge when:
    $\left(\left({\frac {b_{n}}{a_{n}}}\right)^{2}\right)^{N_{t}}=10^{N_{d}}$
Thus:
    $N_{t}=N_{d}\quad {\frac {\ln 10}{2\ln {\frac {b_{n}}{a_{n}}}}}$
For the first term in the Taylor series, all $N_{d}$ digits must be processed. In the last term of the Taylor series, however, there's only one digit remaining to be processed. In all of the intervening terms, the number of digits to be processed can be approximated by linear interpolation. Thus the total is given by:
    ${\frac {N_{d}N_{t}}{2}}$
The run time is given by:
    ${\mathtt {time}}={\frac {u_{n}N_{d}N_{t}}{2}}$
Combining equations, the run time is given by:
    ${\mathtt {time}}={\frac {u_{n}{N_{d}}^{2}\ln 10}{4\ln {\frac {b_{n}}{a_{n}}}}}={\frac {k\,u_{n}}{\ln {\frac {b_{n}}{a_{n}}}}}$
Where k is a constant that combines all of the other constants. Since this is a relative metric, the value of k can be ignored.
The total time, across all the terms of equation 1, is given by:
    ${\mathtt {time}}=\sum _{n=1}^{N}{\frac {u_{n}}{\ln {\frac {b_{n}}{a_{n}}}}}$
$u_{n}$ cannot be modelled accurately without detailed knowledge of the specific software. Regardless, we present one possible model.
The software spends most of its time evaluating the Taylor series from equation 2. The primary loop can be summarized in the following pseudo code:
    ${\mathtt {\text{1: term *= }}}{a_{n}}^{2}$
    ${\mathtt {\text{2: term /= }}}-{b_{n}}^{2}$
    ${\mathtt {\text{3: tmp = term / }}}(2*n+1)$
    ${\mathtt {\text{4: sum += tmp}}}$
In this particular model, it is assumed that each of these steps takes approximately the same amount of time. Depending on the software used, this may be a very good approximation or it may be a poor one.
The unit of time is defined such that one step of the pseudo code corresponds to one unit. To execute the loop, in its entirety, requires four units of time. $u_{n}$ is defined to be four.
Note, however, that if $a_{n}$ is equal to one, then step one can be skipped. The loop only takes three units of time. $u_{n}$ is defined to be three.
As an example, consider the equation:
The following table shows the estimated time for each of the terms:
| $a_{n}$ | $b_{n}$ | ${\frac {b_{n}}{a_{n}}}$ | $\ln {\frac {b_{n}}{a_{n}}}$ | $u_{n}$ | time |
| 74684 | 14967113 | 200.41 | 5.3003 | 4 | 0.75467 |
| 1 | 239 | 239.00 | 5.4765 | 3 | 0.54780 |
| 20138 | 15351991 | 762.34 | 6.6364 | 4 | 0.60274 |
The total time is 0.75467 + 0.54780 + 0.60274 = 1.9052
Compare this with equation 5. The following table shows the estimated time for each of the terms:
| $a_{n}$ | $b_{n}$ | ${\frac {b_{n}}{a_{n}}}$ | $\ln {\frac {b_{n}}{a_{n}}}$ | $u_{n}$ | time |
| 24478 | 873121 | 35.670 | 3.5743 | 4 | 1.1191 |
| 685601 | 69049993 | 100.71 | 4.6123 | 4 | 0.8672 |
The total time is 1.1191 + 0.8672 = 1.9863
The conclusion, based on this particular model, is that equation 6 is slightly faster than equation 5, regardless of the fact that equation 6 has more terms. This result is typical of the general trend. The dominant factor is the ratio between $a_{n}$ and $b_{n}$. In order to achieve a high ratio, it is necessary to add additional terms. Often, there is a net savings in time.