This article mainly introduces Python's method of calculating the value of pi to any position, briefly analyzes the calculation principle of pi, and analyzes the relevant operating skills of Python's calculation of pi in the form of examples. Friends in need can refer to the following
The example in this article describes the method of calculating the value of pi to any digit in Python. Share it with everyone for your reference, the details are as follows:
1. Requirements analysis
Enter the number of digits you want to calculate to the decimal point Number, calculate the value of pi.
2. Algorithm: Ma Qing formula
π/4=4arctan1/5-arctan1/239
This formula was discovered by the British astronomy professor John Mathing in 1706. He used this formula to calculate pi to 100 digits. Ma Qing's formula can obtain 1.4 decimal digits of precision for each calculated item. Because the multiplicand and dividend during its calculation are neither larger than a long integer, it can be easily programmed on a computer.
3. Write a program in python language to find the pi to any position as follows:
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 |
|
Run result:
Please enter the number of digits you want to calculate to the decimal point n:20
3.14159265358979323846
Total time spent: 9.77699995041s
Please enter the number of digits you want to calculate to the decimal point n:50
3.14159265358979323846264338327950288419716939937510
Total time spent: 2.30099987984s
The running screenshot is as follows:
Related recommendations:
Python implements crawler setting proxy IP Share with the method of pretending to be a browser
The above is the detailed content of Python example of how to calculate the value of pi to any digit. For more information, please follow other related articles on the PHP Chinese website!