How is pi (π) calculated?

2020-02-02 17:38发布

How can I write a function which will return pi (π) to a given number of decimal places?

Speed is not a concern. I've been looking at http://bellard.org/pi/, but I still don't understand how to get the nth digit of pi.

9条回答
何必那么认真
2楼-- · 2020-02-02 18:11

My two cents... This might not be the fastest, but I think it's quite easy to understand. I came up with it myself during a math lecture, and I haven't really seen it anywhere else in literature. Either I'm a genius, really stupid, or don't really pay attention to reading books about math, or all of the above... :)

Anyway... Start with the unit circle. We know that x^2+y^2=1, so y=sqrt(1-x^2). We also know that the area of the unit circle is PI. If we now take the integral of the function sqrt(1-x^2) in the range 0 to 1, we will get a quarter of PI. So multiply it by 4 to get PI:

PI formula

If we would try to solve this analytically, I'm sure we would just get PI back. But it's quite easy to write a program to solve it numerically. The following one is in C:

#include <math.h>
#include <stdio.h>

void main(void) {
    double interval=0.0000001,area=0,x,y;

    for (x=0; x<1; x+=interval)
        area+=4*interval*sqrt(1-x*x);

    printf("pi ~ %.20f\n",area);
}

Running it with the above setting for interval, we get:

pi ~ 3.14159285415672595576

So 10,000,000 iterations give 6 correct decimals. Not the most efficient, but it's my baby... :)

查看更多
Bombasti
3楼-- · 2020-02-02 18:13

I believe the algorithm you're looking for is what's known as a "Spigot Algorithm." One particular kind is the BBP (Bailey-Borwein-Plouffe) formula.

I believe that's what you're looking for.

查看更多
ら.Afraid
4楼-- · 2020-02-02 18:15
pi = function () {
    let pi = 3;
    let a = 3;
    let even = false;
    let turn;

    while (a <= 90000) {
        turn = (4/((Math.pow(a, 3)) - (a)));

        if(even){
            turn = turn*-1;
            even = false;
        } else {
            even = true;
        }
        pi = pi + turn;
        a = a + 2;
    }
    return pi;
};
查看更多
甜甜的少女心
5楼-- · 2020-02-02 18:16

"π IN THE MANDELBROT SET" explores the curious relationship between a sequence of points on the complex plane and how computing their "Mandelbrot number" (for lack a better term ... the number of iterations required to determine that the points in the sequence are not members of the Mandelbrot set) relates to PI.

Practical? Probably not.

Unexpected and interesting? I think so.

查看更多
手持菜刀,她持情操
6楼-- · 2020-02-02 18:17

I'd start with the formula

pi = 16 arctan (1/5) - 4 arctan (1/239)

Google will easily find a proof for this formula that normal human beings can understand, and a formula to calculate the arc tangent function. This will allow you to calculate a few thousand decimal digits of pi quite easily and quickly.

查看更多
▲ chillily
7楼-- · 2020-02-02 18:18

As an alternative to JeffH's method of storing every variation, you can just store the maximum number of digits and cut off what you don't need:

#include <string>
#include <iostream>
using std::cout; using std::endl; using std::string;

// The first 99 decimal digits taken from:
// http://www.geom.uiuc.edu/~huberty/math5337/groupe/digits.html
// Add more as needed.
const string pi =
  "1415926535"
  "8979323846"
  "2643383279"
  "5028841971"
  "6939937510"
  "5820974944"
  "5923078164"
  "0628620899"
  "8628034825"
  "342117067";

// A function in C++ that returns pi to X places
string CalcPi(const size_t decimalDigitsCount) 
{
  string returnValue = "3";
  if (decimalDigitsCount > 0)
  {
    returnValue += "." + pi.substr(0, decimalDigitsCount);
  }
  return returnValue;
} 

int main()
{
  // Loop through all the values of "pi at x digits" that we have. 
  for (size_t i = 0; i <= pi.size(); ++i) 
  {
    cout << "pi(" << i << "): " << CalcPi(i) << endl;
  } 
}

http://codepad.org/6mqDa1zj

查看更多
登录 后发表回答