m = {1 => ""} r = 1 rd = {1 => 0} (1..100).each do |c| mof = c % 5 == 0 mot = c % 3 == 0 if mot && !mof rd[r] += 1 c.to_s.length.times { m[r] << " "} unless rd[r] == 2 r += 1 end if mof && !mot c.to_s.length.times { m[r] << " "} r -= 1 end if m[r].nil? m[r] = "" rd[r] = 0 (m[r-1].to_s.length - c.to_s.length).times { m[r] << " " } end c.to_s.length.times { m[r-1] << " " } unless r == 1 || mot || rd[r-1] == 2 c.to_s.length.times { m[r+1] << " " } unless m[r+1].nil? || mof m[r] << c.to_s end (1..15).each {|i| puts m[i].to_s}