Google Code Prettify
# Computes the hailstone sequence of an input number # and prints how many iterations it takes to reach 1. # For more info see <a href="http://en.wikipedia.org/wiki/Collatz_conjecture">http://en.wikipedia.org/wiki/Collatz_conjecture</a> import sys def iterate_hailstone(n): if n % 2 == 0: return n / 2 else: return 3 * n + 1 if __name__ == '__main__': n = int(input('Introduce a natural number: ')) if (n < 1): sys.exit('You need to learn what a natural number is.') num_iterations = 0 while n > 1: print n n = iterate_hailstone(n) num_iterations = num_iterations + 1 print 'Converged in %d iterations' % num_iterations
Or more succinctly:
while(n > 1): n = (n%2) * (3*n+1) + (1 - n%2) * (n/2); n;
Installing the library was painless. Also, it seems very quick to load. The complete instructions are here.
PS. Does the program finish for all natural numbers?