Writing a Python Function to Compute Pi

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)
0
source share
1 answer

, Pi, Madhava.

import math

def myPi(iters):
  sign = 1
  x = 1
  y = 0
  series = 0 
  for i in range (iters):
    series = series + (sign/(x * 3**y))
    x = x + 2
    y = y + 1
    sign = sign * -1
  myPi = math.sqrt(12) * series

  return myPi

print(myPi(1000))
+3

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


All Articles