Skip to content

Instantly share code, notes, and snippets.

@oliverjam
Created June 15, 2017 15:07
Show Gist options
  • Save oliverjam/3d0e0bc480e342c06ace0aa0ae18319a to your computer and use it in GitHub Desktop.
Save oliverjam/3d0e0bc480e342c06ace0aa0ae18319a to your computer and use it in GitHub Desktop.
Python Pi calculator created by oliverjam - https://repl.it/InIh/0
from decimal import getcontext, Decimal
getcontext().prec = 20
def calculatePi():
pi = Decimal(3)
i = Decimal(2)
while i < 20000000:
divisor = Decimal(4) / Decimal(i * (i + 1) * (i + 2))
if i % 4 == 0:
pi -= divisor
else:
pi += divisor
i += 2
return pi
print(calculatePi())
print(Decimal(3.1415926535897932384626433832795028841971693993751058209))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment