This is a simple program to test if integers will follow an interesting, improvable statement that if disproven, calls for a bravo.
But, ignoring the pretentious poetry, this code allows the user to input a number, which, if it is a finite, positive integer, will be calculated according to the Collatz Conjecture as stated/commented out below. Any optimizations? Obvious mistakes? Formatting issues?
Maybe let me know how to inform the user that the function-ception went a few levels too deep?
# The Collatz conjecture states that # when you take a finite, positive integer, and # if it's even, divide by 2, or # if it's odd, multiply by 3 and add 1, # and repeat this process enough times, # you will eventually end up with one. # Here's a module to demonstrate it. def collatz(x): if abs(int(x)) == x: if x == 1: print ("The Collatz Conjecture works with this number!") print ("It took %s repetitions to reach 1.") % (collatz.counter) elif x % 2 == 0: y = x / 2 print ("%s") % (y) collatz.counter += 1 collatz(y) elif x % 2 == 1: y = x * 3 + 1 print ("%s") % (y) collatz.counter += 1 collatz(y) else: print ("Oh, my god!") print ("This number does NOT work with the Collatz Conjecture!") print ("You've disproven it!") print ("...Or maybe this program is broken. Try viewing the source?") else: print ("That isn't a finite positive integer...") collatz.counter = 0 #Here's our attempt: collatz(int(raw_input("Enter a finite positive integer")))