Skip to content

Instantly share code, notes, and snippets.

@jesuscast
Last active December 31, 2015 08:39
Show Gist options
  • Save jesuscast/7961783 to your computer and use it in GitHub Desktop.
Save jesuscast/7961783 to your computer and use it in GitHub Desktop.
Simple algorithm that calculates an approximation of pi. It is based on the idea that the area of a polygon with many sides circumscribed inside a unit circle approaches pi. In the example I test the algorithm with a polygon of 500 sides and the result of executing the code in my machine is 3.141592653589795Important Notes: The maximum length it…
import math
def calcSide(a):
side = math.sqrt(2)
H = math.sqrt(1/2)
for x in range(1,a):
b = side/2
x = 1 - H
side = math.sqrt(b**2+x**2)
H = math.sqrt(1-(side/2)**2)
r = {"side":side,"H":H}
return r
def calcPi(a):
r = calcSide(a)
side = r["side"]
H = r["H"]
pi = (side*H*(2**(a+1)))/2
return pi
calcPi(500)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment