day1/exercise/find_pow_2.py
changeset 64 333092b68926
equal deleted inserted replaced
63:f5eac04a00fe 64:333092b68926
       
     1 def is_pow_2(n):
       
     2     bin_count = 0
       
     3     while n > 0:
       
     4         if n % 2 == 1:
       
     5             bin_count += 1
       
     6         if bin_count > 1:
       
     7             return False
       
     8         n /= 2
       
     9 
       
    10     return bin_count == 1
       
    11 
       
    12 def collatz_pow_2(n):
       
    13     if n == 1: return 4
       
    14     if n == 2: return 4
       
    15     collatz_pow_2 = []
       
    16     while n > 2:
       
    17         print n, 
       
    18         if is_pow_2(n):
       
    19             collatz_pow_2.append(n)
       
    20 
       
    21         if n % 2:
       
    22             n = n * 3 - 1
       
    23         else:
       
    24             n /= 2 
       
    25 
       
    26     return max(collatz_pow_2)
       
    27 
       
    28 import sys
       
    29 collatz_pow_2(int(sys.argv[1]))