Thursday, March 1, 2012

Hailstone sequences - Easy one

Problem
=================================================
Start with x,
    if x is even --> x becomes half
    if x is off --> x becomes triple plus one
Sequence is believed to end with '1' all the time (hailstone sequence)
=================================================

Solution
============
I decided to put my newly learnt skills of Python to use for this easy one. :)

Here it goes:

   def hailstone_seq(x):
       print x,
       while x > 1:
           if x % 2 == 0:
               x = x/2
           else:
               x = 3*x + 1
           print x,