Johnathan - 8 days ago 5
Python Question

# Python: estimate Pi with trig functions as efficiently as possible

I have an assignment where I need to approximate Pi in a computationally efficient manner. Here is my strategy: I use a unit circle, the angle bisector of an isoceles triangle, and the definition of sin. I drew a diagram:

For example, if I want to use an hexagon (6 points/6 sides), I simply need to compute

`a`
:(
`0.5*sin(2*pi/2*x`
) and multiply it by (
`2*x`
). Finally, since
`Pi = Circumference/Diameter`
, then my approximation of Pi = polygon perimeter (since
`Diameter = 1`
).

Essentially:

``````from math import sin, pi
def computePi(x):    #x: number of points desired
p = x*sin(pi/x)
print(p)

computePi(10000)
3.141592601912665
``````

It works, and I think it's as efficient as it gets, no? Thank you for your time!

EDIT: to avoid circularity, I'll compute sin(30) using a Maclaurin series. Then it should work.

Even better is

``````print(4 * math.atan(1))
``````

This does not use pi in any obvious way in the calculation (though as @Jean-FrançoisFabre comments, pi is probably used in the function definition), and in addition to the trig function it has just one simple multiplication. Of course, there is also

``````print(2 * math.acos(0))
``````

and

``````print(2 * math.asin(1))
``````