Search code examples
pythonpython-3.xcomplex-numbersmandelbrot

Calculate Mandelbrot set without complex objects


I am trying to calculate the Mandelbrot set in Python 3.6 and i don't want to use the complex objects to calculate it. Does anybody have a getIterationCount(x, y) function?

I tried to rewrite java code to python but it didn't work.

def getIterationCount(x, y):
    maxiter = 100
    z = complexe(x, y)
    c = z
    for n in range(0, maxiter):
        if abs(z) > 2:
            return n
        z = z*z + c
    return maxiter

Solution

  • I can write it for you if you mean to work only with real numbers:

    def getIterationCount(ca,cb):
        maxiter = 100
        za, zb = ca,cb
        for n in range(0, maxiter):
            if za**2+zb**2 > 4:
                return n
            za, zb = za*za - zb*zb + ca, 2*za*zb + cb
    
        return maxiter