Skip to content

Instantly share code, notes, and snippets.

@marsam
Created September 6, 2011 13:41
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 marsam/1197554 to your computer and use it in GitHub Desktop.
Save marsam/1197554 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Estimation of Pi using the Mandlebrot set
http://en.wikipedia.org/wiki/Mandelbrot_set
http://code.activestate.com/recipes/577584-amazing-estimation-of-pi-using-the-mandlebrot-set/
https://home.comcast.net/%7Edavejanelle/mandel.pdf
"""
def pi(err, abs=abs):
"""Approximation of pi +/- err using the Mandlebrot set"""
n = 0
z = c = complex(-0.75, err)
while abs(z) < 2.0:
n += 1
z = z * z + c
return n * err
if __name__ == '__main__':
for i in range(1, 8):
err_bound = 10 ** (-i)
print(pi(err_bound), 'within', err_bound)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment