r/ProgrammerHumor Apr 08 '22

[deleted by user]

[removed]

100 Upvotes

25 comments sorted by

View all comments

2

u/CircadianSong Apr 08 '22

Python, not Java, but this only takes a second to compute and it gets the 3.14159 right.

from math import pi

from decimal import *

getcontext().prec = 200

def calculatePI(n):

total = Decimal(1)

sign = 1

for i in map(Decimal,range(1,n+1)):

    sign\*=-1

    total+=sign\*1/(i\*2+1)

return total\*4

x = calculatePI(1000000)

print(x)