# Collatz Conjecture

The Collatz conjecture states that every positive integer $k>0$ will — if you iteratively set $k$ to $f(k)$ — result in $1$ (function shown beneath).
The graph beneath shows the path length of numbers from $1$ to $10\,000$. In this range $6170$ is the number with the most steps, $261$. $f(k)={\begin{cases}\frac{k}{2}&{\text{if }}k \mod 2 = 0\\3 \cdot k+1&{\text{if }}k \mod 2 = 1\end{cases}}$ # Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 2nd of September, 2016

# import
import pygame, math, os

# steps to 1
def pathlen(k):
l = 0
while k != 1:
k = [k/2, 3*k+1][k%2]
l += 1

return l

# create data
data = []
maxx = 10**4
for _ in range(1, maxx+1):
data.append(pathlen(_))
maxy = max(data)

# graph size, surface and scale factor
width, height = 1080, 720
graph = pygame.Surface([width, height])
scalex, scaley = float(width)/maxx, float(height)/maxy

# render data
for _ in range(0, len(data)):
pygame.draw.circle(graph, [255, 0, 0], [int(_*scalex), height-int(data[_]*scaley)], 4)

# save graph
pygame.image.save(graph, os.getcwd() + "/collatz.png")

# get the largest step count with steps
#print data.index(maxy), "->", pathlen(data.index(maxy)+1)

This site uses Akismet to reduce spam. Learn how your comment data is processed.