Python: evaluate Pi with trigger functions as efficiently as possible

I have a task where I need to approximately calculate Pi. Here is my strategy: I use the unit circle, the bisector of the angle of the iso-target triangle, and the definition of sin. I drew a diagram:

enter image description here

For example, if I want to use a hexagon (6 points / 6 sides), I just need to calculate a: ( 0.5*sin(2*pi/2*x) and multiply it by ( 2*x). Finally, since Pi = Circumference/Diameter, then my approximation is Pi = polygonal 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 is effective, as it is, no? Thank you for your time!

EDIT: to avoid roundness, I edited it using the Archimedes algorithm using only the Pythagorean tetra:

enter image description here

the code:

from math import sqrt

def approxPi(x):                  #x: number of times you want to recursively apply Archmidedes' algorithm
    s = 1                         #Unit circle
    a = None; b = None;   
    for i in range(x):
        a = sqrt(1 - (s/2)**2)
        b = 1 - a
        print('The approximate value of Pi using a {:5g}-sided polygon is {:1.8f}'.format(6*2**(i),(s*6*2**(i))/2))
        s = sqrt(b**2 + (s/2)**2)
+4
3

print(4 * math.atan(1))

pi - (, @Jean-FranΓ§oisFabre, pi, , ), . ,

print(2 * math.acos(0))

print(2 * math.asin(1))
+4

, , :

from math import sqrt

def psum(n):
    return sum(1/k**2 for k in range(1,n+1))

def approxPi(n):
    s = psum(n)
    return sqrt(6*s)

,

>>> approxPi(100000)
3.141583104326456

, . , . , pi , pi , .

: @Simon - decimal, ( ):

import decimal
from decimal import Decimal as d

def approxPi(n):
    eps = 1/d(10**n)
    decimal.getcontext().prec = 3*n #probably overkill, but need room for products
    a = d(1)
    b = 1/d(2).sqrt()
    t = 1/d(4)
    p = d(1)
    dif = a-b
    if dif < 0: dif = -dif
    i = 1
    while dif >= eps:
        a1 = (a+b)/2
        b1 = a*b
        b1 = b1.sqrt()
        t1 = t - p*(a - a1)**2
        p1 = 2*p
        a,b,t,p = a1,b1,t1,p1
        dif = a1-b1
        if dif < 0: dif = -dif
    pi = (a + b)**2/(4*t)
    return str(pi)[:n+2]

,

>>> approxPi(1000)
'3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989'

this.

. 10 000. , , 1 000 000 Python.

+2

:

from math import radians, sin


def computePi(n):
    p = n * (sin(radians((360/(2*n)))))
    print(p)
computePi(1000)

: https://math.stackexchange.com/questions/588141/how-is-the-value-of-pi-pi-actually-calculated

+1
source

Source: https://habr.com/ru/post/1662089/


All Articles