m = {1 => ""} r = 1 p = '' (1..100).each { |c| mof = c % 5 == 0 mot = c % 3 == 0 done = mot && !mof && (c + 1) % 5 != 0 && (c + 2) % 5 != 0 if mot && !mof c.to_s.length.times { m[r] << " "} unless done r += 1 if m[r].nil? #newline m[r] = "" (m[r-1].to_s.length - c.to_s.length).times { m[r] << " " } end elsif mof && !mot c.to_s.length.times { m[r] << " "} r -= 1 end c.to_s.length.times { m[r-1] << " " } if r != 1 && !mot c.to_s.length.times { m[r+1] << " " } if !m[r+1].nil? && !mof m[r] << c.to_s p += m[r-1] + "\n" if done } puts p + m[r] + "\n" + m[r+1]