Welcome, guest | Sign In | My Account | Store | Cart

Miraculously, you can estimate π just by counting the number of iterations for a point near the neck to escape. The method has a rigorous error bound.

Python, 13 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
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)