Last active
August 9, 2016 06:47
-
-
Save drbr/00c2994df9c357bb5ae3163a7ca26554 to your computer and use it in GitHub Desktop.
Python script that computes successive iterations of an infinite product that converges to π
This file contains 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
# Computes the value of pi by iteratively evaluating the infinite product: | |
# 4 * (8/9) * (24/25) * (48/49) * (80/81) * (120/121) * (168/169) * ... | |
# Each line of output shows the last denominator considered so far, and | |
# the value of the finite product at that point. | |
# | |
# As featured by Matt Parker: https://www.youtube.com/watch?v=8pj8_zjelDo | |
from decimal import * | |
def converge(): | |
area = Decimal(4) | |
fraction = Decimal(3) | |
while True: | |
denominator = fraction * fraction | |
numerator = denominator - 1 | |
area = (area / denominator) * numerator | |
yield (fraction, area) | |
fraction = fraction + 2 | |
for (iteration, area_guess) in converge(): | |
iteration_text = str(iteration) + ': ' + str(area_guess) | |
print iteration_text |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment