怎么求Pi的值

Posted by 刘知安 on 2019-06-16
文章目录
  1. 怎么用计算机求解$\pi$的值?
  • Calculating Pi (π)
    1. Calculating Pi (π)
    2. Measuring circles
    3. Using Polygons to approximate Pi (π)
    4. Calculating Pi (π) using infinite series
  • 程序模拟
  • 怎么用计算机求解$\pi$的值?

    我只是一只搬运工,下文中提供了两种求解方法,在最后,我会用程序来模拟一下求解的速度与精确度,>文章来源<

    Calculating Pi (π)

    In some ways Pi (π) is a really straightforward number – calculating Pi simply involves taking any circle and dividing its circumference by its diameter.

    \pi = \frac{Circumference}{Diameter}

    On the other hand Pi (π) is the first number we learn about at school where we can’t write it as an exact decimal – it is a mysterious number which has digits which go on forever and has fascinated people for thousands of years.

    We learn that we can start to write down Pi (π)  = 3.141592653589….. but that we can never finish it.  Pi (π) goes on forever and has no repeating pattern to its digits – it is what is called an irrational number.  In fact if you search long enough within the digits of Pi (π) you can find any number, including your birthday.

    Pi (π) is also a really useful number. It appears everywhere in mathematics and also has countless uses in Engineering and Science.  Lots of things are round, and whenever something is round, Pi (π) usually becomes important.  For example if an engineer wants to calculate the volume of a water pipe they will use the following formula for a cylinder:

    V=\pi r^2 h

    (Where r is the radius of the pipe and  h is the height of the pipe.)

    Calculating Pi (π)

    Because Pi (π) has so many important uses, then we need to be able to start to calculate it, at least to several decimal places accuracy.  Someone had to come up with the approximate value for Pi (π) which appears on your calculator – it didn’t get there by magic!

    Measuring circles

    The first and most obvious way to calculate Pi (π) is to take the most perfect circle you can, and then measure its circumference and diameter to work out Pi (π).  This is what ancient civilisations would have done and it is how they would have first realised that there is a constant ratio hidden within every circle.  The problem with this method is accuracy – can you trust your tape measure to deliver Pi (π) correct to 10 or more decimal places?

    HexagonsUsing Polygons to approximate Pi (π)

    The Ancient Greek mathematician Archimedes came up with an ingenious method for calculating an approximation of Pi (π).   Archimedes began by inscribing a regular hexagon inside a circle and then circumscribing another regular hexagon outside the same circle.  He was then able to calculate the exact circumferences and diameters of the hexagons and could therefore obtain a rough approximation of Pi (π) by dividing the circumference by the diameter.

    Archimedes then found a way to double the number of sides of his hexagons. He could then find a more accurate approximation of Pi (π) by using polygons with more sides, which were closer to the circle. He did this four times until he was using 96 sided polygons. Archimedes calculated the circumference and diameter exactly and therefore could approximate Pi (π) to being between\frac{223}{71} and \frac{22}{7}. The fraction \frac{22}{7} has remained as one of the most popular and memorable approximations of Pi (π) ever since.

    Around 600 years after Archimedes, the Chinese mathematician Zu Chongzhi used a similar method to inscribe a regular polygon with 12,288 sides.  This produced an approximation of Pi (π) as  \frac{355}{113} which is correct to six decimal places.  It was nearly 600 more years until a totally new method was devised that improved upon this approximation.

    Calculating Pi (π) using infinite series

    Mathematicians eventually discovered that there are in fact exact formulas for calculating Pi (π).  The only catch is that each formula requires you to do something an infinite number of times.  (Which makes sense given that the digits of Pi (π) go on forever.)  One of the amazing things which interests people about Pi (π) is that there isn’t just one formula, but a large number of different ones for people to study.

    One of the most well known and beautiful ways to calculate Pi (π) is to use the Gregory-Leibniz Series:

    \frac{\pi}{4}=1-\frac{1}{3}+\frac{1}{5}-\frac{1}{7}+\frac{1}{9}-\ldots

    If you continued this pattern forever you would be able to calculate \frac{\pi}{4} exactly and then just multiply it by 4 in order to get \pi.. If however you start to add up the first few terms, you will begin to get an approximation for Pi (π). The problem with the series above is that you need to add up a lot of terms in order to get an accurate approximation of Pi (π).  You need to add up more than 300 terms in order to produce Pi (π) accurate to two decimal places!

    Another series which converges more quickly is the Nilakantha Series which was developed in the 15th century. Converges more quickly means that you need to work out fewer terms for your answer to become closer to Pi (π) .

    Nilakantha Series: \pi= 3+ \frac{4}{2\times 3 \times 4}-\frac{4}{4\times 5 \times 6}+\frac{4}{6\times 7 \times 8}-\frac{4}{8\times 9 \times 10}+\ldots

    Mathematicians have also found other more efficient series for calculating Pi (π). Computer programs can add up more and more terms, calculating Pi (π) to extraordinary degrees of accuracy. In 2014 the world record was that a computer has calculated Pi (π) correct to 13,300,000,000,000 decimal places.

    Before the advent of computers it was much harder to calculate Pi (π). In the 19th Century William Shanks took 15 years to calculate Pi (π) correct to 707 decimal places. Unfortunately it was later found that he had made a mistake and was only right to 527 decimal places! The nine or 10 digits of Pi (π) which you see on your calculator have been known about probably since 1400.

    Now that you know how to calculate Pi (π), you could always try your hand at memorising the decimal places of Pi (π). The world record is currently held by Chao Lu of China who learnt Pi (π) correct to 67,890 decimal places in 2005. On the other hand, you could simply use the following mnemonic for learning the first six decimal places of Pi (π): “How I wish I could calculate Pi”

    The length of each word corresponds to a digit in Pi (π).

    HowI wish I could calculatePi
    3141592

     

    上面这句话还是很有意思的,就是教我们怎来记pi的值,How I wish I could calculate pi,每个单词的字母个数就对应了pi的值,即3.141592。

    程序模拟

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    #include<iostream>
    #include<cmath>
    #include<iomanip>
    using namespace std;

    double calculate_pi_1(int pricise, int *sum_cnt)
    {
    // sum_cnt是一个输出参数
    double last_value = 0;
    double cur_value = last_value;
    int sign = 1;
    int n = 1;
    while (1)
    {
    cur_value += sign * 4 / double(2*n-1);
    (*sum_cnt)++;
    // 精确到多少位,需要在它的下一位进行判断
    if (abs(cur_value - last_value) < pow(10, -pricise - 1))
    // 如果两次迭代的插值小于10^-p-1,就退出
    break;
    else
    {
    last_value = cur_value;
    sign = -sign; // 变号
    n++; // 计算下一项
    }
    }
    return cur_value;
    }

    double calculate_pi_2(int pricise,int *sum_cnt)
    {
    // sum_cnt是一个输出参数
    double last_value=3;
    double cur_value=last_value;
    int sign=1;
    int n=1;
    while (1)
    {
    cur_value+=sign*4/ double((n+1)*(n+2)*(n+3));
    (*sum_cnt)++;
    // 精确到多少位,需要在它的下一位进行判断
    if (abs(cur_value - last_value) < pow(10, -pricise-1))
    // 如果两次迭代的插值小于10^-p-1,就退出
    break;
    else
    {
    last_value=cur_value;
    sign=-sign; // 变号
    n++; // 计算下一项
    }
    }
    return cur_value;
    }

    int main()
    {
    cout << "*************第一种方法*************" << endl;
    for (int i = 2; i <=6;i++)
    {
    int sum_cnt = 0;
    double pi = 0;
    pi = calculate_pi_1(i, &sum_cnt);
    // 控制输出位数
    cout << fixed << setprecision(i) << pi << endl;
    cout <<"迭代次数" << sum_cnt<<endl;

    }
    cout<<"*************第二种方法*************"<<endl;
    for (int i = 2; i <= 6;i++)
    {
    int sum_cnt = 0;
    double pi = 0;
    pi = calculate_pi_2(i, &sum_cnt);
    // 控制输出位数
    cout << fixed << setprecision(i) << pi << endl;
    cout << "迭代次数"<<sum_cnt << endl;

    }
    return 0;

    }

    从运行结果上来看,第一种方法比较精确,但是需要的迭代次数比较多。第二种方法精确度不是很高,不知道是不是我程序模拟的方式不对,如有错误,还请指出。
    在这里插入图片描述