Collatz Sequence Graphs

2025-11-04 Matplotlib
This is a graph of the Collatz sequence. Also known as the 3n+1 problem. It was plotted with numbers from 1 to 1000.

The x-axis represents the number of calculations, and the y-axis represents the calculation process.

Graphs drawn by turning to a specific angle if odd, and to another specific angle if even.

The x-axis represents natural numbers, and the y-axis represents the number of calculations until the value becomes 1.

The two graphs above chose the starting dir randomly.

The x and y axes represent angles based on odd and even numbers, and the z axis represents sqrt(value).

💐 graph generation code

!pip install datasets matplotlib
from datasets import load_dataset

ds = load_dataset("taery/Collatz_conjecture_100m", data_files="collatz_10k_sample.txt")
data=list(ds['train']['text'])
sequences=list(map(lambda line : line.split(","),data))
sequences=list(map(lambda line : list(map(lambda x : int(x), line)),sequences))
sequences=sequences[:1000] #10k is take long time..
from math import sin,cos,pi
def nums_to_point(num):
    x_points=[0]
    y_points=[0] 
    dir=0
    index=1
    for n in num[1:]:
        if n%2==0: 
            dir+=pi/6
            x_points.append(x_points[index-1]+sin(dir))
            y_points.append(y_points[index-1]+cos(dir))
        else:
            dir-=pi/6
            x_points.append(x_points[index-1]+sin(dir))
            y_points.append(y_points[index-1]+cos(dir))
        index+=1
    return x_points,y_points
    points=[]
for line in col_nums[:1000]:
    points.append(nums_to_point(line))
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
plt.clf() 
plt.figure(figsize=(10, 10))
for idx,i in enumerate(points):
    color=cm.cividis(np.random.rand())
    #color=np.random.rand(4,)
    plt.plot(i[0],i[1],color=color,linewidth=0.5,alpha=0.7)
    plt.axis("equal")
name="my_graph"
plt.savefig("graph/"+name+".svg")
plt.show()

below is 3d graph code.

from math import sin,cos,pi
def nums_to_point(num):
    x_points=[0]
    y_points=[0]
    z_points=[0]
    dir=pi
    for index,n in enumerate(num,1):
        if n%2==0:
            dir+=pi/18
            x_points.append(x_points[index-1]+sin(dir))
            y_points.append(y_points[index-1]+cos(dir))
        else:
            dir
            x_points.append(x_points[index-1]+sin(dir))
            y_points.append(y_points[index-1]+cos(dir))
        z_points.append(z_points[index-1]+0.8)
    return x_points,y_points,z_points
points=[]
for line in col_nums:
    points.append(nums_to_point(line))
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
plt.clf()
fig = plt.figure(figsize=(7,7))
ax = fig.add_subplot(111, projection="3d")
all_x = [x for point in points for x in point[0]]
all_y = [y for point in points for y in point[1]]
all_z = [z for point in points for z in point[2]]
xlim = (min(all_x), max(all_x))
ylim = (min(all_y), max(all_y))
zlim = (min(all_z), max(all_z))
ax.set_xlim(xlim) 
ax.set_ylim(ylim)
ax.set_zlim(zlim) 
xlim = ax.get_xlim3d()
ylim = ax.get_ylim3d()
zlim = ax.get_zlim3d()
ax.set_xlabel("X Axis")
ax.set_ylabel("Y Axis")
ax.set_zlabel("count")
ax.set_box_aspect([xlim[1]-xlim[0], ylim[1]-ylim[0], zlim[1]-zlim[0]])
for num in points:
    #color=cm.cividis(np.random.rand())
    color=np.random.rand(4,)
    ax.plot(num[0],num[1],num[2],color=color,alpha=0.7,lw=0.5)
plt.savefig("graph/my3d.svg")
plt.show()

Adjusted the size of dir and the sign of the expression.