Created
May 28, 2024 07:27
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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