Skip to content

Instantly share code, notes, and snippets.

@bkonkle
Created March 7, 2010 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bkonkle/324447 to your computer and use it in GitHub Desktop.
Save bkonkle/324447 to your computer and use it in GitHub Desktop.
XKCD inspired me to write a quick Collatz Conjecture script in Python
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