Created
March 7, 2010 16:24
-
-
Save bkonkle/324447 to your computer and use it in GitHub Desktop.
XKCD inspired me to write a quick Collatz Conjecture script in Python
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 random | |
def main(): | |
start = n = round(random.random() * 100000) | |
i = 0 | |
while not n == 1: | |
i += 1 | |
if n % 2: | |
# The remainder isn't zero, so this evaluates to True | |
old = n | |
n = n * 3 + 1 | |
print "Odd: %i * 3 + 1 = %i" % (old, n) | |
else: | |
# The remainder is zero, so this evaluates to False | |
old = n | |
n = n / 2 | |
print "Even: %i / 2 = %i" % (old, n) | |
print "The number %i was converged to 1 in %i steps." % (start, i) | |
if __name__ = 'main': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment