Python uses the triangle iteration method to calculate the circumference PI, pythonpi
This example describes how python uses triangle iteration to calculate the circumference rate PI. Share it with you for your reference. The details are as follows:
Method 1:
Copy codeThe Code is as follows: # Calculating PI using trigonometric iterations
# FB36-20130825
Import math
X = 1.0
Y = 1.0
* Z = 1.0
W = 1.0
V = 1.0
U= 1.0
For I in range (30 ):
X = math. sin (x) + x
Y = math. cos (y) + y
Z = math. cos (z) + math. sin (z) + z
W = math. cos (w)-math. sin (w) + w
V = math. cos (v) * math. sin (v) + v
U = math. cos (u)/math. sin (u) + u
Print I
Print x, y * 2.0, z * 4.0/3.0, w * 4.0, v * 2.0, u * 2.0
Print
Method 2:
Copy codeThe Code is as follows: # Calculating PI using trigonometric iterations
# FB36-20130901
Import math
Def sin2 (x ):
Return (math. e ** complex (0.0, x)-math. e ** complex (0.0,-x)/2.0). imag
Def cos2 (x ):
Return (math. e ** complex (0.0, x) + math. e ** complex (0.0,-x)/2.0). real
X = 1.0
Y = 1.0
X2 = 1.0
Y2 = 1.0
For I in range (5 ):
X = math. sin (x) + x
Y = math. cos (y) + y
X2 = sin2 (x2) + x2
Y2 = cos2 (y2) + y2
Print I, x, x2, y x 2.0, y2 x 2.0
I hope this article will help you with Python programming.