Methods for finding the circumference rate π and the circumference rate π

Source: Internet
Author: User

Methods for finding the circumference rate π and the circumference rate π

Method 1: Calculate the approximate value of π by probability method.

The probability method is also called the Monte Carlo method. Suppose there is a circle with a radius of 1, then the area of the 1/4 circle is equal to 1/4 π. The probability method is used to calculate the area of 1/4 circles, and 1/4 π is obtained. The specific process is as follows: Use a random function to generate x and y coordinates (the two values are 0 ~ 1), and then judge whether the points composed of the two random numbers are in the area of the 1/4 circle. If the points are in the 1/4 circle, Count. Due to the uniformity of the Point Coordinates generated by the random function, when there are enough points generated, the approximate distribution of the points inside and outside the shadow can be obtained.

# Include <stdio. h> # include <stdlib. h> # include <time. h> int main (void) {int n, sum = 0; double x, y; printf ("Enter the number of vertices:"); scanf ("% d ", & n); srand (unsigned) time (NULL); for (int I = 1; I <n; I ++) {x = (double) rand () /RAND_MAX; y = (double) rand ()/RAND_MAX; if (x * x + y * y <= 1) {sum + = 1 ;}} printf ("PI = % f \ n", (double) 4 * sum/n); return 0 ;}

Method 2: Obtain the approximate value of π by the cutover method.

The circle method uses the inner polygon of the circle to approach the circle perimeter. By constantly dividing the inner polygon by cutting and making it infinitely close to the circumference, a higher precision π value can be obtained.

# Include <stdio. h> # include <math. h> int main (void) {double x = 1, PI; int count, n = 0; int s = 6; // Number of inner circular edges printf ("Enter the number of rounds:"); scanf ("% d", & count); while (n <= count) {printf ("% d subcircle, inner side % d edge:", n, s); PI = s * x/2; printf ("PI = %. 12f \ n ", PI); x = sqrt (2-sqrt (4-x * x); s * = 2; n + = 1;} return 0 ;}
Method 3: Calculate the approximate value of π by using the formula.

You can use the following formula to conveniently calculate the approximate value of π. π = 2 + 2/3 + 2/3x2/5 + 2/3x2/5x3/7 + 2/3x2/5x3/7x4/9 + percentile

# Include <stdio. h> int main (void) {double temp = 2, PI = 2; int a = 1, B = 3; // a is a molecule, B is the denominator while (temp> 1e-20) {temp * = (double) a/B; PI + = temp; a + = 1; B + = 2 ;} printf ("PI = %. 12f \ n ", PI); return 0 ;}
Note:The program needs to end the loop according to the precision requirements.

Method 4: Calculate the π of any number of digits.

The π value calculated from methods 1 to 3 is limited by the representation range of variables in computer hardware due to the use of single variables to save the results. Therefore, A maximum of 10 digits after the decimal point of π can be calculated. To improve accuracy, you can define an array to save infinite circular decimals bit by bit. The basic idea is to use the formula 3 of method 3.

# Include <stdio. h> # include <stdlib. h> int main (void) {char * temp, * pi; int decLen; // number of decimal places int a = 1, B = 3; // the numerator denominator int result, I, carry; int count = 0, flag = 1; // calculate the number of times printf ("Enter π decimal places:"); scanf ("% d", & decLen ); decLen + = 2; if (! (Temp = (char *) malloc (sizeof (char) * decLen) {return 0;} if (! (Pi = (char *) malloc (sizeof (char) * decLen) {return 0 ;}for (I = 0; I <decLen; I ++) {* (pi + I) = 0; * (temp + I) = 0;} pi [1] = 2; // The initialization value temp [1] = 2; while (flag & ++ count <1e6) {carry = 0; for (I = decLen-1; I> = 0; I --) {result = temp [I] * a + carry; temp [I] = result % 10; carry = result/10;} carry = 0; for (I = 0; I <decLen; I ++) {result = temp [I] + carry * 10; temp [I] = result/B; carry = result % B;} flag = 0; for (I = decLen-1; I> 0; I --) {result = pi [I] + temp [I]; pi [I] = result % 10; pi [I-1] + = result/10; flag | = temp [I];} a + = 1; B + = 2;} printf ("calculated: % d times \ n ", count); printf (" PI = % d. ", pi [1]); // output single digit and decimal point for (I = 2; I <decLen; I ++) {if (I> 2) & (I-2) % 10 = 0) {printf ("");} if (I> 2) & (I-2) % 50 = 0) {printf ("\ n");} printf ("% d", pi [I]);} printf ("\ n "); return 0 ;}

Note:The condition for terminating the loop is that all values in the array temp are 0, that is, there is no remainder to be processed. If the element in the array is not 0, you must set another condition to terminate the loop. For example, specify the number of cycles.


What are the calculation methods for the circumference rate π? What are their respective advantages and disadvantages?

The ancients calculated the circumference rate, generally using the circular cut method. That is, the inner side of the circle or the inner side of the tangent polygon approach the circumference of the circle. Archimedes uses a 96-side shape to get the accuracy of the three digits after the decimal point, Liu Hui uses a 3072-side shape to get the accuracy of five digits, and rudde uses a 262-side shape to get the accuracy of 35 digits. This Ry-based algorithm is computation intensive, slow, and thankless. With the development of mathematics, mathematicians intentionally or unintentionally discovered many formulas for calculating the circumference rate during their mathematical studies. The following describes some classic common formulas. In addition to these classical formulas, there are many other formulas and the formulas derived from these classical formulas, which are not listed in detail.
1. Ma Qing Formula
π = 16arctan1/5-4arctan1/239
This formula was discovered by John Ma Qing, a British astronomy professor in 1706. He used this formula to calculate the circumference rate of 100 bits. Ma Qing's formula calculates a decimal precision of 1.4 bits for each item. Because the multiplier and divisor are not greater than long integers in the calculation process, it can be easily programmed on the computer.
There are also many arc tangent formulas similar to the Ma Qing formula. Among all these formulas, Ma Qing seems to be the fastest. Even so, if you want to calculate more digits, such as tens of millions, then the Ma Qing formula cannot help.
2. ramanokin Formula
In 1914, the Indian mathematician ramanokin published a series of 14 formulas for calculating the circumference rate in his paper. This formula can obtain an 8-digit decimal precision for each calculation. In 1985, Gosper used this formula to calculate the circumference rate of 17,500,000 bits.
In December 1989, the brothers David chunnovski and gregaoli chunovovski improved the lamanoukin formula. This formula is called the chilenovski formula. Each calculation item can obtain a 15-digit decimal precision. In 4,044,000,000, The chilenovski brothers used this formula to calculate the number of digits. Another more convenient form for computer programming is:
3. AGM (Arithmetic-Geometric Mean) Algorithm
Gaussian-lepeat formula:

What is the calculation method of the circumference rate? How many computing methods are there?

Circumference π d area π r square
3.14

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.