This Pi Day, Calculate the Value of Pi for Yourself | WIRED
Calculating area of half-cicrle of radius r=1 as sum of vertical slices
The surface area of circle is r**2 * pi = pi for r==1
Area of a slice is y * dx
x**2 + y**2 = r**2 = 1 => y = sqrt(1-x**2)
def calcPiSlices(n): dx = 2. / n # step size area = 0 # sum of all areas of half-circle for x in numpy.arange(-1, 1, dx): area = area + math.sqrt(1-x**2) * dx # area of a vertical slice return 2*area # *2 to get area of whole circle = pi
Here is the calculation in "real" Jupyter Notebook on
Pi - Wikipedia | 5 Ways to Calculate Pi - wikiHow
faster: Nilakantha series method
π = 3 + 4/(2*3*4) - 4/(4*5*6) + 4/(6*7*8) - 4/(8*9*10) + 4/(10*11*12) - 4/(12*13*14) ...
def calcPiNilakantha(n): pi = 3.0 for x in range(2, n, 4): pi += (4 / (x*(x+1)*(x+2))) - (4 / ((x+2)*(x+3)*(x+4))) return pi
No comments:
Post a Comment