Created
June 15, 2017 15:07
-
-
Save oliverjam/3d0e0bc480e342c06ace0aa0ae18319a to your computer and use it in GitHub Desktop.
Python Pi calculator created by oliverjam - https://repl.it/InIh/0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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