Exploring Fractals: Beyond the Mandelbrot Set with SQLite


def mandelbrot(c, max_iter):
  """
  Calculate the number of iterations required to determine if a complex number 'c' belongs to the Mandelbrot set.

  Args:
      c: A complex number.
      max_iter: The maximum number of iterations allowed to classify 'c' within the Mandelbrot set.

  Returns:
      The number of iterations needed to classify 'c' within the Mandelbrot set, 
      or 'max_iter' if the limit is not reached.
  """
  z = c
  for n in range(max_iter):
    if abs(z) > 2.0:
      return n
    z = z * z + c
  return max_iter

# Example usage
c = complex(-0.76, 0.07)
max_iter = 255
result = mandelbrot(c, max_iter)
print(f"It took {result} iterations to classify {c} in the Mandelbrot set.")


import sqlite3

def mandelbrot(c, max_iter):
  """
  Calculate the number of iterations required to determine if a complex number 'c' belongs to the Mandelbrot set.

  Args:
      c: A complex number.
      max_iter: The maximum number of iterations allowed to classify 'c' within the Mandelbrot set.

  Returns:
      The number of iterations needed to classify 'c' within the Mandelbrot set, 
      or 'max_iter' if the limit is not reached.
  """
  z = c
  for n in range(max_iter):
    if abs(z) > 2.0:
      return n
    z = z * z + c
  return max_iter

# Connect to the database
conn = sqlite3.connect("mandelbrot.db")

# Create a table to store results
cursor = conn.cursor()
cursor.execute("""CREATE TABLE IF NOT EXISTS mandelbrot_results (
                  c_real REAL,
                  c_imag REAL,
                  iterations INTEGER
                )""")

# Example calculations for a few complex numbers
complex_numbers = [
  complex(-0.76, 0.07),
  complex(-0.75, 0.07),
  complex(-0.74, 0.07),
]

for c in complex_numbers:
  max_iter = 255
  iterations = mandelbrot(c, max_iter)
  # Insert the results into the table
  cursor.execute("INSERT INTO mandelbrot_results (c_real, c_imag, iterations) VALUES (?, ?, ?)", (c.real, c.imag, iterations))

# Save the changes and close the connection
conn.commit()
conn.close()

print("Mandelbrot results saved to 'mandelbrot.db' database.")
  1. We import the sqlite3 library to interact with the SQLite database.
  2. The mandelbrot function remains the same as before.
  3. We connect to a database named mandelbrot.db using sqlite3.connect.
  4. We create a table named mandelbrot_results using a cursor object. The table has three columns:
    • c_real: Stores the real part of the complex number.
    • c_imag: Stores the imaginary part of the complex number.
    • iterations: Stores the number of iterations needed to classify the complex number within the Mandelbrot set.
  5. We define a list of complex numbers to calculate the Mandelbrot set for.
  6. We iterate through the list, calculate the iterations for each complex number using the mandelbrot function, and insert the results (real part, imaginary part, and iterations) into the mandelbrot_results table.
  7. Finally, we commit the changes to the database and close the connection.


  1. Julia Sets
    These are closely related to the Mandelbrot set, but instead of varying the constant c in the formula z = z^2 + c, you fix c and vary the initial value of z. Different complex values of c create unique and fascinating Julia sets.

  2. Burning Ship Fractal
    This fractal uses a slightly modified formula: z = abs(z^2 - c). It produces intricate flame-like shapes and exhibits interesting self-similarity.

  3. Mandelbrot variations
    You can experiment with variations of the Mandelbrot formula itself. For example, z = z^4 + c or z = z^3 + 2c^2 - 1 lead to different and beautiful fractal structures.

  4. Cellular Automata
    These are discrete dynamical systems where the state of each cell depends on its neighbors. Examples include Conway's Game of Life, which generates surprisingly complex patterns from simple rules.

  5. Koch Snowflake
    This fractal starts with a triangle and iteratively adds smaller triangles onto its sides. It demonstrates the concept of self-similarity and the creation of infinite detail within a finite space.