Skip to content

Instantly share code, notes, and snippets.

@hunj
Created November 8, 2016 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hunj/043b551b7df3abd3275c4e3458cae9fb to your computer and use it in GitHub Desktop.
Save hunj/043b551b7df3abd3275c4e3458cae9fb to your computer and use it in GitHub Desktop.
Compute π using Nilakantha's series
from decimal import *
def compute_pi():
pi = Decimal(3.0)
curr_multiplier = Decimal(2)
iteration = Decimal(0)
load = Decimal(1)
getcontext().prec = 6
while 1:
load = Decimal(4.0) / (curr_multiplier * (curr_multiplier+1) * (curr_multiplier+2))
if iteration % 2 == 0:
pi += load
else:
pi -= load
print pi, curr_multiplier, iteration
curr_multiplier += 2
iteration += 1
getcontext().prec += 1
compute_pi()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment