## Ruby 1.9 num = "#{?b.ord-?a.ord}" one = num.to_i limit = ?U.ord-?A.ord loop { puts num num = num.gsub(/(?<digit>.)\k<digit>*/) { $&.size.to_s + $+ } break if (limit -= one) == one - one } A golfed version: k="#{?b.ord-?a.ord}" j=k.to_i i=?U.ord-?A.ord loop{puts k k=k.gsub(/(?<d>.)\k<d>*/){$&.size.to_s+$+} (i-=j)==j-j&&exit}