fork download
  1. @memo={}
  2. def F x
  3. return @memo[x] if @memo[x]
  4. return @memo[x]=if x%2==1
  5. x*3+1
  6. else
  7. x/2
  8. end
  9. end
  10.  
  11. def G t
  12. tt=t
  13. n=0
  14. until tt==1
  15. tt=F(tt)
  16. n+=1
  17. end
  18. n
  19. end
  20.  
  21. def H n
  22. m_t=-1
  23. m_gt=-1
  24. (5..n).each{|t|
  25. gt=G(t)
  26. if m_gt < gt
  27. m_gt=gt
  28. m_t=t
  29. end
  30. }
  31. [m_t,m_gt]
  32. end
  33.  
  34. if __FILE__==$0
  35. while gets
  36. puts H($_.to_i).join(', ')
  37. end
  38. end
  39.  
Success #stdin #stdout 1.67s 7048KB
stdin
5
6
7
8
9
10
2012
32767
stdout
5, 5
6, 8
7, 16
7, 16
9, 19
9, 19
1161, 181
26623, 307