Created
November 8, 2016 21:31
-
-
Save hunj/043b551b7df3abd3275c4e3458cae9fb to your computer and use it in GitHub Desktop.
Compute π using Nilakantha's series
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 * | |
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