def sq(x): out = 0 while x != 1: out += 1 if x % 2 == 1: x = x * 3 + 1 else: x /= 2 return out maxx = [0, 0] limit = 1000000 for i in xrange(1, limit + 1): if i % 100000 == 0: print i tmp = sq(i) if tmp > maxx[1]: maxx[1] = tmp maxx[0] = i print maxx ''' limit = 1000000 num = {1:1} maxx = [0, 0] tmp = [] for i in xrange(2, limit + 1): x = i while 1: if num.has_key(x): tmp.reverse() bak = x while len(tmp) > 0: num.update({tmp[0]: num.get(bak) + 1}) bak = tmp[0] tmp.pop(0) break else: tmp.append(x) if x % 2 == 0: x /= 2 else: x = 3 * x + 1 if num.get(i) > maxx[1]: maxx = [i, num.get(i)] print maxx '''