Friday, July 04, 2025

THE ENIGMATIC BEAUTY OF THE COLLATZ FRACTAL: WHERE ORDER MEETS CHAOS

Introduction


The Collatz conjecture - a simple puzzle about integers - has puzzled mathematicians since 1937. While unsolved in its original form, its extension to complex numbers reveals beautiful fractal patterns that show the balance between order and chaos.


The Collatz Conjecture: Basic Rules


For positive integers:

- If n is even: n becomes n/2

- If n is odd: n becomes 3n + 1

All sequences seem to end in the cycle 4 → 2 → 1.


Complex Version


The complex Collatz function is:

f(z) = z + 1/4 - [(2z + 1)/4] * cos(pi*z)


Key points:

- cos(pi*z) detects even/odd behavior

- Points "escape" when their magnitude exceeds R (usually R=100)


Fractal Generation


The escape-time method works by:

1. Taking a grid of complex points z = x + yi

2. Applying the function repeatedly until escape

3. Coloring points based on how long they take to escape


Fractal Features


Central Area:

- Dark region near the origin (0 + 0i)

- Matches the integer pattern 1 → 4 → 2 → 1


Boundary Structure:

- Infinitely detailed edges

- Self-similar patterns at different scales


Mathematical Details


The system's sensitivity is measured by:

Λ = limit (1/n) * sum(ln|f'(zi)|) as n→∞


Where the derivative is:

f'(z) = 1 - (1/2)cos(pi*z) + [pi*(2z+1)/4]sin(pi*z)


Computation


Python implementation snippet:

def collatz_iter(z):

    return z + 0.25 - (2*z + 1)/4 * cmath.cos(cmath.pi * z)


Visualization of Collatz Fractals


The following figure was created by the program below. It shows the beauty of the Collatz function.




Further Reading


- Lagarias (2010) "The Ultimate Challenge: The 3x+1 Problem"

- Fractal dimension methods:

  Box-counting: D0 = -lim logN(ε)/logε as ε→0

  Correlation: D2 = lim logC(r)/logr as r→0


Code Example


import numpy as np

import matplotlib.pyplot as plt

import cmath

import time


def collatz_fractal(real_min, real_max, imag_min, imag_max, width=800, height=800, 

                    max_iter=100, escape_radius=100.0):

    """

    Generate a fractal visualization of the Collatz function in the complex plane.

    

    Parameters:

        real_min, real_max (float): Range of real axis.

        imag_min, imag_max (float): Range of imaginary axis.

        width, height (int): Pixel dimensions of the image.

        max_iter (int): Maximum iterations per point.

        escape_radius (float): Escape radius to stop iteration.

        

    Returns:

        numpy.ndarray: 2D array of escape times.

    """

    # Create grid of complex numbers

    real_vals = np.linspace(real_min, real_max, width)

    imag_vals = np.linspace(imag_min, imag_max, height)

    image = np.zeros((height, width), dtype=np.float32)

    

    start_time = time.time()

    

    # Iterate over each pixel

    for i in range(height):

        for j in range(width):

            z = complex(real_vals[j], imag_vals[i])

            # Track escape time

            for k in range(max_iter):

                if abs(z) > escape_radius:

                    image[i, j] = k

                    break

                # Complex Collatz function

                try:

                    z = z + 0.25 - (2*z + 1)/4 * cmath.cos(cmath.pi * z)

                except (OverflowError, ValueError):

                    image[i, j] = max_iter - 1

                    break

            else:

                image[i, j] = max_iter - 1

                

    print(f"Computed in {time.time() - start_time:.2f} seconds")

    return image


def plot_fractal(image, real_min, real_max, imag_min, imag_max):

    """Plot the fractal image with colorbar and labels."""

    plt.figure(figsize=(10, 8))

    plt.imshow(image, 

               extent=[real_min, real_max, imag_min, imag_max], 

               origin='lower', 

               cmap='inferno', 

               interpolation='bilinear')

    plt.colorbar(label='Iterations to Escape')

    plt.title('Collatz Function Fractal')

    plt.xlabel('Real Axis')

    plt.ylabel('Imaginary Axis')

    plt.show()


# Generate and plot the fractal

if __name__ == "__main__":

    # Adjust these bounds to explore different regions

    real_min, real_max = -5, 5

    imag_min, imag_max = -5, 5

    

    fractal_image = collatz_fractal(real_min, real_max, imag_min, imag_max,

                                    width=800, height=800, max_iter=100)

    plot_fractal(fractal_image, real_min, real_max, imag_min, imag_max)

No comments:

Post a Comment