Collatz stepsΒΆ

How long is the Collatz sequence for each n?

collatz steps

Out:

<matplotlib.collections.PathCollection object at 0x7fa33eca4850>

import matplotlib.pyplot as plt


def collatz(n):
    if n == 1:
        return 0
    return 1 + collatz(n//2 if n % 2 == 0 else 3*n+1)


n_range = list(range(1, 10_000))
n_counts = [collatz(n) for n in n_range]

plt.scatter(n_range, n_counts, s=2)

Total running time of the script: ( 0 minutes 0.469 seconds)

Gallery generated by Sphinx-Gallery