Skip to content

Instantly share code, notes, and snippets.

@RobertTalbert
Created August 28, 2023 13:54
Show Gist options
  • Save RobertTalbert/ff818256a0bea8d77e2f9c038370360e to your computer and use it in GitHub Desktop.
Save RobertTalbert/ff818256a0bea8d77e2f9c038370360e to your computer and use it in GitHub Desktop.
Python code for exploring the Collatz conjecture
# Individual Collatz computation
def f(n):
if n % 2 == 0:
return n//2
else:
return 3*n+1
# Create sequence of integers from the Collatz function
# This assumes the Collatz conjecture is true LOL
def collatz(n):
result = [ ]
while n != 1:
result.append(n)
n = f(n)
result.append(1)
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment