一个看起来很牛的计算Pi的程序:
int a=10000,b,c=2800,d,e,f[2801],g;
main() {
for(;b-c;)
f[b++]=a/5;
for(;d=0,g=c*2;c -=14,printf("%.4d",e+d/a),e=d%a)
for(b=c; d+=f[b]*a,f[b]=d%--g,d/=g--,--b; d*=b);
}
二、数学公式
数学家们研究了数不清的方法来计算PI,这个程序所用的公式如下:
1 2 3 k
pi = 2 + --- * (2 + --- * (2 + --- * (2 + ... (2 + ---- * (2 + ... ))...)))
3 5 7 2k+1
就是
Pi/2 = Sum{k!/(2k+1)!!, k=0,1,2,...}
其实上面的程序效率并不高,下面转贴更多Pi的公式,来源于:
http://mathworld.wolfram.com/PiFormulas.html
There are many formulas of of many types. Among others, these include series, products, geometric constructions, limits, special values, and pi iterations.
is intimately related to the properties of circles and spheres. For a circle of radius , the circumference and area are given by
(1) | |||
(2) |
Similarly, for a sphere of radius , the surface area and volume enclosed are
(3) | |||
(4) |
An exact formula for in terms of the inverse tangents of unit fractions is Machin's formula
(5) |
There are three other Machin-like formulas, as well as thousands of other similar formulas having more terms.
(6) | |||
(7) |
(Wells 1986, p. 50), which is known as the Gregory series and may be obtained by plugging into the Leibniz series for . The error after the th term of this series in the Gregory series is larger than so this sum converges so slowly that 300 terms are not sufficient to calculate correctly to two decimal places! However, it can be transformed to
(8) |
where is the Riemann zeta function (Vardi 1991, pp. 157-158; Flajolet and Vardi 1996), so that the error after terms is .
An infinite sum series to Abraham Sharp (ca. 1717) is given by
(9) |
(Smith 1953, p. 311). Additional simple series in which appears are
(10) | |||
(11) | |||
(12) | |||
(13) | |||
(14) | |||
(15) | |||
(16) | |||
(17) |
(Wells 1986, p. 53).
In 1666, Newton used a geometric construction to derive the formula
(18) | |||
(19) |
which he used to compute (Wells 1986, p. 50; Borwein et al. 1989; Borwein and Bailey 2003, pp. 105-106). The coefficients can be found from the integral
(20) | |||
(21) |
by taking the series expansion of about 0, obtaining
(22) |
(Sloane's A054387 and A054388). Using Euler's convergence improvement transformation gives
(23) | |||
(24) | |||
(25) |
(Beeler et al. 1972, Item 120).
This corresponds to plugging into the power series for the hypergeometric function ,
(26) |
Despite the convergence improvement, series (◇) converges at only one bit/term. At the cost of a square root, Gosper has noted that gives 2 bits/term,
(27) |
and gives almost 3.39 bits/term,
(28) |
where is the golden ratio. Gosper also obtained
(29) |
A spigot algorithm for is given by Rabinowitz and Wagon (1995; Borwein and Bailey 2003, pp. 141-142).
More amazingly still, a closed form expression giving a digit-extraction algorithm which produces digits of (or ) in base-16 was discovered by Bailey et al. (Bailey et al. 1997, Adamchik and Wagon 1997),
(30) |
This formula, known as the BBP formula, was discovered using the PSLQ algorithm (Ferguson et al. 1999) and is equivalent to
(31) |
A similar formula was subsequently discovered by Ferguson, leading to a two-dimensional lattice of such formulas which can be generated by these two formulas given by
(32) |
for any complex value of (Adamchik and Wagon), giving the BBP formula as the special case .
There is a series of BPP-type formulas for in powers of , the first few independent formulas of which are
(33) | |||
(34) | |||
(35) | |||
(36) | |||
(37) | |||
(38) |
Similarly, there are a series of BPP-type formulas for in powers of , the first few independent formulas of which are
(39) | |||
(40) | |||
(41) | |||
(42) | |||
(43) | |||
(44) | |||
(45) | |||
(46) | |||
(47) | |||
(48) | |||
(49) |
F. Bellard found the rapidly converging BBP-type formula
(50) |
A related integral is
(51) |
(Dalzell 1944; Dalzell 1971; Le Lionnais 1983, p. 22; Borwein et al. 2004, p. 3; Boros and Moll 2004, p. 125; Lucas 2005; Borwein et al. 2006, p. 14). This integral was known by K. Mahler in the mid-1960s and appears in an exam at the University of Sydney in November 1960 (Borwein et al. 2004, p. 3). Beukers (2000) and Boros and Moll (2004, p. 126) state this it is not clear if these exists a natural choice of rational polynomial whose integral between 0 and 1 produces , where 333/106 is the next convergent. However, an integral exists for the fourth convergent, namely
(52) |
(Lucas 2005; Bailey et al. 2006, p. 219). In fact, Lucas (2005) gives a few other such integrals.
Backhouse (1995) used the identity
(53) | |||
(54) | |||
(55) |
for positive integer and and where , , and are rational constant to generate a number of formulas for . In particular, if , then (Lucas 2005).
An even more general identity due to Wagon is given by
(56) |
(Borwein and Bailey 2003, p. 141), which holds over a region of the complex plane excluding two triangular portions symmetrically placed about the real axis, as illustrated above.
Following the discovery of the base-16 digit BBP formula and related formulas, similar formulas in other bases were investigated. Borwein and Galway have recently shown that there is no non-binary Bailey-Borwein-Plouffe-type formula, although this does not rule out a completely different scheme for digit extraction algorithms in other bases (Bailey 2002).
S. Plouffe has devised an algorithm to compute the th digit of in any base in steps.
A slew of additional identities due to Ramanujan , Catalan, and Newton are given by Castellanos (1988, pp. 86-88), including several involving sums of Fibonacci numbers. Ramanujan found
(57) |
(Hardy 1923; Hardy 1924; Hardy 1999, p. 7).
Plouffe (2006) found the beautiful formula
(58) |
An interesting infinite product formula due to Euler which relates and the th prime is
(59) | |||
(60) |
(Blatner 1997, p. 119), plotted above as a function of the number of terms in the product.
A method similar to Archimedes' can be used to estimate by starting with an -gon and then relating the area of subsequent -gons. Let be the angle from the center of one of the polygon's segments,
(61) |
then
(62) |
(Beckmann 1989, pp. 92-94).
Vieta (1593) was the first to give an exact expression for by taking in the above expression, giving
(63) |
which leads to an infinite product of nested radicals,
(64) |
(Wells 1986, p. 50; Beckmann 1989, p. 95). However, this expression was not rigorously proved to converge until Rudio (1892).
A related formula is given by
(65) |
which can be written
(66) |
where is defined using the iteration
(67) |
with (J. Munkhammar, pers. comm., April 27, 2000). The formula
(68) |
is also closely related.
A pretty formula for is given by
(69) |
where the numerator is a form of the Wallis formula for and the denominator is a telescoping sum with sum 1/2 since
(70) |
(Sondow 1997).
A particular case of the Wallis formula gives
(71) |
(Wells 1986, p. 50). This formula can also be written
(72) |
where denotes a binomial coefficient and is the gamma function (Knopp 1990). Euler obtained
(73) |
which follows from the special value of the Riemann zeta function . Similar formulas follow from for all positive integers .
An infinite sum due to Ramanujan is
(74) |
(Borwein et al. 1989; Borwein and Bailey 2003, p. 109; Bailey et al. 2006, p. 44). Further sums are given in Ramanujan (1913-14),
(75) |
and
|