>>514 Ruby

suffixes = %w[th st nd rd]
f = -> n {(-(-((n - 10) / 10 % 10) / 9)) * 41 / (n % 10 * 10 + 11) * (n % 10)}

[
  1, 2, 3, 4, 11, 12, 13, 21, 22, 23, 101, 102, 103, 111, 112, 113, 121, 122, 123
].each{|v| puts '%1$d => %1$d%2$s' % [v, suffixes[f[v]]]}

# =>
1 => 1st
2 => 2nd
3 => 3rd
4 => 4th
11 => 11th
12 => 12th
13 => 13th
21 => 21st
22 => 22nd
23 => 23rd
101 => 101st
102 => 102nd
103 => 103rd
111 => 111th
112 => 112th
113 => 113th
121 => 121st
122 => 122nd
123 => 123rd