2021级《程序设计原理及C语言》练习题

M. calculate pi

单点时限: 2.0 sec

内存限制: 512 MB

The series used to approximate $\pi$ , $\frac{\pi}{4} \cong 1-\frac{1}{3}+\frac{1}{5}-\frac{1}{7}+\frac{1}{9}-\frac{1}{11}+\ldots$ , converges extremely slowly. Even after 10,000 terms have been evaluated, the approximation is correct only to four digits. Using this technique to compute $\pi$ to the limit of floating-point precision would be impractical. The following series converges much more quickly:

$$\frac{\pi}{6} \cong \frac{1}{2}+\left(\frac{1}{2}\right) \frac{1}{3}\left(\frac{1}{2}\right)^{3}+\left(\frac{1}{2} \times \frac{3}{4}\right) \frac{1}{5}\left(\frac{1}{2}\right)^{5}+\left(\frac{1}{2} \times \frac{3}{4} \times \frac{5}{6}\right) \frac{1}{7}\left(\frac{1}{2}\right)^{7}+\ldots$$

Each term can be divided into three parts, as suggested by the parentheses in the formula. Figure out how each part changes from term to term and use this information to write a program that calculates $\pi$ to the limit of floating-point accuracy. Display your answer with 10 decimal places.