Skip to content Skip to sidebar Skip to footer

Writing A Python Function To Calculate Pi

newbie here: Just learning Python and this one has me pooped. It's coming up with a function for manually computing Pi, the Madhava way. - Also known as exercise #16 from here: htt

Solution 1:

Try this code, manually computing Pi, the Madhava way.

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))

Post a Comment for "Writing A Python Function To Calculate Pi"