Recursion을 써서 만들었습니다.

--- yong27 님 감사합니다.

   1 def ThreePlusOne(n, count=0):
   2     count += 1
   3     if n==1:
   4         return count
   5     elif n%2==0:
   6         return ThreePlusOne(n/2, count) # return을 빼먹으면 None이  리턴
   7     else:
   8         return ThreePlusOne(n*3+1, count) #return을 빼먹으면 None이 리턴
   9         
  10 def main():
  11     i, j = raw_input().split()
  12 
  13     b = 0
  14     for a in range(int(i), int(j)+1):
  15         temp = ThreePlusOne(a)
  16         if b < temp: b = temp
  17     print i, j, b
  18 
  19 if __name__ == '__main__':
  20     #import psyco
  21     #psyco.bind(main)
  22     main()

3N+1Problem 참조

AlgorithmQuiz/3Plus1/mgenome (last edited 2013-08-09 16:13:17 by 61)

web biohackers.net