2
votes

I need to make a function that tests if a complex number, c is in the Mandelbrot set which is defined as zn+1 = zn2 + c. The values of n are in subscript if that was confusing. The function accepts variables c (some complex number of the form 0 + 0j) and n (number of iterations). To see if c is in the set, I need to test z = z**2 + c > 2. If > 2 I need to return false. Now I know that with c = 0 + 0j and n = 25 I should get True. But I can only get true with very small values of n. What do I need to do differently.

def inMSet(c,n):
    for x in range(0, n):
        z = n**2 + c
        if abs(z) > 2:
            return False
        else:
            return True 
1
What programming language ??? Please tag responsibly. - Paul R
Maybe I'm missing something here, but the for loop doesn't seem to be doing anything. Should you have z = x**2 + c instead? - Geoff Montee
So what exactly do I need to do with n in my code? - Josh

1 Answers

2
votes

The definition has an iterative formula, starting with z=0.

def inMSet(c,n):
    z = 0
    for x in range(0, n):
        z = z**2 + c
        if abs(z) > 2:
            return False
    return True 

>>> inMSet(0+0j,25)
True