Skip to content

Instantly share code, notes, and snippets.

@IlievskiV
Created September 6, 2020 09:59
Show Gist options
  • Save IlievskiV/8e6989c69047b1274a4e0f9ec3314469 to your computer and use it in GitHub Desktop.
Save IlievskiV/8e6989c69047b1274a4e0f9ec3314469 to your computer and use it in GitHub Desktop.
def mandelbrot(x, y, threshold):
"""Calculates whether the number c = x + i*y belongs to the
Mandelbrot set. In order to belong, the sequence z[i + 1] = z[i]**2 + c
must not diverge after 'threshold' number of steps. The sequence diverges
if the absolute value of z[i+1] is greater than 4.
:param float x: the x component of the initial complex number
:param float y: the y component of the initial complex number
:param int threshold: the number of iterations to considered it converged
"""
# initial conditions
c = complex(x, y)
z = complex(0, 0)
for i in range(threshold):
z = z**2 + c
if abs(z) > 4.: # it diverged
return i
return threshold - 1 # it didn't diverge
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment