In [11]:
def will_it_stop(n):
    while n > 1:
        print n
        if n%2 is 0:
            n = n/2
        else:
            n = 3*n + 3

In [3]:
def is_power_of_two(n):
    return n&(n-1)

In [4]:
print is_power_of_two(2)
print is_power_of_two(4)
print is_power_of_two(8)
print is_power_of_two(16)
print is_power_of_two(32)
print is_power_of_two(64)
print is_power_of_two(128)
print is_power_of_two(3)
print is_power_of_two(6)
print is_power_of_two(7)
print is_power_of_two(14)
print is_power_of_two(9)
print is_power_of_two(18)


0
0
0
0
0
0
0
2
4
6
12
8
16

In [7]:
def will_it_ever_stop(n):
    print "TAK" if (n&(n-1)) == 0  else "NIE"

In [8]:
print will_it_ever_stop(100000000000000)
print will_it_ever_stop(1)
print will_it_ever_stop(2)
print will_it_ever_stop(4)
print will_it_ever_stop(8)
print will_it_ever_stop(16)
print will_it_ever_stop(32)
print will_it_ever_stop(64)
print will_it_ever_stop(128)
print will_it_ever_stop(3)
print will_it_ever_stop(6)
print will_it_ever_stop(7)
print will_it_ever_stop(14)
print will_it_ever_stop(9)
print will_it_ever_stop(18)


NIE
None
TAK
None
TAK
None
TAK
None
TAK
None
TAK
None
TAK
None
TAK
None
TAK
None
NIE
None
NIE
None
NIE
None
NIE
None
NIE
None
NIE
None

In [ ]: