Collatz Numbers

# Collatz numbers

def collatz(n)
  while n > 1
    yield n
    if n%2 == 0 then
      n = n/2
    else
      n = 3*n + 1
    end
  end
  yield 1
end

(1..128).each {|n|
  count = 0
  collatz(n) {|i| count += 1}
  printf "#{n}:#{count} "
}

Note the shortest periods at n = 2^k and that there are many places count(n)==count(n+1) such as n = 12, 18, 22,28,..

1:1 2:2 3:8 4:3 5:6 6:9 7:17 
8:4 9:20 10:7 11:15 12:10 13:10 14:18 15:18 
16:5 17:13 18:21 19:21 20:8 21:8 22:16 23:16 
24:11 25:24 26:11 27:112 28:19 29:19 30:19 31:107 
32:6 33:27 34:14 35:14 36:22 37:22 38:22 39:35 
40:9 41:110 42:9 43:30 44:17 45:17 46:17 47:105 
48:12 49:25 50:25 51:25 52:12 53:12 54:113 55:113 
56:20 57:33 58:20 59:33 60:20 61:20 62:108 63:108 
64:7 65:28 66:28 67:28 68:15 69:15 70:15 71:103 
72:23 73:116 74:23 75:15 76:23 77:23 78:36 79:36 
80:10 81:23 82:111 83:111 84:10 85:10 86:31 87:31 
88:18 89:31 90:18 91:93 92:18 93:18 94:106 95:106 
96:13 97:119 98:26 99:26 100:26 101:26 102:26 103:88 
104:13 105:39 106:13 107:101 108:114 109:114 110:114 111:70 
112:21 113:13 114:34 115:34 116:21 117:21 118:34 119:34 
120:21 121:96 122:21 123:47 124:109 125:109 126:109 127:47 
128:8