newbie here:
Just by learning Python, and I have me. He came up with a function for manually calculating Pi, Madhava way. - Also known as exercise number 16 from here: http://interactivepython.org/courselib/static/thinkcspy/Functions/thinkcspyExercises.html
Can someone take a look at my inconvenient and overly complex code and tell me if I missed something? Many thanks. (First, take a look at the equation on the wiki page, otherwise my code will not make sense - well, it may still not be.)
import math
def denom_exp(iters):
for i in range(0, iters):
exp = 3^iters
return exp
def base_denom(iters):
for i in range(0, iters):
denom = 1 + 2*iters
return denom
def myPi(iters):
sign = 1
pi = 0
for i in range(0, iters):
pi = pi + sign*(1/((base_denom(iters))*denom_exp(iters)))
sign = -1 * sign
pi = (math.sqrt(12))*pi
return pi
thisisit = myPi(10000)
print(thisisit)
source
share