Skip to content

Instantly share code, notes, and snippets.

@talhaahussain
Created May 28, 2024 07:27
Show Gist options
  • Save talhaahussain/9154afdf518b35e0a4c5485d10a6eaea to your computer and use it in GitHub Desktop.
Save talhaahussain/9154afdf518b35e0a4c5485d10a6eaea to your computer and use it in GitHub Desktop.
collatz.py -- A very simple demonstration of the Collatz conjecture, an unsolved problem in mathematics, in Python. Applies the rules of the conjecture in a repeated loop on some integer x, and outputs the number of steps taken to reach 1.
import math
x = 99 # Select a starting point here
i = 0
running = True
while running:
i += 1
if x % 2 == 0:
x = x / 2
print(int(x))
else:
x = 3*x + 1
print(int(x))
if x == 1:
print("Steps taken: " + str(i))
running = False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment